现在的位置: 首页 > 综合 > 正文

字符串中有汉字的处理方法集合

2011年11月02日 ⁄ 综合 ⁄ 共 7576字 ⁄ 字号 评论关闭
1、
str="aaa是";  
  re=/[\u4e00-\u9fa5]/g  
  if(re.test(str))  
  {  
          alert("有汉字")  
  }

2、
简单汉字判断  
  private   static   int   isCharacter(String   word){  
                      byte[]   str_byte   =   null;  
                      str_byte   =   word.substring(0,   1).getBytes();  
                      if(str_byte.length==2){  
                              return   1;//是汉字  
                      }else{  
                              return   0;//不是汉字  
                      }  
  }  

3、
<%  
  Public   Function   IsAllGB(s)  
  Dim   p   :   Set   p=   new   RegExp  
  p.Global=True  
  p.IgnoreCase=True  
  p.MultiLine=True  
  p.Pattern="[\u4E00-\u9FA5\uFE30-\uFFA0]"  
  Dim   q  
  q=p.Replace(s,"**")  
  Set   p=Nothing  
  If   Len(q)<>Len(s)*2   Then  
  IsAllGB=False  
  Else  
  IsAllGB=True  
  End   If  
  End   Function  
  Response.Write   IsAllGB("s你好")  
  %>   

4、
<%strs="要比较的字符"  
  if   strlen(strs)<>len(strs)   then  
  response.write   "中文字符"  
  else  
  response.write   "英文字符"  
  end   if  
        '**************************************************  
        '函数名:strLen  
        '作     用:求字符串长度。汉字算两个字符,英文算一个字符。  
        '参     数:str     ----要求长度的字符串  
        '返回值:字符串长度  
        '**************************************************  
        Public   Function   StrLen(str)  
  If   IsNull(str)   Or   Str   =   ""   Then  
  strLen   =   0  
  Exit   Function  
  End   If  
  Dim   WINNT_CHINESE  
  WINNT_CHINESE=(Len("例子")=2)  
  If   WINNT_CHINESE   Then  
  l=Len(str)  
  t=l  
  For   i=1   To   l  
  c=Asc(Mid(str,i,1))  
  If   c<0   Then   c=c+65536  
  If   c>255   Then   t=t+1  
  Next  
  strLen=t  
  Else    
  strLen=Len(str)  
  End   If  
            End   Function  
  %>

5、
if   IsGB(str)   then  
        return   true;  
  else  
        return   false;  
  end   if

6、
for   i=1   to   len(str)  
  char=(mid(str,i,1))  
  tmp=65536+asc(char)  
  if   tmp>=45217   and   tmp<=62289   then  
      msgbox   "汉字"  
  end   if  
  next   

7、
取该字符的ASCII码值    
  如果在0x41~0x7a之间,为英文字符(包括标点符号)    
   
  如果是GB2312-80编码    
  编码范围是高位0xa1-0xfe,低位是   0xa1-0xfe    
  汉字范围为   0xb0a1   和   0xf7fe

8、
CString   ss="hello   你好!";  
  BOOL   bCh=FALSE;//是否有中文  
  for(int   i=0;1<(int)strlen(ss);i++)  
  {  
          if((BYTE)ss[i]>=0xa1)  
                  bCH=TRUE;  
  }

9、
using   System;  
  using   System.Text.RegularExpressions;  
  namespace   test  
  {  
  ///   <summary>  
  ///   Class10   的摘要说明。  
  ///   </summary>  
  public   class   Class10  
  {  
  public   Class10()  
  {  
  //  
  //   TODO:   在此处添加构造函数逻辑  
  //  
  }  
  public   static   string   Check(string   pendingString)  
  {  
  if   (Regex.IsMatch(pendingString,   @"[\u4e00-\u9fa5]+"))  
  {  
  return   "有汉字";  
  }  
  else  
  {  
  return   "无汉字";  
  }  
  }  
  static   void   Main()  
  {  
  string   s   =   "《我爱祖国》";  
  Console.WriteLine(Check(s));  
   
  s   =   @"abcdefodfl980883247)(*(*(^&*^&$%^$%#$#@%&*&(&*)"":::\http:   ";  
  Console.WriteLine(Check(s));  
   
  Console.ReadLine();  
  }  
   
   
  }  
  }   

