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對數(shù)字進行計數(shù)
else if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
n2++;//n2對字母進行計數(shù)
else if (ch == ' ')
n3++;//n3對空格進行計數(shù)
else if (ch >= 0x4e00 && ch <= 0x9fa5)
{
n4++;//n4對漢字進行計數(shù)
}
else
{
n5++;//n5對特殊字符進行計數(shù)
}
}
判斷是否含有空格
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("該字符為漢字..");
}