[英]multiple delimiters (words) in vba
多個定界符如何用於split()函數? 我想使用兩個以上的單詞作為分隔符,但不確定如何做到。
c = Trim(t.value)
arr = Split(c, "hello" , "hi")
您可以先使用replace替換多個單詞,然后拆分使用。
例如
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.