按字典顺序比较两个字符串,忽略大小写。
考虑以下脚本:
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
输出:
'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
对我来说," test9999"应该按字典顺序小于" TEST_59895",忽略大小写。为什么?因为' 9'小于' _'。
vbBinaryCompare
时的结果,并使用LCase
- 或UCase
两个变量作为解决方法。StrComp
使用vbTextCompare
得出同样的结论?我认为vbTextCompare
的定义是比较忽略的情况?答案 0 :(得分:5)
vbTextCompare
的效果是将Option Compare Text
的规则应用于这一比较。
您可以在documentation中看到Option Compare Text
不依赖于ANSI值,而是依赖于
不区分大小写的文本排序顺序由系统的区域设置决定。
您的区域设置可以指定任何排序顺序,因此恰好sort digits after underscores。