//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("不是漢字"); } }
歡迎評論。。。。讓我看到你的反饋。。。。
