C# 中判斷字符串是不是漢字


//1、用ASCII碼判斷
//在 ASCII碼表中,英文的范圍是0-127,而漢字則是大於127,具體代碼如下:
string text = "是不是漢字,ABC,柯樂義";
        for (int i = 0; i < text.Length; i++)
        {
            if ((int)text[i] > 127)
            {
                Console.WriteLine("是漢字");
            }
            else
            {
                Console.WriteLine("不是漢字");
            }
        } 

//2、用漢字的 UNICODE 編碼范圍判斷
//漢字的 UNICODE 編碼范圍是4e00-9fbb,具體代碼如下:
 string text = "是不是漢字,ABC,keleyi.com";
            char[] c = text.ToCharArray();
            for (int i = 0; i < c.Length; i++)
            {
                if (c[i] >= 0x4e00 && c[i] <= 0x9fbb)
                {
                    Console.WriteLine("是漢字");
                }
                else
                {
                    Console.WriteLine("不是漢字");
                }
            }

//3、用正則表達式判斷
//用正則表達式判斷也是用漢字的 UNICODE 編碼范圍,具體代碼如下:
string text = "是不是漢字,ABC,keleyi.com";
             for (int i = 0; i < text.Length; i++)
             {
                   if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]+{1}"))
                   {
                       Console.WriteLine("是漢字");
                   }
                   else
                   {
                       Console.WriteLine("不是漢字");
                   }    
             }

 

歡迎評論。。。。讓我看到你的反饋。。。。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM