Trim function does not remove spaces at end of a string while comparing two strings in vbs

陌路散爱 提交于 2019-12-06 13:33:40

VBScript's Trim removes spaces/blanks, not other kinds of whitespace. You'll need a RegExp to clean strings with leading/trailing vbTab, vbCrLf, ... which you often get, when you process the output of .Run or. Exec.

Demo snippet:

>> s1 = "abc" & vbCrLf & " "
>> s2 = "abc"
>> WScript.Echo Len(s1), Len(s2)
>> set r = New RegExp
>> r.Global = True
>> r.Pattern = "^\s+|\s+$"
>> s1 = r.Replace(s1, "")
>> s2 = r.Replace(s2, "")
>> WScript.Echo Len(s1), Len(s2)
>> WScript.Echo CStr(s1 = s2)
>>
6 3
3 3
True
Dave Allonby

You might like to try this:

Dim str1, str2
str1 = "help"
str2 = "Me"
WScript.Echo str1 & str2
str1 = str1 & vbTab
WScript.Echo str1 & str2
WScript.Echo len(str1) & " " & len(str2)
If Right(str1,1) = vbTab Then
    WScript.Echo left(str1,Len(str1) - 1) & str2
Else
    WScript.echo "Oops"
End If

Note that the trailing vbTab added to str1 is now removed simply by removing the last character.

This is useful if you want to preserve the inner VbTabs if you were going to use them to split the string into an array, for example.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!