方法一
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. javascript 简单工厂模式

    var Bicycle = new Interface("Bicycle",["assemble","wash","ride&qu ...

  2. asp.net将ppt文档转换成pdf

    一.添加引用 using Microsoft.Office.Core;using Microsoft.Office.Interop.PowerPoint; 二.转换方法   C# 代码   复制 // ...

  3. java关于图片处理修改图片大小

    最近做了一个关于图片浏览的内容.因为图片都是一些证件的资料的扫描件所以比较大,对系统的影响也是非常之大的,有很大可能直接把系统干死.那么我是这么处理的,给大家分享一下.如果大家有好的方案的话一定要早点 ...

  4. shape-outside 矩形之外的另一种思路

    http://docs.webplatform.org/wiki/css/properties/shape-outside

  5. hibernate的一对多和多对一关联

    一对一的关联就不写了,一般项目也用不到,如果可以一对一就直接合成一个表了,也不会出现一对一的关系. 本文主要研究一对多的关系. 1.一对多的关系研究: (1)RDB中关系表达:  多的一方创建外键指向 ...

  6. 分模块开发创建service子模块——(八)

    1.右击父工程新建maven子模块

  7. imperva 更改agent的注册密码

    imperva agent 在注册到网关的时候显示账号密码错误 如下图 这是一个数据库审计的设备由于当初最开始实施的时候并不是我安装的,所以账号密码我也不清楚,客户留的账号密码也不确定.因此导致账号密 ...

  8. df -h执行卡住不动问题解决【转】

    昨天生产环境报日志写不进去了,因此 登陆线上环境后,习惯用df -h命令查看空间使用情况,结果发现该命令执行半天也没有返回. 因此使用mount命令查看该机器上的目录: [conversant@swi ...

  9. SpringBoot修改默认端口号,session超时时间

    有时候我们可能需要启动不止一个SpringBoot,而SpringBoot默认的端口号是8080,所以这时候我们就需要修改SpringBoot的默认端口了.修改SpringBoot的默认端口有两种方式 ...

  10. day16作业

    一.填空题 1.Integer Character 2.String s = "123";Integer i = new Integer(s);System.out.println ...