0
多個分隔符如何用於split()函數?我想用兩個以上的單詞作爲分隔符,但我不確定這是可能的。vba中的多個分隔符(單詞)
c = Trim(t.value)
arr = Split(c, "hello" , "hi")
多個分隔符如何用於split()函數?我想用兩個以上的單詞作爲分隔符,但我不確定這是可能的。vba中的多個分隔符(單詞)
c = Trim(t.value)
arr = Split(c, "hello" , "hi")
您可以先使用replace替換多個單詞,然後在split中使用它。
例如
mystring= Replace(mystring, "hello", "#")
mystring= Replace(mystring, "hi", "#")
mystring= Replace(mystring, "thanks", "#")
newstring= Split(mystring, "#")
你可以去像如下:
Option Explicit
Sub main()
Dim t As String
Dim arr As Variant, seps As Variant, sep As Variant
seps = Array("hello", "hi") '<--| define your seperators list
t = "hello i would hello like to hi split this string hello with multiple hello separators hi" '<--| string to split
t = " " & t & " " '<--| add trailing blank to catch possible "border" 'separators'
For Each sep In seps
t = Replace(t, " " & sep & " ", "|") 'turn all separators into one only
Next sep
t = Trim(t) '<--| remove trailing blanks
If Left(t, 1) = "|" Then t = Right(t, Len(t) - 1) '<--| remove any initial 'separator'
If Right(t, 1) = "|" Then t = Left(t, Len(t) - 1) '<--| remove any final 'separator'
arr = Split(t, "|")
End Sub
我希望能夠拉分隔符字沿前一次分裂。有沒有辦法找回來? – johndoe253
沒有得到。你可以解釋嗎? – Techidiot
如果你想拉分隔符,只需在每次更換後重復「分割」 – Stavm