C# 判断中文字符(字符串)-整理

简介:   http://www.cnblogs.com/xiexiaokui/archive/2007/11/07/952655.html 方法一http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733169.aspx在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs。

 

http://www.cnblogs.com/xiexiaokui/archive/2007/11/07/952655.html

方法一
http://blog.csdn.net/qiujiahao/archive/2007/08/09/1733169.aspx
在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;
 }

方法二:
http://hi.baidu.com/yhfd/blog/item/3222e1fca22cfb80b901a027.html
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>
        /// 判断句子中是否含有中文     宁夏大学 张冬 zd4004.blog.163.com
        /// </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;
                }
            }
        }



其实原理都一样。
具体的实现,还有几种,就不列举了
相关文章
|
2月前
|
C#
C#的小例子和字符串(一)
C#的小例子和字符串(一)
127 0
|
2月前
|
C# 开发者
C# 10.0引入常量插值字符串:编译时确定性的新篇章
【1月更文挑战第22天】在C# 10.0中,微软为开发者带来了一项引人注目的新特性——常量插值字符串。这一功能允许在编译时处理和计算字符串插值表达式,从而得到可以在编译时确定的常量字符串。本文将深入探讨C# 10.0中常量插值字符串的概念、工作原理、使用场景及其对现有字符串处理方式的改进,旨在帮助读者更好地理解和应用这一强大的新特性。
|
2月前
|
C#
C#有关字符串的分割,替换,截取
C#有关字符串的分割,替换,截取
|
2月前
|
编译器 C# 开发者
C# 10.0中插值字符串的改进:灵活性与性能的双重提升
【1月更文挑战第19天】C# 10.0带来了对插值字符串的显著改进,进一步增强了这一功能的灵活性和性能。插值字符串是C#中处理字符串格式化的一种强大方式,它允许开发者直接在字符串中嵌入变量和表达式。在C# 10.0中,插值字符串不仅获得了语法上的简化,还通过新的编译时优化提高了运行时性能。本文将详细探讨C# 10.0中插值字符串的改进内容,以及这些改进如何为开发者带来更加高效和便捷的编程体验。
|
10天前
|
C#
技术经验分享:c#拆分字符串英文和数字(包括国外所以文字)
技术经验分享:c#拆分字符串英文和数字(包括国外所以文字)
技术经验分享:c#拆分字符串英文和数字(包括国外所以文字)
|
8天前
|
C#
C# 中的字符与字符串
C# 中的字符与字符串
7 0
|
2月前
|
C#
C#字符串
C#字符串
21 0
|
2月前
|
C#
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
101 1
|
2月前
|
JSON C# 开发者
C# 11.0引入自然字符串字面量:简化字符串处理的新时代
【1月更文挑战第26天】C# 11.0带来了一个令人兴奋的新特性:自然字符串字面量。这一特性旨在简化字符串的创建和处理,使开发者能够更直观地编写涉及多行字符串、转义字符和插值表达式的代码。自然字符串字面量不仅提高了代码的可读性,还减少了因转义字符引起的错误。本文将深入探讨C# 11.0中自然字符串字面量的概念、使用方法和其对现有字符串处理方式的改进。
|
2月前
|
存储 C# 索引
C# 字符串操作指南:长度、连接、插值、特殊字符和实用方法
字符串用于存储文本。一个字符串变量包含由双引号括起的字符集合
75 2