使用vbTextCompare时StrComp的意外结果

Ben*_*iel 2 vbscript vba qtp hp-uft

目标

按字典顺序比较两个字符串,忽略大小写.

使用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
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是为了比较忽略的情况?

GSe*_*erg 5

效果vbTextCompare是将规则Option Compare Text应用于那一个比较.

您可以在文档中看到Option Compare Text不依赖于ANSI值,而是依赖于它

不区分大小写的文本排序顺序由系统的语言环境决定.

您的区域设置可以指示任何排序顺序,因此它会在下划线后对数字进行排序.