拆分后,vbScript会忽略许多空格

时间:2018-05-25 17:36:33

标签: vbscript split hp-uft

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

以下示例字符串:

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

3 个答案:

答案 0 :(得分:3)

其中一种方法是在分割之前处理字符串。

示例代码

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, "¬")

我首先剥离了所有空格,然后将其替换为单个¬,当我稍后拆分字符串时,它将作为一个空格。

答案 1 :(得分:1)

可以对字符串执行循环,并用单个空格替换双空格

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

答案 2 :(得分:0)

你可以试试这个

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

或者

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