C#字符串格式判斷

string strTmp = "se 5 %# @e";

使用正則表達式

Regex r = new Regex(@"[\u4e00-\u9fa5]+");

Match mc = r.Match(strTmp);

if(mc.Length!=0)

{

Console.WriteLine("strTmp含有漢字..");

}

一般方法

int n1=0,n2=0,n3=0,n4=0,n5=0;

foreach (char ch in strTmp)

{

if (ch >= '0' && ch <= '9')

n1++;//n1對數字進行計數

else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')

n2++;//n2對字母進行計數

else if (ch == ' ')

n3++;//n3對空格進行計數

else if (ch >= 0x4e00 && ch <= 0x9fa5)

{

n4++;//n4對漢字進行計數

}

else

{

n5++;//n5對特殊字符進行計數

}

}

判斷是否含有空格

int pos=strTmp.IndexOf(' ');

if (pos == -1)

{

Console.WriteLine("不含有空格");

}

else

{

Console.WriteLine("含空格");

}

判斷一個字符是否爲漢字

string strTmp = "";

byte[] tmp = System.Text.Encoding.Default.GetBytes(strTmp);

if (tmp.Length > 1)

{

Console.WriteLine("該字符爲漢字..");

}

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