在C#中,通常判断一个字符是否为大写字母,有些人可能会第一时间想到用正则表达式,那除了正则表达式,是否还有其他方式呢?
答案是肯定的,先一睹为快,具体代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Demo_GC
{
class Program
{
static void Main(string[] args)
{
char c = 'B';
CompareChar(c);
}
/// <summary>
/// 判断字符是否为大写字母
/// </summary>
/// <param name="c"></param>
/// <returns></returns>
private static int CompareChar(char c)
{
if (c > 'A' && c < 'Z')
{
return 1;
}
else
{
return 0;
}
}
}
}
没错,字符也可以这样直接比较,这里的字符比较其实是比较字符对应的ACSII码,调试状态监控变量c,如下:
我们可以看到字符'B'之前有个66,这个'B'对应的ACSII码中数值,由此得知,其实C#中字符比较实际上比较的是ACSII码,具体字符对应的ASCII码如下图所示: