如果您确实需要将逗号作为数组的一部分,那么最简单的方法可能是执行一个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
Run Code Online (Sandbox Code Playgroud)
您可以使用一些时髦的双字节字符,因为在您的单词列表中很少会遇到这些字符,就像这样。
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
Run Code Online (Sandbox Code Playgroud)
以下是第二种方法的结果:

| 归档时间: |
|
| 查看次数: |
37371 次 |
| 最近记录: |