首页 > Windows开发 > 详细

C#验证中文

时间:2016-04-27 22:19:03      阅读:472      评论:0      收藏:0      [点我收藏+]

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

 

 

C#验证中文

原文:http://www.cnblogs.com/duanjt/p/5440530.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!