2

I have a simple script which takes two strings and compare them. The first one has a space at the end and the second does not have that.

Function compare(str1,str2)
 dim a 
 If strComp(trim(str1),trim(str2))=0 Then 
     msgbox "OK"
     a=1  
 Else
     msgbox "KO" 
     a=0
 End If    

 compare=a

End Function

I use this function in this way:

s1=     SUCCESSFULLY CONNECTED
s2=     SUCCESSFULLY CONNECTED
result=compare(s1,s2)

The difference between s1 and s2 is that s1 ends with a single space while s2 does not have any space at the end. This why I use the Trim function to ignore that space. Despite that, for s1 and s2, I always get the message "KO" in dialog box. I have even changed the condition by

If trim(str1)=trim(str2) Then

But popup is still returned "KO". This is a wonderful situation!

Please, I'm tired of that and hope that you help understand this situation. Thank you in advance

4

2 回答 2

5

VBScriptTrim删除空格/空格,而不是其他类型的空白。当您处理 .Run 或的输出时,您将需要一个 RegExp 来清理带有前导/尾随 vbTab、vbCrLf、... 的字符串。执行。

演示片段:

>> 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
于 2013-07-16T11:40:25.493 回答
1

你可能想试试这个:

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

请注意,添加到的尾随 vbTabstr1现在只需删除最后一个字符即可删除。

例如,如果您想保留内部 VbTab(如果您打算使用它们将字符串拆分为数组),这将非常有用。

于 2013-10-04T19:09:59.397 回答