【发布时间】:2015-05-19 08:21:57
【问题描述】:
目标
按字典顺序比较两个字符串,忽略大小写。
使用 StrComp 的可能解决方案
考虑以下脚本:
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-ing 或UCase-ing 这两个变量作为解决方法。 - 但是为什么
StrComp不能使用vbTextCompare得出相同的结论呢?我以为vbTextCompare的定义就是比较忽略大小写?
【问题讨论】: