方法一
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);    //范围(0x4e000x9fff)转换成intchfromchend
        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++; 
//
如果是中文字符那么高位不为
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;
                }
            }
        }

C# 判断字符编码的六种方法的更多相关文章

  1. 用chardet判断字符编码的方法

    转自http://www.cnblogs.com/xiaowuyi/archive/2012/03/09/2387173.html 用chardet判断字符编码的方法   1.chardet下载与安装 ...

  2. python 判断字符编码

    一般情况下,需要加这个: import sys reload(sys) sys.setdefaultencoding('utf-8') 打开其他文件编码用codecs.open 读 下面的代码读取了文 ...

  3. XE Delphi 判断字符为中文的方法

    在uses中添加System.AnsiStrings /// Param ch--字符串/// Param cno--字符位置 function IsZHChar(const ch: AnsiStri ...

  4. PHP 判断字符的编码 并输出想要的编码格式字符 (转)

    /** * 判断字符编码  并输出想要的编码 * Enter description here ... * @param unknown_type $string * @param unknown_t ...

  5. Servlet字符编码过滤器,实现图书信息的添加功能,避免产生文字乱码现象的产生

    同样的代码,网上可以找到和我一模一样的代码和配置,比我的更加详细,但是我重新写一个博客的原因自是把错误的原因写出来,因为这就是个坑,我弄了一天,希望对你们有所帮助.只为初学者发现错误不知道怎么解决有所 ...

  6. Android中判断字符是否为中文、韩文、日文

    我们经常需要在程序中判断一个字符是否为CJK(Chinese.Japanese.Korean)语言的字符. 例如,在Contacts里面程序需要判断联系人姓名的所属语言. 今天为大家介绍一种NameS ...

  7. PHP爬虫(3)PHP DOM开源代码里的大坑和字符编码

    一.开源代码的问题 在PHP爬虫(2)中介绍了开源工程Sunra.PhpSimple.HtmlDomParser.在实际工作中发现一个问题,例如http://www.163.com的网页数据怎么也抓取 ...

  8. C#三种判断字符是否为汉字的方法

    判断一个字符是不是汉字通常有三种方法,第一种用 ASCII 码判断,第二种用汉字的 UNICODE 编码范围判 断,第三种用正则表达式判断,以下是具体方法. 1.用ASCII码判断 在 ASCII码表 ...

  9. (转)java判断string变量是否是数字的六种方法小结

    java判断string变量是否是数字的六种方法小结 (2012-10-17 17:00:17) 转载▼ 标签: it 分类: 转发 1.用JAVA自带的函数 public static boolea ...

随机推荐

  1. SpringSecurity csrf验证忽略某些请求

    前几天项目中遇到springSecurity问题,研究了大半天,掉进了csrf的坑,先认识一下csrf CSRF概念:CSRF跨站点请求伪造(Cross—Site Request Forgery),跟 ...

  2. 操作系统环境变量LANG和NLS_LANG的关系

    =Native Language Support本地语言支持 NLS ORACLE11g-ORA-12705: Cannot access NLS data files or invalid envi ...

  3. Shell中各种判断语法

    Shell判断 按照文件类型进行判断 -b 判断文件是否存在,并且是否为快设备文件(是块设备文件为真) -c 判断文件是否存在,并且是否为字符设备文件(是字符设备文件为真) -d 判断文件是否存在,并 ...

  4. Java基本数据类型装箱的127临界点

    package wrapper.demo; public class WrapperDemo { /** * @param args */ public static void main(String ...

  5. MySQL binlog导入失败

    一个同事问我,说他用innobackupex恢复数据后用mysqlbinlog导入增量数据时,发现数据没有导入进去并且也没有报错. mysqlbinlog /u01/mysql_py/database ...

  6. C#中的GetElementsByClassName方法

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24     public static class Spread     {    ...

  7. win7下PHP+MySQL+CoreSeek中文检索引擎配置

    1.Windows下的coreseek安装测试 (64位win7旗舰版) 官方参考:http://www.coreseek.cn/products-install/install_on_windows ...

  8. 转载 http://blog.csdn.net/dengta_snowwhite/article/details/6418384

    从SDCard保存的txt文件读取中文到android系统中会出现乱码问题,如何解决这个乱码问题,网上有不少解答方法,譬如说利用String temp1 =EncodingUtils.getStrin ...

  9. spring-service.xml 模板

    ssm模板 <?xml version="1.0" encoding="UTF-8"?>  <beans xmlns="http:/ ...

  10. 【转】卖萌的大牛你桑不起啊 ——记CVPR2011一篇极品文章

    来源:http://blog.renren.com/share/228707015/7197269922 作者 : 庞宇 CVPR2011正在如火如荼的进行中,在网上能看到的部分文章中,我终于找到一篇 ...