1.isDigit/isLetter method
JDK has its own isDigit/isLetter method, which can determine whether a character is a number/letter.
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);
}
}
The output of the above method is
. 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 code
The ascii codes corresponding to 0-9 are 48-57, AZ:65-90, az:97-122.
You can use the ascii code value to judge.
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. Regular match
Regular matching is also a common method for us to process strings.
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. Determine the use of regular Chinese characters
To determine whether a character is a Chinese character, you can use regular matching.
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);
}
}
Among them, 4e00-9fa5 represents the unicode encoding range of Chinese characters. For the relationship between unicode encoding and utf-8, utf-16, you can refer to reference 1.
5. Judging Chinese characters UnicodeScript
Starting from JDK1.7, UnicodeScript can also be used to determine whether it is a Chinese character.
public static boolean isChinese(char c) {
Character.UnicodeScript sc = Character.UnicodeScript.of(c);
if (sc == Character.UnicodeScript.HAN) {
return true;
}
return false;
}
references
1.https://www.cnblogs.com/kingcat/archive/2012/10/16/2726334.html