C# 判斷中文字元的8種方法

來源:互聯網
上載者:User

方法一
在unicode 字串中,中文的範圍是在4E00..9FFF:CJK Unified Ideographs。

通過對字元的unicode編碼進行判斷來確定字元是否為中文。

protected bool   IsChineseLetter (string input,int index )
    
... {          int code = 0 ;
        
int chfrom = Convert.ToInt32( " 4e00 " , 16 );     // 範圍(0x4e00~0x9fff)轉換成int(chfrom~chend)
         int chend = Convert.ToInt32( " 9fff " , 16 );
        
if (input != "" )
        
... {
             code
= Char.ConvertToUtf32(input, index );     //獲得 字串input中指定索引index處字元unicode編碼
            
           
if (code >= chfrom && code <= chend)     
            
... {
                 return true;
      // 當code在中文範圍內返回true

             }

            
else
            
... {
                  return false ;   
// 當code不在中文範圍內返回false
             }

         }

          return false;
}

方法二:
public bool IsChina(string CString)
          {
              bool BoolValue = false;
              for (int i = 0; i < CString.Length; i++)
              {
                  if (Convert.ToInt32(Convert.ToChar(CString.Substring(i, 1))) < Convert.ToInt32(Convert.ToChar(128)))
                  {
                      BoolValue = false;
                  }
                  else
                  {
                      return BoolValue = true;
                  }
              }
              return BoolValue;
          }

方法三:

        /// <summary>
        /// 判斷句子中是否含有中文
        /// </summary>
        /// <param >字串</param>
        public bool WordsIScn(string words)
        {
            string TmmP;

            for (int i = 0; i < words.Length; i++)
            {
                TmmP = words.Substring(i, 1);

                byte[] sarr = System.Text.Encoding.GetEncoding("gb2312").GetBytes(TmmP);

                if (sarr.Length == 2)
                {
                    return true;
                }
            }
            return false;
        }

方法四:
for (int i=0; i<s.length; i++)
{
Regex rx = new Regex("^[\u4e00-\u9fa5]$");
if (rx.IsMatch(s[i]))
// 是
else
// 否
}
正解!
\u4e00-\u9fa5 漢字的範圍。
^[\u4e00-\u9fa5]$ 漢字的範圍的正則

方法五
unicodeencoding unicodeencoding = new unicodeencoding();
byte [] unicodebytearray = unicodeencoding.getbytes( inputstring );
for( int i = 0; i < unicodebytearray.length; i++ )
{
i++;
//如果是中文字元那麼高位不為0
if ( unicodebytearray[i] != 0 )
{
}
……  

方法六
    /// <summary>
        /// 給定一個字串,判斷其是否只包含有漢字
        /// </summary>
        /// <param name="testStr"></param>
        /// <returns></returns>
        public bool IsOnlyContainsChinese(string testStr)
        {
            char[] words = testStr.ToCharArray();
            foreach (char word in words)
            {
                if ( IsGBCode(word.ToString()) || IsGBKCode(word.ToString()) ) // it is a GB2312 or GBK chinese word
                {
                    continue;
                }
                else
                {
                    return false;
                }
            }
            return true;
        }

        /// <summary>
        /// 判斷一個word是否為GB2312編碼的漢字
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private bool IsGBCode(string word)
        {
            byte[] bytes = Encoding.GetEncoding("GB2312").GetBytes(word);
            if (bytes.Length <= 1) // if there is only one byte, it is ASCII code or other code
            {
                return false;
            }
            else
            {
                byte byte1 = bytes[0];
                byte byte2 = bytes[1];
                if (byte1 >= 176 && byte1 <= 247 && byte2 >= 160 && byte2 <= 254)    //判斷是否是GB2312
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 判斷一個word是否為GBK編碼的漢字
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private bool IsGBKCode(string word)
        {
            byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(word.ToString());
            if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
            {
                return false;
            }
            else
            {
                byte byte1 = bytes[0];
                byte byte2 = bytes[1];
                if ( byte1 >= 129 && byte1 <= 254 && byte2 >= 64 && byte2 <= 254)     //判斷是否是GBK編碼
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        /// <summary>
        /// 判斷一個word是否為Big5編碼的漢字
        /// </summary>
        /// <param name="word"></param>
        /// <returns></returns>
        private bool IsBig5Code(string word)
        {
            byte[] bytes = Encoding.GetEncoding("Big5").GetBytes(word.ToString());
            if (bytes.Length <= 1) // if there is only one byte, it is ASCII code
            {
                return false;
            }
            else
            {
                byte byte1 = bytes[0];
                byte byte2 = bytes[1];
                if ( (byte1 >= 129 && byte1 <= 254) && ((byte2 >= 64 && byte2 <= 126) || (byte2 >= 161 && byte2 <= 254)) )     //判斷是否是Big5編碼
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

上面是前輩們留下的,我想偷點懶。

方法七

引用從微軟亞洲語言開發包裡的Simplified Chinese Pin-Yin Conversion Library 裡提取ChnCharInfo.dll後
using Microsoft.International.Converters.PinYinConverter;
private bool IsHanzi(Char c)
{
    return ChineseChar.IsValidChar(c);
}

方法八

引用從微軟亞洲語言開發包裡的Simplified Chinese Pin-Yin Conversion Library 裡提取ChnCharInfo.dll後
using Microsoft.International.Converters.PinYinConverter;
private bool IsHanzi(Char c)
{
    try
    {
        ChineseChar CChar1 = new ChineseChar(c);
        return ture;
    }
    catch
    {
        return false;
    }
}

附:有了這個開發,我這樣的菜鳥也可以很容易就玩轉漢字遊戲.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.