[C#] C# 判断中文字符的六种方法 →→→→→进入此内容的聊天室

来自 , 2020-03-19, 写在 C#, 查看 97 次.
URL http://www.code666.cn/view/27584e8c
  1. protected bool IsChineseLetter(string input,int index)
  2. {
  3. int code = 0;
  4. int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend)
  5. int chend = Convert.ToInt32("9fff", 16);
  6. if (input != "")
  7. {
  8. code = Char.ConvertToUtf32(input, index); //获得字符串input中指定索引index处字符unicode编码
  9.  
  10. if (code >= chfrom && code <= chend)
  11. {
  12. return true; //当code在中文范围内返回true
  13.  
  14. }
  15. else
  16. {
  17. return false ; //当code不在中文范围内返回false
  18. }
  19. }
  20. return false;
  21. }
  22.  
  23. 方法二:
  24. http://hi.baidu.com/yhfd/blog/item/3222e1fca22cfb80b901a027.html
  25. public bool IsChina(string CString)
  26. {
  27. bool BoolValue = false;
  28. for (int i = 0; i < CString.Length; i++)
  29. {
  30. if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
  31. {
  32. BoolValue = false;
  33. }
  34. else
  35. {
  36. return BoolValue = true;
  37. }
  38. }
  39. return BoolValue;
  40. }
  41.  
  42. 方法三:
  43.  
  44. /// <summary>
  45. /// 判断句子中是否含有中文 宁夏大学 张冬 zd4004.blog.163.com
  46. /// </summary>
  47. /// <param >字符串</param>
  48. public bool WordsIScn(string words)
  49. {
  50. string TmmP;
  51.  
  52. for (int i = 0; i < words.Length; i++)
  53. {
  54. TmmP = words.Substring(i, 1);
  55.  
  56. byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP);
  57.  
  58. if (sarr.Length == 2)
  59. {
  60. return true;
  61. }
  62. }
  63. return false;
  64. }
  65.  
  66.  
  67. 方法四:
  68. for (int i=0; i<s.length; i++)
  69. {
  70. Regex rx = new Regex("^[\u4e00-\u9fa5]$");
  71. if (rx.IsMatch(s[i]))
  72. // 是
  73. else
  74. // 否
  75. }
  76. 正解!
  77. \u4e00-\u9fa5 汉字的范围。
  78. ^[\u4e00-\u9fa5]$ 汉字的范围的正则
  79.  
  80. 方法五
  81. unicodeencoding unicodeencoding = new unicodeencoding();
  82. byte [] unicodebytearray = unicodeencoding.getbytes( inputstring );
  83. for( int i = 0; i < unicodebytearray.length; i++ )
  84. {
  85. i++;
  86. //如果是中文字符那么高位不为0
  87. if ( unicodebytearray[i] != 0 )
  88. {
  89. }
  90. ……
  91.  
  92. 方法六
  93. /// <summary>
  94. /// 给定一个字符串,判断其是否只包含有汉字
  95. /// </summary>
  96. /// <param name="testStr"></param>
  97. /// <returns></returns>
  98. public bool IsOnlyContainsChinese(string testStr)
  99. {
  100. char[] words = testStr.ToCharArray();
  101. foreach (char word in words)
  102. {
  103. if ( IsGBCode(word.ToString()) || IsGBKCode(word.ToString()) ) // it is a GB2312 or GBK chinese word
  104. {
  105. continue;
  106. }
  107. else
  108. {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114.  
  115. /// <summary>
  116. /// 判断一个word是否为GB2312编码的汉字
  117. /// </summary>
  118. /// <param name="word"></param>
  119. /// <returns></returns>
  120. private bool IsGBCode(string word)
  121. {
  122. byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
  123. if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code
  124. {
  125. return false;
  126. }
  127. else
  128. {
  129. byte byte1 = bytes[0];
  130. byte byte2 = bytes[1];
  131. if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判断是否是GB2312
  132. {
  133. return true;
  134. }
  135. else
  136. {
  137. return false;
  138. }
  139. }
  140. }
  141.  
  142. /// <summary>
  143. /// 判断一个word是否为GBK编码的汉字
  144. /// </summary>
  145. /// <param name="word"></param>
  146. /// <returns></returns>
  147. private bool IsGBKCode(string word)
  148. {
  149. byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString());
  150. if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
  151. {
  152. return false;
  153. }
  154. else
  155. {
  156. byte byte1 = bytes[0];
  157. byte byte2 = bytes[1];
  158. if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254) //判断是否是GBK编码
  159. {
  160. return true;
  161. }
  162. else
  163. {
  164. return false;
  165. }
  166. }
  167. }
  168.  
  169. /// <summary>
  170. /// 判断一个word是否为Big5编码的汉字
  171. /// </summary>
  172. /// <param name="word"></param>
  173. /// <returns></returns>
  174. private bool IsBig5Code(string word)
  175. {
  176. byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString());
  177. if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
  178. {
  179. return false;
  180. }
  181. else
  182. {
  183. byte byte1 = bytes[0];
  184. byte byte2 = bytes[1];
  185. if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254)) ) //判断是否是Big5编码
  186. {
  187. return true;
  188. }
  189. else
  190. {
  191. return false;
  192. }
  193. }
  194. }
  195.  
  196. //csharp/1145

回复 "C# 判断中文字符的六种方法"

这儿你可以回复上面这条便签

captcha