字符竄中提取中文的方法

string str="求11從字345符。串asd中提:取中“文的;方,法;",s="";

for(int  i=0;i <str.Length;++i) 
{
  char j=(char)str[i];
  if  (IsChineseChar(j)!=false) 
  { 
  s+=j;
  }
}

bool  IsChineseChar(char ch) 

  byte[]  bytes  =  System.Text.Encoding.GetEncoding("gb2312").GetBytes(ch.ToString()); 
   
  if  (bytes.Length  !=  2) 
  { 
  return  false; 
  } 
   
  int  zone  =  bytes[0]; 
  int  num    =  bytes[1]; 
   
  return  (zone  >=  0xB0  &&  zone  <=0xF7)  &&  (num  >0xA0  &&  num <0xFD); 
}

 

2、//使用正則,記得導入using System.Text.RegularExpressions;
string str= "==從==字符串==中==提取==中文==的==方法!";
MatchCollection mc
= Regex.Matches(str,"([/u4e00-/u9fa5]+)");
foreach (Match min mc)
MessageBox.Show(m.Groups[
1].Value);

3、用正則,替換掉所有非中文內容

4、用正則過濾出MatchCollection結果集,然後foreach循環,用StringBuilder.Appent()連接起來就OK了

5、string   str="求11從字345符。串asd中提:取中“文的;方,法;";
str
=Regex.Replace(str,"([^/u4e00-/u9fa5])","");
6、Sub Test3()
    Dim S, i, t
    Text = "asdfghjkl教你提取字符$%^&*()123~`45串中中文的方法qwertyuio!!!"
    S = ""
    For i = 1 To Len(Text)
        '漢字小於ASC值0﹐否則在0-127之間
        If Asc(Mid(Text, i, 1)) < 0 Then
            S = S & Mid(Text, i, 1)
        End If
    Next i
    Debug.Print S
End Sub

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章