繁体   English   中英

vba中的多个定界符(单词)

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM