導航:首頁 > 編程語言 > 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是數字相關的資料

熱點內容
簡單音樂網站源碼 瀏覽:644
運動健康app華為手錶怎麼連接 瀏覽:748
肌肉塑造全書pdf下載 瀏覽:796
安卓簡約拼圖用什麼軟體好 瀏覽:289
fx1n加密程序 瀏覽:844
淘客阿里雲伺服器 瀏覽:476
100壓縮打造 瀏覽:422
安卓手機怎麼和蘋果平板傳文件 瀏覽:973
開始選項卡中的頁眉和頁腳命令選項 瀏覽:424
pdf的字體怎麼改 瀏覽:856
python讀寫視頻 瀏覽:88
科魯茲壓縮機軸承 瀏覽:353
word文檔轉換成pdf文件找不到 瀏覽:27
組件注冊命令 瀏覽:760
安卓大屏導航用的是什麼運放 瀏覽:443
myandroidtools的備份 瀏覽:900
python爬蟲天氣預報 瀏覽:761
android70許可權管理 瀏覽:749
魔獸辛迪加是什麼伺服器 瀏覽:472
電腦文件夾排序怎麼自定義排序 瀏覽:41