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

    很多人的博客都写了,如:http://www.cnblogs.com/fxllx82/archive/2008/04/15/1154534.html

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

    我这里粘帖出来只是因为比较常用,方便自己查找,感谢不尽,如果有意见,可通知我撤销,谢谢!

    方法一:
    原文:http://www.soave.cn/blog/article.asp?id=232
    在unicode 字符串中,中文的范围是在4E00..9FFF:CJK Unified Ideographs。
    通过对字符的unicode编码进行判断来确定字符是否为中文。

    Code

    方法二:

    Code

    方法三:

    **///<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;
                    }

                }

            }

  • 相关阅读:
    完全开源Android网络框架 — 基于JAVA原生的HTTP框架
    博客园—Android客户端
    撸一个Android高性能日历控件,高仿魅族
    Android开发登陆博客园的正确方式
    基于pthread的线程池实现
    重复造轮子系列——基于FastReport设计打印模板实现桌面端WPF套打和商超POS高度自适应小票打印
    重复造轮子系列——基于Ocelot实现类似支付宝接口模式的网关
    零基础ASP.NET Core WebAPI团队协作开发
    零基础ASP.NET Core MVC插件式开发
    jquery对下拉框的操作
  • 原文地址:https://www.cnblogs.com/jackhuclan/p/1254443.html
Copyright © 2011-2022 走看看