当前位置:   article > 正文

C#验证字符串是否包含汉字:用正则表达式 vs 用ASCII码 vs 用汉字的 Unicode 编码_用正则表达式验证是否验证是否包含中文字

用正则表达式验证是否验证是否包含中文字

目录

一、使用的方法

1.使用正则表达式验证字符串

2.使用正则表达式验证字符

3.用ASCII码判断

4.用汉字的 Unicode 编码范围判断

二、实例

1.源码

2.生成效果


         验证一个字符串是否是纯汉字或者包含有汉字的前提,是VS编辑器的默认编码格式设置为:选择 Unicode (UTF-8 带签名 - 代码页655001)。

一、使用的方法

1.使用正则表达式验证字符串

         这种方法是有缺陷的(网上的很多例子都有这样的缺陷),是不完美的,是有限制的,换句话说,用正则表达式只能验证一个字符串是纯汉字的情况,当字符串是汉字、数字、字母、符号的无序混合体的时候,用正则表达式匹配字符串的返回值都是false,只有纯汉字的情况下返回值才是true。

  1. // 用正则表达式验证字符串是否纯汉字
  2. using System.Text.RegularExpressions;
  3. namespace _088_1
  4. {
  5. class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. ArgumentNullException.ThrowIfNull(args);
  10. string[] input = ["ss","ss达到","到达","到达ss", "ss达到ss"];
  11. foreach(string inputItem in input)
  12. {
  13. if (ContainsChineseCharacter(inputItem))
  14. {
  15. Console.WriteLine("字符串是纯汉字。");
  16. }
  17. else
  18. {
  19. Console.WriteLine("无法判断是否包含汉字。");
  20. }
  21. }
  22. }
  23. /// <summary>
  24. /// 使用正则表达式匹配纯汉字字符串
  25. /// </summary>
  26. static bool ContainsChineseCharacter(string input)
  27. {
  28. string pattern = @"^[\u4e00-\u9fa5]+$";
  29. return Regex.IsMatch(input, pattern);
  30. }
  31. }
  32. }
  33. //运行结果:
  34. /*
  35. 无法判断是否包含汉字。
  36. 无法判断是否包含汉字。
  37. 字符串是纯汉字。
  38. 无法判断是否包含汉字。
  39. 无法判断是否包含汉字。
  40. */

2.使用正则表达式验证字符

        当然了,不是说用正则表达式的方法解决不了这个问题,而是执行方法的细节用错了。

        根据1里面的描述和例子,用正则表达式判断一个字符(char)是否是汉字100%的灵验。那么,标题的设计目的就解决了:先把字符串转换成字符数组,然后对数组遍历根据正则表达式判断当前字符是否汉字,只要有一个字符是汉字,那么就返回字符串中包含有汉字。

        适用的正则表达式可以是:^[\u4E00-\u9FA5]+$、^[\u4E00-\u9FA5]*$、^[\u4e00-\u9fa5]{0,}$、^[\u4e00-\u9fa5]{1,}$ ,其中, ^[\u4e00-\u9fa5]{0,}$   要做字符串为空的判断。

3.用ASCII码判断

        在 ASCII码表中,英文的范围是0-127,而汉字则是大于127。因此,可以通过遍历判断一个字符串中是否包含有汉字。

4.用汉字的 Unicode 编码范围判断

        汉字的 Unicode 编码范围是4e00-9fbb。因此,可以通过遍历判断一个字符串中是否包含有汉字。

        好啦,翠花,上源码!

二、实例

        本实例作者用3种方法实现设计目的:

        验证1:用正则表达式验证字符串中是否包含汉字;
        验证2:用ASCII码验证字符串中是否包含汉字;
        验证3:用Unicode汉字编码验证字符串中是否包含汉字;

