zoukankan      html  css  js  c++  java
  • C# 判断中文字符(字符串)整理

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

    Unicode:32到128 非中文及可打印字符

    方法一
    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)

              
    不可打印字符:

    \cx

    匹配 x 指示的控制字符。例如,\cM 匹配 Control-M 或回车符。x 的值必须在 A-Z 或 a-z 之间。如果不是这样,则假定 c 就是“c”字符本身。

    \f

    换页符匹配。等效于 \x0c 和 \cL。

    \n

    换行符匹配。等效于 \x0a 和 \cJ。

    \r

    匹配一个回车符。等效于 \x0d 和 \cM。

    \s

    匹配任何空白字符,包括空格、制表符、换页符等。与 [\f\n\r\t\v] 等效。

    \S

    匹配任何非空白字符。与 [^ \f\n\r\t\v] 等效。

    \t

    制表符匹配。与 \x09 和 \cI 等效。

    \v

    垂直制表符匹配。与 \x0b 和 \cK 等效。

  • 相关阅读:
    开发人员维护测试数据库
    mybatis 自动生成 dao层 entity和xml时遇到的问题
    产品上线过程中犯下的错误
    leetcode: invert binary tree
    重构函数基本原则(持续更新)
    通过Mybatis获取mysql表中重复记录的方法
    Vmware 部分所学到的功能简写
    Vmware 主机锁定模式
    光纤卡网卡的区别以及HBA的常规定义-----引自百度百科
    存储基本概念(lun,volume,HBA,DAS,NAS,SAN,iSCSI,IPSAN)
  • 原文地址:https://www.cnblogs.com/blackbean/p/1986997.html
Copyright © 2011-2022 走看看