Advertisement
cancelpc

C# 判斷中文字符有以下六種方法:

Aug 25th, 2017
466
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.15 KB | None | 0 0
  1. /*
  2.   方法一:
  3. */
  4.  
  5.     protected bool IsChineseLetter(string input,int index)
  6.   {
  7.     int code = 0;
  8.     int chfrom = Convert.ToInt32("4e00", 16); // 範圍(0x4e00~0x9fff)轉換成 int(chfrom~chend)
  9.       int chend = Convert.ToInt32("9fff", 16);
  10.  
  11.       if (input != "")
  12.       {
  13.         code = Char.ConvertToUtf32(input, index); // 獲得字符串 input 中指定索引 index 處字符unicode編碼
  14.  
  15.           if (code >= chfrom && code <= chend)
  16.           {
  17.               return true; //當code在中文範圍內返回true
  18.           }
  19.           else
  20.           {
  21.               return false ; //當code不在中文範圍內返回false
  22.           }
  23.       }
  24.  
  25.     return false;
  26.   }
  27.  
  28.  
  29. /*
  30.   方法二:
  31. */
  32.  
  33.   public bool IsChina(string CString)
  34.   {
  35.       bool BoolValue = false;
  36.  
  37.     for (int i = 0; i < CString.Length; i++)
  38.       {
  39.           if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
  40.           {
  41.               BoolValue = false;
  42.           }
  43.           else
  44.           {
  45.                 BoolValue = true;
  46.           }
  47.       }
  48.  
  49.     return BoolValue;
  50.   }
  51.  
  52. /*
  53.   方法三:
  54. */
  55.  
  56.   /// <summary>
  57.   /// 判斷句子中是否含有中文
  58.   /// </summary>
  59.   /// <param >字符串</param>
  60.  
  61.   public bool WordsIScn(string words)
  62.   {
  63.       string TmmP;
  64.  
  65.     for (int i = 0; i < words.Length; i++)
  66.       {
  67.           TmmP = words.Substring(i, 1);
  68.           byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP);
  69.  
  70.             if (sarr.Length == 2)
  71.           {
  72.               return true;
  73.           }
  74.       }
  75.  
  76.     return false;
  77.   }
  78.  
  79.  
  80. /*
  81.   方法四:
  82. */
  83.  
  84.   for (int i=0; i<s.length; i++)
  85.   {
  86.       Regex rx = new Regex("^[\u4e00-\u9fa5]$");
  87.  
  88.     if (rx.IsMatch(s[i]))
  89.           // 是
  90.       else
  91.           // 否
  92.   }
  93.  
  94.   // 正解!
  95.   // \u4e00-\u9fa5 漢字的範圍.
  96.   // ^[\u4e00-\u9fa5]$ 漢字的範圍的正則
  97.  
  98.  
  99.  
  100. /*
  101.   方法五:
  102. */
  103.  
  104.   unicodeencoding unicodeencoding = new unicodeencoding();
  105.   byte [] unicodebytearray = unicodeencoding.getbytes( inputstring );
  106.  
  107.   for( int i = 0; i < unicodebytearray.length; i++ )
  108.   {
  109.       i++;
  110.       // 如果是中文字符那麼高位不為0
  111.       if ( unicodebytearray[i] != 0 )
  112.       {
  113.       }
  114.  
  115.   }
  116.  
  117. /*
  118.   方法六:
  119. */
  120.  
  121.   /// <summary>
  122.   /// 給定一個字符串,判斷其是否只包含有漢字
  123.   /// </summary>
  124.   /// <param name="testStr"></param>
  125.   /// <returns></returns>
  126.  
  127.   public bool IsOnlyContainsChinese(string testStr)
  128.   {
  129.       char[] words = testStr.ToCharArray();
  130.  
  131.     foreach (char word in words)
  132.       {
  133.         if ( IsGBCode(word.ToString()) || IsGBKCode(word.ToString()) ) // it is a GB2312 or GBK chinese word
  134.         {
  135.               continue;
  136.           }
  137.           else
  138.           {
  139.               return false;
  140.           }
  141.       }
  142.  
  143.         return true;
  144.   }
  145.  
  146.   /// <summary>
  147.   /// 判斷一個word是否為GB2312編碼的漢字
  148.   /// </summary>
  149.   /// <param name="word"></param>
  150.   /// <returns></returns>
  151.  
  152.   private bool IsGBCode(string word)
  153.   {
  154.       byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
  155.  
  156.     if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code
  157.       {
  158.           return false;
  159.       }
  160.       else
  161.       {
  162.           byte byte1 = bytes[0];
  163.           byte byte2 = bytes[1];
  164.  
  165.         if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254) //判斷是否是GB2312
  166.         {
  167.             return true;
  168.         }
  169.         else
  170.         {
  171.             return false;
  172.         }
  173.     }
  174.   }
  175.  
  176.   /// <summary>
  177.   /// 判斷一個word是否為GBK編碼的漢字
  178.   /// </summary>
  179.   /// <param name="word"></param>
  180.   /// <returns></returns>
  181.  
  182.   private bool IsGBKCode(string word)
  183.   {
  184.     byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString());
  185.  
  186.     if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
  187.       {
  188.           return false;
  189.       }
  190.       else
  191.       {
  192.           byte byte1 = bytes[0];
  193.           byte byte2 = bytes[1];
  194.  
  195.           if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254) //判斷是否是GBK編碼
  196.           {
  197.               return true;
  198.           }
  199.           else
  200.           {
  201.               return false;
  202.           }
  203.       }
  204.   }
  205.  
  206.   /// <summary>
  207.   /// 判斷一個word是否為Big5編碼的漢字
  208.   /// </summary>
  209.   /// <param name="word"></param>
  210.   /// <returns></returns>
  211.  
  212.   private bool IsBig5Code(string word)
  213.   {
  214.       byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString());
  215.  
  216.     if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
  217.       {
  218.           return false;
  219.       }
  220.       else
  221.       {
  222.           byte byte1 = bytes[0];
  223.           byte byte2 = bytes[1];
  224.  
  225.             // 判斷是否是Big5編碼
  226.           if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254)) )
  227.           {
  228.               return true;
  229.           }
  230.           else
  231.           {
  232.               return false;
  233.           }
  234.       }
  235.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement