Java中判斷字串是否為數字的幾種方法
阿新 • • 發佈:2019-01-23
原文地址:http://blog.csdn.net/u013066244/article/details/53197756
用JAVA自帶的函式
public static boolean isNumericZidai(String str) {
for (int i = 0; i < str.length(); i++) {
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))) {
return false ;
}
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
其中Character.isDigit
方法:確定或判斷指定字元是否是一個數字。
測試方法:
public static void main(String[] args) {
double aa = -19162431.1254;
String a = "-19162431.1254";
String b = "-19162431a1254";
String c = "中文";
System.out.println(isNumericzidai(Double.toString(aa)));
System.out.println(isNumericzidai(a));
System.out.println(isNumericzidai(b));
System.out.println(isNumericzidai(c));
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
結果顯示:
false
false
false
false
- 1
- 2
- 3
- 4
這種方法顯然不能判斷 負數。
用正則表示式
public static boolean isNumericzidai(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+.?[0-9]+");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
網上給出的最好的方法,可惜還是錯誤;首先正則表示式-?[0-9]+.?[0-9]+
這裡就錯誤
網上說:可匹配所有數字。
比如:
double aa = -19162431.1254;
String a = "-19162431.1254";
String b = "-19162431a1254";
String c = "中文";
System.out.println(isNumericzidai(Double.toString(aa)));
System.out.println(isNumericzidai(a));
System.out.println(isNumericzidai(b));
System.out.println(isNumericzidai(c));
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
結果
false
true
true
false
- 1
- 2
- 3
- 4
正確的正則表示式是:-?[0-9]+\\.?[0-9]*
,點號.
,是匹配任意字元,需要進行轉義。
注意:感謝網友留言提示了一個錯誤,之前我的正則表示式是這麼寫的: -?[0-9]+\\.?[0-9]+
,這種正則表示式在匹配0-9的正負數時,會出錯,這是因為該表示式最後是+
,表示的是匹配前一個字元1次或無限次。
也就是說在匹配一位的時候,會出現錯誤。所以改為*
,表示匹配前一位字元0次或無限次
System.out.println(isNumeric("9"));
- 1
結果為
true
- 1
於是我們改成:
public static boolean isNumericzidai(String str) {
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
Matcher isNum = pattern.matcher(str);
if (!isNum.matches()) {
return false;
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
執行上面程式碼結果:
false
true
false
false
- 1
- 2
- 3
- 4
可是為什麼double aa = -19162431.1254;
沒有匹配為true呢?
原來當數字位數很長時,系統會自動轉為科學計數法。所以aa=-1.91624311254E7
.
所以我們需要使用java
專門用於商業精度計數的類 BigDecimal
.
我們需要new BigDecimal(str)
,但是呢,如果str為中文,會報異常。所以我用try
catch
捕捉異常。
正確的通用程式碼(傳入包含中文、負數、位數很長的數字的字串也能正常匹配):
/**
* 匹配是否包含數字
* @param str 可能為中文,也可能是-19162431.1254,不使用BigDecimal的話,變成-1.91624311254E7
* @return
* @author yutao
* @date 2016年11月14日下午7:41:22
*/
public static boolean isNumeric(String str) {
// 該正則表示式可以匹配所有的數字 包括負數
Pattern pattern = Pattern.compile("-?[0-9]+\\.?[0-9]*");
String bigStr;
try {
bigStr = new BigDecimal(str).toString();
} catch (Exception e) {
return false;//異常 說明包含非數字。
}
Matcher isNum = pattern.matcher(bigStr); // matcher是全匹配
if (!isNum.matches()) {
return false;
}
return true;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
使用org.apache.commons.lang
public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
null will return false. An empty String ("") will return true.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = true
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false
Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
第三種我沒有測試,網上說,也不能判斷負數。
又因為使用第三方的包,我懶得用,java
原生就很好了。