10、
<script   language=javascript>  
  str="aaa是";  
  if(/[\u4e00-\u9fa5]/.test(str))  
  {  
          alert("有汉字")  
  }  
  </script>

11、
String.prototype.existChinese   =   function()  
  {  
  //[\u4E00-\u9FA5]為漢字﹐[\uFE30-\uFFA0]為全角符號  
  return   /[\u4e00-\u9fa5]/.test(this);  
  }

12、
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  
   
                                        {  
   
                                                BoolValue   =   true;  
   
                                        }  
   
                              }  
   
                              return   BoolValue;  
   
                    }

13、
char.GetUnicodeCategory(c)   ==   UnicodeCategory.OtherLetter  
  GetUnicodeCategory能判断全角符号,字符,数字等等,最近这种问题真多,都喜欢用2个字节来判断,却没人看我的回答。  
  这问题一行代码就能解决。  

14、
System.Text.RegularExpressions.Regex   regex   =   new   System.Text.RegularExpressions.Regex(   "[\u4e00-\u9fa5]");  
  string   replacedString   =   regex.Replace(str,"");//如果存在指定编码的字符串则过滤掉  
  用正则表达式来作过滤,我这个代码是过滤中文,你照着改改就可以了. 

15、提取汉字

private void button1_Click(object sender, System.EventArgs e)
{
 if(txtIN.Text != "")
 {
  int i = 0;
  string strIN = txtIN.Text;
  string temp;
  byte[] array = new byte[2];
  txtOUT.Text = "";
  for(i = 0;i < strIN.Length;i++)
  {
   temp = strIN.Substring(i,1);
   array = Encoding.Default.GetBytes(temp);
   if(array.Length != 1)
   {
    txtOUT.Text = txtOUT.Text + temp;
   }
  }
 }
}
按钮点击的时候将txtIN中字符串中的汉字显示到txtOUT中。

 

foreach(char c in str)
{
    if(char.GetUnicodeCategory(c) == UnicodeCategory.OtherLetter){
        Console.Write(c.ToString());
    }
}
中文是OtherLetter,如果string里还有其它语言字符,只能获取u值判断了

  16、
//计算字符串的实际长度,一个汉字或全角字符算两个Byte  
  public   static   int   GetStringLength(string   param)  
  {  
  ASCIIEncoding   n   =   new   ASCIIEncoding();  
  byte[]   b   =   n.GetBytes(param);  
  int   l   =   0;     //   l   为字符串之实际长度  
  for   (int   i=0;i   <=   b.Length-1;i++)  
  {  
  if   (b[i]   ==63)     //判断是否为汉字或全脚符号  
  {  
  l++;  
  }  
  l++;  
  }  
  return   l;  
  }  
   
  //strLen:字符串的实际长度,len:要取的字符串长度,其中一个汉字或全角字符算两个Byte  
  public   static   string   GetSubString(string   param,int   len,ref   int   strLen)  
  {  
  string   returnStr="";  
  bool   isSub=false;  
  ASCIIEncoding   n   =   new   ASCIIEncoding();  
  byte[]   b   =   n.GetBytes(param);  
  int   l   =   0;     //   l   为字符串之实际长度  
  for   (int   i=0;i   <=   b.Length-1;i++)  
  {  
  if   (b[i]   ==63)     //判断是否为汉字或全脚符号  
  {  
  l++;  
  }  
  l++;  
  if((isSub==false)&&((l==len)||((l+1)==len)))  
  {  
  returnStr=param.Substring(0,i+1);  
  isSub=true;  
  }  
  }  
   
  strLen=l;  
  if(returnStr=="")  
  {  
  returnStr=param;  
  }  
  return   returnStr;  
  }  

17、

private void button1_Click(object sender, System.EventArgs e)
{
 if(txtIN.Text != "")
 {
  int i = 0;
  string strIN = txtIN.Text;
  string temp;
  byte[] array = new byte[2];
  txtOUT.Text = "";
  for(i = 0;i < strIN.Length;i++)
  {
   temp = strIN.Substring(i,1);
   array = Encoding.Default.GetBytes(temp);
   if(array.Length != 1)
   {
    txtOUT.Text = txtOUT.Text + temp;
   }
  }
 }
}
按钮点击的时候将txtIN中字符串中的汉字显示到txtOUT中。

