Home >Backend Development >C#.Net Tutorial >Example of how regular expressions in C# validate Chinese characters
This article introduces you to the method of using C# regular expressions to verify Chinese characters through example code. Friends who need it can refer to it.
No more nonsense, I will directly post the code for you. The details The code looks like this:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace 正则表达01 { /// <summary> /// 表达是否是字符是不是中文 /// </summary> class Program { /// <summary> /// 在 ASCII码表中,英文的范围是0-127,而汉字则是大于127 /// </summary> static void justice1() { string text = "adonai,天下英雄出我辈,一入江湖岁月催。鸿图霸业谈笑间,不胜人生一场醉。 提剑跨骑挥鬼雨,白骨如山鸟惊飞。 尘世如潮人如水,只叹江湖几人回"; for (int i = 0; i < text.Length; i++) { if ((int)text[i] > 127) Console.WriteLine("是汉字"); else Console.WriteLine("不是汉字"); } } /// <summary> /// 汉字的UNICODE编码范围是4e00-9fbb /// </summary> static void justice2() { string text = "adonai,天下英雄出我辈,一入江湖岁月催。鸿图霸业谈笑间,不胜人生一场醉。 提剑跨骑挥鬼雨,白骨如山鸟惊飞。 尘世如潮人如水,只叹江湖几人回"; char[] c = text.ToCharArray(); for (int i = 0; i < c.Length; i++) if (c[i] >= 0x4e00 && c[i] <= 0x9fbb) Console.WriteLine("是汉字"); else Console.WriteLine("不是汉字"); } /// <summary> /// 正则表达式判断也是用汉字的 UNICODE 编码范围 /// </summary> static void justice3() { string text = "adonai,天下英雄出我辈,一入江湖岁月催。鸿图霸业谈笑间,不胜人生一场醉。 提剑跨骑挥鬼雨,白骨如山鸟惊飞。 尘世如潮人如水,只叹江湖几人回"; for (int i = 0; i < text.Length; i++) { if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]")) Console.WriteLine("是汉字"); else Console.WriteLine("不是汉字"); } } static void Main(string[] args) { justice1(); Console.ReadKey(); } } }
The above is the detailed content of Example of how regular expressions in C# validate Chinese characters. For more information, please follow other related articles on the PHP Chinese website!