C#验证中文

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)
        {
            return true;
        }
    }

    return false;
}

 方法二: 

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)
        {
            return true;     //当code在中文范围内返回true
        }
    }
    return false;
}

 方法三: 

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;
}

 

原文地址:https://www.cnblogs.com/duanjt/p/5440530.html