如何判断一个字符串的第一字为汉字或字母或数字

解决方案 »

  1.   

    问了3遍啊,先取字符串的第一个字符,然后数字和字母很好判断啊,汉字的话,getBytes的长度为2应该就是汉字了吧,可以满足你的要求了,可能不准确
      

  2.   

    先获取字符串的第一个字符用charAt();
      

  3.   


    public static String judgeFirstChar(String str) {
            char c = str.charAt(0);
            if (c >= '0' && c <= '9') {
                return "intChar";  //数字
            }
            
            if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')){
                return "charChar";  //字符
            }
            
            String regx = "([\u4E00-\u9FA5]{1,})";
            if (Pattern.matches(regx, c + "")) {
                return "chineseChar"; //汉字
            }
            
            return null;  //其他
        }仅供参考