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

 

 

相关文章:

  • 2022-01-17
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-01-19
猜你喜欢
  • 2021-11-24
  • 2021-09-26
  • 2021-07-11
  • 2022-12-23
  • 2022-12-23
  • 2022-01-28
相关资源
相似解决方案