有一个用 C# 重写的旧 vb6 项目,并且一个函数具有以下代码:
If (strPlainChar >= "A" And strPlainChar <= "Z") Then
但显然 C# if 语句不允许大于字符串,我该如何重新创建这段代码?
有一个用 C# 重写的旧 vb6 项目,并且一个函数具有以下代码:
If (strPlainChar >= "A" And strPlainChar <= "Z") Then
但显然 C# if 语句不允许大于字符串,我该如何重新创建这段代码?
You can use the String.CompareTo
method to compare strings:
strPlainChar.CompareTo("A") >= 0 && strPlainChar.CompareTo("Z") <= 0
Or if these are just characters, you can use the standard comparison operators:
strPlainChar >= 'A' && strPlainChar <= 'Z'
If strPlainChar
consists only of a single character, you would use the char
type instead of string
:
char strPlainChar = 'G';
if (strPlainChar >= 'A' && strPlainChar <= 'Z')
{
...
}
你可以这样做:
char c = strPlainChar[0];
if (c >= 'A' && c <= 'Z')
但我认为这对于 C# 来说更惯用:
if (char.IsLetter(strPlainChar, 0) && char.IsUpper(strPlainChar, 0))