C# 中判断字符是否大写

发布时间 2023-04-21 10:01:13作者: 糯米白白

在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')<br><br>            {
                return 1;
            }
            else
            {
                return 0;
            }
        }
    }
}

我们可以看到字符'B'之前有个66,这个'B'对应的ACSII码中数值,由此得知,其实C#中字符比较实际上比较的是ACSII码,具体字符对应的ASCII码如下图所示:

原文链接:https://www.cnblogs.com/qianxingdewoniu/p/5682308.html