C#验证中文的方式有很多种,下面列举了其中几种可供参考,还有正则表达式的验证这里没有写,后面有机会再补上。
方法一:
private
bool isChina(string msg)
{
string temp;
for (int i = 0; i < msg.Length; i++)
{
temp = msg.Substring(i, 1);
byte[] ser = Encoding.GetEncoding("gb2312").GetBytes(temp);
if (ser.Length == 2)
{
returntrue;
}
}
returnfalse;
}
方法二:
private
bool CheckChina(string input)
{
int code = 0;
int chfrom = Convert.ToInt32("4e00", 16); //范围(0x4e00~0x9fff)转换成int(chfrom~chend)int chend = Convert.ToInt32("9fff", 16);
for (int i = 0; i < input.Length; i++)
{
code = Char.ConvertToUtf32(input, 1); //获得字符串input中指定索引index处字符unicode编码if (code >= chfrom && code <= chend)
{
returntrue; //当code在中文范围内返回true
}
}
returnfalse;
}
方法三:
public
bool IsChina1(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
{
return BoolValue = true;
}
}
return BoolValue;
}
原文:http://www.cnblogs.com/duanjt/p/5440530.html
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:254677821)!