Ben*_*iel 2 vbscript vba qtp hp-uft
按字典顺序比较两个字符串,忽略大小写.
请考虑以下脚本:
val1 = "test9999"
val2 = "TEST_59895"
LexCompare val1, val2, vbBinaryCompare
LexCompare LCase(val1), LCase(val2), vbBinaryCompare
LexCompare UCase(val1), UCase(val2), vbBinaryCompare
LexCompare val1, val2, vbTextCompare
LexCompare LCase(val1), LCase(val2), vbTextCompare
LexCompare UCase(val1), UCase(val2), vbTextCompare
WScript.Echo "ANSI values: '9'=" & Asc("9") & ", '_'=" & Asc("_")
Sub LexCompare(string1, string2, compareType)
result = ""
Select Case StrComp(string1, string2, compareType)
Case -1
result = "is smaller than"
Case 0
result = "is identical to"
Case 1
result = "is greater than"
End Select
WScript.Echo "'" & string1 & "' " & result & " '" & string2 & "', compareType: " & compareType
End Sub
Run Code Online (Sandbox Code Playgroud)
输出:
'test9999' is greater than 'TEST_59895', compareType: 0
'test9999' is smaller than 'test_59895', compareType: 0
'TEST9999' is smaller than 'TEST_59895', compareType: 0
'test9999' is greater than 'TEST_59895', compareType: 1
'test9999' is greater than 'test_59895', compareType: 1
'TEST9999' is greater than 'TEST_59895', compareType: 1
ANSI values: '9'=57, '_'=95
Run Code Online (Sandbox Code Playgroud)
对我来说,"test9999"应该按字典顺序小于"TEST_59895",忽略大小写.为什么?因为'9'小于'_'.
vbBinaryCompare,并将使用LCase-ing或UCase-ing两个变量作为变通方法.StrComp使用相同的结论vbTextCompare?我认为这个定义vbTextCompare是为了比较忽略的情况?