判断是否是数字,字母,中文字符

1.isDigit/isLetter方法

JDK中自带有isDigit/isLetter方法,可以判断字符是否是数字/字母。

    public static void isNum() {
        String s = ".?abcd123;";
        for (int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            boolean letter = Character.isLetter(c);
            boolean digit = Character.isDigit(c);
            System.out.println(c + " is letter: " + letter + ", is digit: " + digit);
        }
    }

上面方法的输出为

. is letter: false, is digit: false
? is letter: false, is digit: false
a is letter: true, is digit: false
b is letter: true, is digit: false
c is letter: true, is digit: false
d is letter: true, is digit: false
1 is letter: false, is digit: true
2 is letter: false, is digit: true
3 is letter: false, is digit: true
; is letter: false, is digit: false

2.ascii码

0-9对应的ascii码为48-57,A-Z:65-90,a-z:97-122。
可以利用ascii码值来进行判断。

    public static void asciimethod() {
        String s = ".?abcd023;";
        boolean flag = false;
        for(int i=0; i<s.length(); i++) {
            char c = s.charAt(i);
            // 0-9:48-57,a-z:97-122,A-Z:65-90
            if (c >= 48 && c <= 57) flag = true;
            System.out.println(c + " is num: " + flag);
            flag = false;
        }
    }

3.正则匹配

正则匹配也是我们进行字符串处理常用的方法。

    public static void remethod() {
        String s1 = "1231";
        Pattern pattern = Pattern.compile("[0-9]*");
        boolean f1 = pattern.matcher(s1).matches();
        System.out.println("s1 is num: " + f1);
        String s2 = "12ab";
        boolean f2 = pattern.matcher(s2).matches();
        System.out.println("s2 is num: " + f2);
    }

4.判断中文字符使用正则

要判断一个字符是否为中文字符,可以使用正则匹配的方式。

    public static void checkCharCn() {
        String s1 = "你好";
        String regex = "[\u4e00-\u9fa5]";
        Pattern pattern = Pattern.compile(regex);
        for (int i=0; i<s1.length(); i++) {
            char c = s1.charAt(i);
            boolean flag = pattern.matcher(String.valueOf(c)).matches();
            System.out.println(c + " is CN char: " + flag);
        }
    }

其中,4e00-9fa5表示汉字的unicode编码范围。关于unicode编码与utf-8, utf-16的关系,可以查阅参考文献1。

5.判断中文字符UnicodeScript

从JDK1.7开始,还可以使用UnicodeScript来判断是否为中文字符。

    public static boolean isChinese(char c) {
        Character.UnicodeScript sc = Character.UnicodeScript.of(c);
        if (sc == Character.UnicodeScript.HAN) {
            return true;
        }

        return false;
    }

参考文献

1.https://www.cnblogs.com/kingcat/archive/2012/10/16/2726334.html

猜你喜欢

转载自blog.csdn.net/bitcarmanlee/article/details/131539669
今日推荐