日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > java >内容正文

java

在Java中怎么判断传递过来的字符串是否是数字,或者是否是字符串?(亲测)

發(fā)布時間:2024/9/20 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Java中怎么判断传递过来的字符串是否是数字,或者是否是字符串?(亲测) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Java中判斷字符串是否全是數(shù)字:

1.用JAVA自帶的函數(shù)
public static boolean isNumeric(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;
}

2.用正則表達式
首先要import?java.util.regex.Pattern 和?java.util.regex.Matcher
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}

3.使用org.apache.commons.lang
org.apache.commons.lang.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");
http://jakarta.apache.org/commons/lang/api-release/index.html下面的解釋:
isNumeric
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

上面三種方式中,第二種方式比較靈活。

第一、三種方式只能校驗不含負號“-”的數(shù)字,即輸入一個負數(shù)-199,輸出結(jié)果將是false;

而第二方式則可以通過修改正則表達式實現(xiàn)校驗負數(shù),將正則表達式修改為“^-?[0-9]+”即可,修改為“-?[0-9]+.?[0-9]+”即可匹配所有數(shù)字。

使用正則表達式:

1

2

3

4

5

6

7

8

public?boolean?isNumeric(String?str)?{??

????????Pattern?pattern?=?Pattern.compile("[0-9]*");??

????????Matcher?isNum?=?pattern.matcher(str);??

????????if?(!isNum.matches())?{??

????????????return?false;??

????????}??

????????return?true;??

????}

但是這個方法并不安全,沒有對字符串進行空校驗。?在程序執(zhí)行的時候很容易拋出異常。?例如執(zhí)行: ?

1

2

3

4

5

6

public?static?void?main(String[]?args)?{??

???????

????????String?str?=?null;??

????????System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));??

???

????}

就會拋出異常:

1

2

3

4

5

6

7

Exception?in?thread?"main"?java.lang.NullPointerException??

????at?java.util.regex.Matcher.getTextLength(Matcher.java:1140)??

????at?java.util.regex.Matcher.reset(Matcher.java:291)??

????at?java.util.regex.Matcher.<init>(Matcher.java:211)??

????at?java.util.regex.Pattern.matcher(Pattern.java:888)??

????at?com.ossez.bcu.util.BarcodeChecksum.isNumeric(BarcodeChecksum.java:37)??

????at?com.ossez.bcu.util.BarcodeChecksum.main(BarcodeChecksum.java:53)

所以這個方法并不準確。?如果執(zhí)行:

1

2

3

4

public?static?void?main(String[]?args)?{??

????????String?str?=?"";??

????????System.out.println(BarcodeChecksum.INSTANCE.isNumeric(str));??

????}

將會返回 true。?這說明這個方法沒有對空字符串進行校驗。?可以使用 Apache 的 StringUtils.isNumeric() 函數(shù)進行判斷。?這個函數(shù)位于 org.apache.commons.lang.StringUtils;? 中。?但是,需要注意,如果傳入?yún)?shù)為 "" 同樣也會你存在判斷不準確的情況,這時候需要首先對需要進行判斷的參數(shù)進行非空校驗,然后刪除傳入數(shù)據(jù)中的空格。

1

2

3

4

public?static?void?main(String[]?args)?{??

????????String?str?=?"";??

????????System.out.println(StringUtils.isNumeric(str));??

????}

上面這個函數(shù)將會返回 true。?請先 trim 數(shù)據(jù)

來源:https://www.cnblogs.com/pengzp97/p/10286435.html

總結(jié)

以上是生活随笔為你收集整理的在Java中怎么判断传递过来的字符串是否是数字,或者是否是字符串?(亲测)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。