很久以前在項目中用到的一個小知識點,感覺用處還是有的,記錄保留一下。
1)用JAVA自帶的函數(shù)
1
public static boolean isNumeric(String str) {
2
for(int i = str.length(); --i >= 0;) {
3
if (!Character.isDigit(str.charAt(i))) {
4
return false;
5
}
6
}
7
8
return true;
9
}

2

3

4

5

6

7

8

9

2)用正則表達式
1
public static boolean isNumeric(String str) {
2
Pattern pattern = Pattern.compile("[0-9]*");
3
4
return pattern.matcher(str).matches();
5
}

2

3

4

5

3)用ascii碼
1
public static boolean isNumeric(String str) {
2
for(int i = str.length(); --i >= 0;) {
3
int chr = str.charAt(i);
4
if(chr < 48 || chr > 57) {
5
return false;
6
}
7
}
8
9
return true;
10
}

2

3

4

5

6

7

8

9

10

以上總結希望對能用到的人提供點相關資料。