zoukankan      html  css  js  c++  java
  • 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;
    }

     

  • 相关阅读:
    Java中的泛型
    Java中List、Collections实现梭哈游戏
    Java中HashMap案例
    Java中ArrayDeque,栈与队列
    Java中List的使用
    学会使用JDK API
    Java中的数学运算BigDecimal
    在清华听演讲语录
    Java面向对象深度
    类变量、实参、形参、方法参数、代码块参数
  • 原文地址:https://www.cnblogs.com/duanjt/p/5440530.html
Copyright © 2011-2022 走看看