gpt4 book ai didi

vb.net - 比较字符串 - ASCII SPACE

转载 作者:行者123 更新时间:2023-12-04 01:17:59 24 4
gpt4 key购买 nike

这样做有什么区别:

Dim strTest As String
If strTest > " " Then

End If

还有这个:

Dim strTest As String
If strTest <> "" Then

End If

我认为代码示例 1 正在比较 ASCII 值(SPACE 的 ASCII 代码是 32)。我查看了 MSDN 上的字符串部分,但找不到答案。

更新

我也对这里发生的事情感到困惑:

 Dim strTest As String = "Test"
If strTest > " " Then

End If

最佳答案

> (大于)运算符将按字母顺序或字符代码值顺序进行测试(取决于 Option Compare 设置),而 <> (不等于)运算符测试是否相等。只要两个字符串完全不同,那么 <>将始终评估为 True . >只要运算符右侧的字符串按字母顺序或字符代码值排在第一个字符串之后,就会计算为真。因此:

Option Compare Text  ' Compare strings alphabetically

...

Dim x As String = "Hello"
Dim y As String = "World"

If x <> y Then
' This block is executed because the strings are different
Else
' This block is skipped
End If

If x > y Then
' This block is skipped
Else
' This block is executed because "Hello" is less than "World" alphabetically
End If

但是,在您的问题中,您将空字符串变量(设置为 Nothing )与空字符串进行比较。在这种情况下,比较运算符将 null 变量视为空字符串。因此,Nothing <> ""应评估为 False因为运算符的两边都被认为是空字符串。空字符串或 null 字符串应始终被视为排序顺序中的第一个,因此 Nothing > "Hello"应评估为 False因为空字符串出现在其他所有内容之前。但是,Nothing > ""应评估为 False因为它们是相等的,因此既不在另一个之前也不在另一个之后。

回答你最后一个问题,"Test" > " "将测试字母 T 是在空格之前还是之后。如果Option Compare设置为 Text , 它将按字母顺序比较它们并返回 True (这最终取决于您所在地区的字母排序)。如果Option Compare设置为 Binary , 它会根据它们的字符代码值来比较它们。如果它们是 ASCII 字符串,空格字符的值低于字母,如 T,因此它也应该返回 True。 .

关于vb.net - 比较字符串 - ASCII SPACE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12021261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com