vba 如何将字符串和分隔符拆分为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/25299074/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to split string and delimiters into an array
提问by siwanut jinnachit
I have the following string:
我有以下字符串:
top,fen,test,delay,test
top,fen,test,delay,test
I want to convert it into an array in the following way:
我想通过以下方式将其转换为数组:
{top}{,}{fen}{,}{delay}{,}{test}
{top}{,}{fen}{,}{delay}{,}{test}
回答by David Zemens
If you actually need the commas as part of the array, then probably the simplest approach is to do a Replacestatement, replacing each comma with a comma surrounded by some other character(s) you can use as a delimiter. Whatever character you opt to use should be unique enough that it is unlikely to appear in the rest of your word list.  I will use an underscore, here, but you could use any other special character.
如果您确实需要将逗号作为数组的一部分,那么最简单的方法可能是执行Replace语句,将每个逗号替换为一个逗号,该逗号由一些其他字符包围,您可以将其用作分隔符。您选择使用的任何字符都应该足够独特,以至于它不太可能出现在您的单词列表的其余部分中。我将在这里使用下划线,但您可以使用任何其他特殊字符。
Sub test()
Dim wordlist As String
Dim arrayofWords
Dim i
wordlist = "top,fen,test,delay,test"
wordlist = Replace(wordlist, ",", "_,_")
arrayofWords = Split(wordlist, "_")
'Enclose each word in curly brackets
' omit if this part is not needed
For i = LBound(arrayofWords) To UBound(arrayofWords)
    arrayofWords(i) = "{" & arrayofWords(i) & "}"
Next
End Sub
You could use some funky double-byte character since these would rarely if ever be encountered in your word list, like so.
您可以使用一些时髦的双字节字符,因为在您的单词列表中很少会遇到这些字符,就像这样。
Sub test()
Const oldDelimiter As String = ","
Dim splitter As String
Dim newDelimiter As String
Dim wordlist As String
Dim arrayofWords
Dim i As Long
'Create our new delimiter by concatenating a new string with the comma:
splitter = ChrW(&H25B2)
newDelimiter = splitter & oldDelimiter & splitter
'Define our word list:
wordlist = "top,fen,test,delay,test"
'Replace the comma with the new delimiter, defined above:
wordlist = Replace(wordlist, oldDelimiter, newDelimiter)
'Use SPLIT function to convert the string to an array
arrayofWords = Split(wordlist, splitter)
'Iterate the array and add curly brackets to each element
'Omit if this part is not needed
For i = LBound(arrayofWords) To UBound(arrayofWords)
    arrayofWords(i) = "{" & arrayofWords(i) & "}"
Next
End Sub
Here are results from the second method:
以下是第二种方法的结果:


回答by citizenkong
Here's a reallyshort solution:
这是一个非常简短的解决方案:
Function StringToCurlyArray(s As String) As String()
    StringToCurlyArray = Split("{" & Replace(s, ",", "}|{,}|{") & "}", "|")
End Function
Pass your comma-delimited string into that and you'll get an array of curly-braced strings back out, including commas.
将逗号分隔的字符串传递到其中,您将得到一组花括号字符串,包括逗号。
回答by Fabien TheSolution
Try something like this :
尝试这样的事情:
WordsList = Split("top,fen,test,delay,test", ",")
Result = ""
Count = UBound(WordsList)
For i = 0 To Count
    Result = Result  & "{" & WordsList(i) & "}"
    if i < Count then Result = Result & "{,}"
Next i
In an array will look like this :
在数组中将如下所示:
WordsList = Split("top,fen,test,delay,test", ",")
Dim Result()
Count = (UBound(WordsList)*2) - 1
Redim Result(Count)
j = 0
For i = 0 To UBound(WordsList)
    Result(j) = WordsList(i)
    j = j + 1
    if j < Count then Result(j) = ","
    j = j + 1
Next i
Split : http://msdn.microsoft.com/en-us/library/6x627e5f%28v=vs.90%29.aspx
拆分:http: //msdn.microsoft.com/en-us/library/6x627e5f%28v=vs.90%29.aspx
UBound : http://msdn.microsoft.com/en-us/library/95b8f22f%28v=vs.90%29.aspx
UBound : http://msdn.microsoft.com/en-us/library/95b8f22f%28v=vs.90%29.aspx
Redim : http://msdn.microsoft.com/en-us/library/w8k3cys2.aspx
Redim:http: //msdn.microsoft.com/en-us/library/w8k3cys2.aspx

