如何将多个分隔符用于split()函数?我想使用两个以上的单词作为分隔符,但我不确定这是怎么可能的。
c = Trim(t.value)
arr = Split(c, "hello" , "hi")
答案 0 :(得分:6)
您可以先使用替换替换多个单词,然后在拆分中使用它。
e.g。
mystring= Replace(mystring, "hello", "#")
mystring= Replace(mystring, "hi", "#")
mystring= Replace(mystring, "thanks", "#")
newstring= Split(mystring, "#")
答案 1 :(得分: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