foreach(char c in str)
{
    if(char.GetUnicodeCategory(c) == UnicodeCategory.OtherLetter){
        Console.Write(c.ToString());
    }
}
中文是OtherLetter,如果string里还有其它语言字符,只能获取u值判断了

18、正则表达式(汉字,字母,数字 混合验证)
首位汉字,
2-4位为字母,
第5位汉字,
6-14位为数字,
最后一位为汉字

^[\u4e00-\u9fa5\uf900-\ufa2d][a-zA-Z]{3}[\u4e00-\u9fa5\uf900-\ufa2d]\d{9}[\u4e00-\u9fa5\uf900-\ufa2d]$

^[\u4e00-\u9fa5][a-zA-Z]{3}[\u4e00-\u9fa5]\d{9}[\u4e00-\u9fa5]$

判断一个字符串内都是数字(转载,C#)

 
方法1:
public static bool IsNum(string str)
{
for(int i=0;i<str.Length;i++)
{
if(str[i]<='0' str[i]>='9')
return false;
}
return true;
}

方法2:
最快就是用int.parse();
出错就说明不全是数字

方法3:
public static bool IsNum(string str)
{
for(int i=0;i<str.Length;i++)
{

if(Char.IsNumber(str, i)==false)
return false;
break;

}
return true;
}

方法4:
/// <summary>
/// 检察是否都是数字
/// </summary>
/// <param name="str">要检查的字串</param>
/// <returns>bool</returns>
public static bool IsNumeric(string str)
{
if ( str == null )
{
return false;
}
else
{
Regex reg = new Regex("^(-?\\d+)(\\.\\d+)?$");
return reg.IsMatch(str);
}
}

方法5:
private Regex RegIntegral=new Regex("^\\d+$");//整数0+正整数

/// <summary>
/// 是否整数0+正整数
/// </summary>
/// <param name="inputData">输入字符串</param>
/// <returns></returns>
public bool IsIntegral(string inputData)
{
Match m = RegIntegral.Match(inputData);
return m.Success;
}

public static bool IsNum(string str)
{
foreach( char c in str )
{
if( c<='0' || c>='9')
return false;
}
return true;
}

方法6:
public static bool IsNumber(char c)
{
if (!char.IsLatin1(c))
{
return char.CheckNumber(CharUnicodeInfo.GetUnicodeCategory(c));
}
if (!char.IsAscii(c))
{
return char.CheckNumber(char.GetLatin1UnicodeCategory(c));
}
if (c >= '0')
{
return (c <= '9');
}
return false;
}

我测试所用的源代码:
private void button9_Click(object sender, EventArgs e)
{
int count = 1000000;
bool bl = true;
string str = textBox1.Text; //如果你要测试,相应改一下
DateTime time1 = DateTime.Now;
for (int i = 0; i < count; i++)
{
bl = IsNum1(str);
}
DateTime time2 = DateTime.Now;
for (int i = 0; i < count; i++)
{
bl = IsNum2(str);
}
DateTime time3 = DateTime.Now;
for (int i = 0; i < count; i++)
{
bl = IsNum3(str);
}
DateTime time4 = DateTime.Now;

TimeSpan span1 = time2 - time1;
TimeSpan span2 = time3 - time2;
TimeSpan span3 = time4 - time3;
MessageBox.Show(span1.TotalMilliseconds + "\r\n"
+ span2.TotalMilliseconds + "\r\n"
+ span3.TotalMilliseconds);
}
Regex reg = new Regex(@"^\d$", RegexOptions.Compiled);
bool IsNum1(string str)
{
return reg.IsMatch(str);
}
bool IsNum2(string str)
{
foreach (char c in str)
{
if (!Char.IsNumber(c))
{
return false;
}
}
return true;
}
bool IsNum3(string str)
{
foreach (char c in str)
{
if (c <='0' || c>='9')
{
return false;
}
}
return true;
}

抱歉!评论已关闭.