vbScript 在拆分后忽略许多空格

vbScript ignore many blank spaces after split

当我拆分一个有很多空格的字符串时,有没有办法跳过空格?

下面的示例字符串:

Lorem                            ipsum dolor sit amet, consectetur
             adipiscing elit. Morbi cursus quam sapien, sed ultricies diam vestibulum ac.
Morbi                    luctus nisl eleifend                    mi tincidunt, 
sed vehicula magna lobortis. 

拆分时,数组中有很多位置的" "(空格)

[0] Lorem
[1] " "
[2] " "
[3] " "
[4] " "
[5] Ipsum

那么,有没有办法跳过这个空白并得到类似的东西?

[0] Lorem
[1] Ipsum
[3] dolor

这是我的代码:

strTmp = split(tmpstr," ")
For each text in strTmp
'Here I validate other things
  If InStr(x,textToFind) Then
    print "text found"
  Else
    print "not found"
  End If
Next

可以在字符串上做一个循环,用单个空格替换双空格

Do Until InStr(text, "  ") = 0
        text= Replace(text, "  ", " ")
    Loop

你可以试试这个

If trim(text) <> "" Then
Else
End if

或者

If len(trim(text)) > 0 Then
Else
End if

其中一种方法是在拆分字符串之前对其进行处理。

示例代码

varStr = "Lorem                            ipsum dolor sit amet, consectetur             adipiscing elit. Morbi cursus quam sapien, sed ultricies diam vestibulum ac. Morbi                    luctus nisl eleifend                    mi tincidunt, sed vehicula magna lobortis"

' this is what you are getting right now
arrStr = Split(varStr, " ")

Set objRegEx = CreateObject("VBScript.RegExp")
With objRegEx
    .Global = True
    .MultiLine = True
    .Pattern = "\s+" 'matches any whitespace character
    varStr1 = objRegEx.Replace(varStr, "¬")
End With
Set objRegEx = Nothing

' this is what you want
arrStr1 = Split(varStr1, "¬")

我首先去除了所有空格并用一个 ¬ 替换它,稍后我拆分字符串时它将用作分隔符。