導航:首頁 > 編程語言 > java判斷string是數字

java判斷string是數字

發布時間:2022-09-20 05:30:39

java中怎樣判斷一個字元串是否是數字

用java的異常機制,不僅可以判斷是否是數字,還可以判斷整數或者小數:
public void checkInt(String bh){
try{
int num = Integer.parseInt(bh);//將輸入的內容轉換成int
System.out.println("是整數:"+num);//是整數
}catch (NumberFormatException e) {//轉換成int類型時失敗
try{
double d =Double.parseDouble(bh);//轉成double類型
System.out.println("是小數:"+d);//是小數
}catch (NumberFormatException e2) {//轉成double類型失敗
System.out.println("不是數字");
}
}
}

② java中判斷字元串是否為數字的方法的幾種方法

可以使用正則表達式+中文字元編碼區間驗證一個字元串中是否包含漢字
代碼如下:
public
static
void
main(string[]
args)
{
int
count
=
0;
string
regex
=
"[\\u4e00-\\u9fa5]";
//system.out.println(regex);
string
str
=
"中文fdas
";
//system.out.println(str);
pattern
p
=
pattern.compile(regex);
matcher
m
=
p.matcher(str);
while
(m.find())
{
for
(int
i
=
0;
i
<=
m.groupcount();
i++)
{
count
=
count
+
1;
}
}
system.out.println("共有
"
+
count
+
"個
");
}

③ java判斷string是不是數字

常見的方法為正則判斷,也可以使用char.isDigit(Char c)方法。

示例:

//方法一:正則
publicbooleanisNumerics(Stringstr){
if(!str.matches("[0-9]+")){
returnfalse;
}else{
returntrue;
}

}

//方法二:isDigit()
publicbooleanisNumeric(Stringstr){
for(inti=0;i<str.length();i++){
System.out.println(str.charAt(i));
if(!Character.isDigit(str.charAt(i))){
returnfalse;
}
}
returntrue;
}

④ Java中怎樣判斷一個字元串是否是數字

ava中判斷字元串是否為數字的方法:

1.用JAVA自帶的函數
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

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

第一、三種方式只能校驗不含負號「-」的數字,即輸入一個負數-199,輸出結果將是false;

而第二方式則可以通過修改正則表達式實現校驗負數,將正則表達式修改為「^-?[0-9]+」即可,修改為「-?[0-9]+.?[0-9]+」即可匹配所有數字。

閱讀全文

與java判斷string是數字相關的資料

熱點內容
程序員可以干什麼 瀏覽:70
績效考核權重分配演算法 瀏覽:524
android應用logo 瀏覽:898
光遇安卓服墓土商店什麼時候開 瀏覽:566
月收益翻倍的源碼 瀏覽:638
asop源碼放在哪裡 瀏覽:989
電腦伺服器密碼怎麼找 瀏覽:574
jdp轉換pdf 瀏覽:749
把pdf導入iphone 瀏覽:508
米哈游租賃的雲伺服器是哪個 瀏覽:524
android直接打電話 瀏覽:1016
ubuntu停止命令 瀏覽:283
cnc攻絲編程 瀏覽:869
換個手機號碼app怎麼注冊 瀏覽:320
怎麼下載小猴口算app 瀏覽:115
輕鏈app的貨怎麼樣 瀏覽:625
電腦里的u盤如何加密 瀏覽:372
我的世界全部版本伺服器下載地址 瀏覽:50
交換原理pdf 瀏覽:230
菜鳥驛站app怎麼邀請新人 瀏覽:449