3

有一个用 C# 重写的旧 vb6 项目,并且一个函数具有以下代码:

If (strPlainChar >= "A" And strPlainChar <= "Z") Then

但显然 C# if 语句不允许大于字符串,我该如何重新创建这段代码?

4

3 回答 3

8

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'
于 2013-05-12T18:49:02.093 回答
6

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')
{
    ...
}
于 2013-05-12T18:49:29.617 回答
2

可以这样做:

char c = strPlainChar[0];
if (c >= 'A' && c <= 'Z')

但我认为这对于 C# 来说更惯用:

if (char.IsLetter(strPlainChar, 0) && char.IsUpper(strPlainChar, 0))
于 2013-05-12T18:51:20.797 回答