2016-09-14 135 views

回答

6

您可以先使用replace替换多个单词,然后在split中使用它。

例如

mystring= Replace(mystring, "hello", "#") 
mystring= Replace(mystring, "hi", "#") 
mystring= Replace(mystring, "thanks", "#") 
newstring= Split(mystring, "#") 
+0

我希望能够拉分隔符字沿前一次分裂。有没有办法找回来? – johndoe253

+0

没有得到。你可以解释吗? – Techidiot

+0

如果你想拉分隔符,只需在每次更换后重复“分割” – Stavm

1

你可以去像如下:

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