1.源码

  1. // 用正则表达式验证字符串中是否包含汉字
  2. // 用ASCII码验证字符串中是否包含汉字
  3. // 用UNICODE汉字编码验证字符串中是否包含汉字
  4. using System.Text.RegularExpressions;
  5. namespace _088
  6. {
  7. public partial class Form1 : Form
  8. {
  9. private GroupBox? groupBox1;
  10. private Label? label1;
  11. private Button? button1;
  12. private Button? button2;
  13. private Button? button3;
  14. private TextBox? textBox1;
  15. public Form1()
  16. {
  17. InitializeComponent();
  18. StartPosition = FormStartPosition.CenterScreen;
  19. Load += Form1_Load;
  20. }
  21. private void Form1_Load(object? sender, EventArgs e)
  22. {
  23. //
  24. // label1
  25. //
  26. label1 = new Label
  27. {
  28. AutoSize = true,
  29. Location = new Point(6, 48),
  30. Name = "label1",
  31. Size = new Size(80, 17),
  32. TabIndex = 0,
  33. Text = "输入字符串:"
  34. };
  35. //
  36. // textBox1
  37. //
  38. textBox1 = new TextBox
  39. {
  40. Location = new Point(92, 42),
  41. Name = "textBox1",
  42. Size = new Size(100, 23),
  43. TabIndex = 1
  44. };
  45. //
  46. // button1
  47. //
  48. button1 = new Button
  49. {
  50. Location = new Point(199, 13),
  51. Name = "button1",
  52. Size = new Size(75, 23),
  53. TabIndex = 2,
  54. Text = "验证1",
  55. UseVisualStyleBackColor = true
  56. };
  57. button1.Click += Button1_Click;
  58. //
  59. // button2
  60. //
  61. button2 = new Button
  62. {
  63. Location = new Point(198, 42),
  64. Name = "button2",
  65. Size = new Size(75, 23),
  66. TabIndex = 3,
  67. Text = "验证2",
  68. UseVisualStyleBackColor = true
  69. };
  70. button2.Click += Button2_Click;
  71. //
  72. // button3
  73. //
  74. button3 = new Button
  75. {
  76. Location = new Point(199, 71),
  77. Name = "button3",
  78. Size = new Size(75, 23),
  79. TabIndex = 4,
  80. Text = "验证3",
  81. UseVisualStyleBackColor = true
  82. };
  83. button3.Click += Button3_Click;
  84. //
  85. // groupBox1
  86. //
  87. groupBox1 = new GroupBox
  88. {
  89. Location = new Point(12, 12),
  90. Name = "groupBox1",
  91. Size = new Size(280, 100),
  92. TabIndex = 0,
  93. TabStop = false,
  94. Text = "验证是否含有汉字"
  95. };
  96. groupBox1.Controls.Add(button3);
  97. groupBox1.Controls.Add(button2);
  98. groupBox1.Controls.Add(button1);
  99. groupBox1.Controls.Add(textBox1);
  100. groupBox1.Controls.Add(label1);
  101. groupBox1.SuspendLayout();
  102. //
  103. // Form1
  104. //
  105. AutoScaleDimensions = new SizeF(7F, 17F);
  106. AutoScaleMode = AutoScaleMode.Font;
  107. ClientSize = new Size(304, 123);
  108. Controls.Add(groupBox1);
  109. Name = "Form1";
  110. StartPosition = FormStartPosition.CenterScreen;
  111. Text = "验证字符串中是否含有汉字";
  112. UseWaitCursor = true;
  113. groupBox1.ResumeLayout(false);
  114. groupBox1.PerformLayout();
  115. }
  116. /// <summary>
  117. /// 用正则表达式验证字符串中是否包含汉字
  118. /// </summary>
  119. private void Button1_Click(object? sender, EventArgs e)
  120. {
  121. if(textBox1!.Text != "")
  122. {
  123. char[] input = textBox1!.Text.ToCharArray();
  124. foreach (char c in input)
  125. {
  126. if (ContainsChineseCharacter(c.ToString()))
  127. {
  128. MessageBox.Show("字符串中含有汉字", "验证1");
  129. return;
  130. }
  131. else { }
  132. }
  133. MessageBox.Show("字符串中没有汉字", "验证1");
  134. }
  135. else
  136. {
  137. MessageBox.Show("字符串不能为空", "验证1");
  138. }
  139. }
  140. /// <summary>
  141. /// 用ASCII码验证字符串中是否包含汉字
  142. /// </summary>
  143. private void Button2_Click(object? sender, EventArgs e)
  144. {
  145. if (textBox1!.Text != "")
  146. {
  147. char[] input = textBox1!.Text.ToCharArray();
  148. foreach(char c in input)
  149. {
  150. if (c > 127)
  151. {
  152. MessageBox.Show("字符串中含有汉字", "验证2");
  153. return;
  154. }
  155. else { }
  156. }
  157. MessageBox.Show("字符串中没有汉字", "验证2");
  158. }
  159. else
  160. {
  161. MessageBox.Show("字符串不能为空", "验证2");
  162. }
  163. }
  164. /// <summary>
  165. /// 用UNICODE汉字编码验证字符串中是否包含汉字
  166. /// </summary>
  167. private void Button3_Click(object? sender, EventArgs e)
  168. {
  169. if (textBox1!.Text != "")
  170. {
  171. char[] input = textBox1!.Text.ToCharArray();
  172. foreach (char c in input)
  173. {
  174. if (c >= 0x4e00 && c <= 0x9fbb)
  175. {
  176. MessageBox.Show("字符串中含有汉字", "验证3");
  177. return;
  178. }
  179. else { }
  180. }
  181. MessageBox.Show("字符串中没有汉字", "验证3");
  182. }
  183. else
  184. {
  185. MessageBox.Show("字符串不能为空", "验证3");
  186. }
  187. }
  188. /// <summary>
  189. /// 使用正则表达式匹配纯汉字字符
  190. /// </summary>
  191. static bool ContainsChineseCharacter(string input)
  192. {
  193. string pattern = @"^[\u4e00-\u9fa5]+$";
  194. return Regex.IsMatch(input, pattern);
  195. }
  196. }
  197. }

2.生成效果

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
  

闽ICP备14008679号