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

java判斷字元串是否是數字

發布時間:2024-08-16 20:39:41

『壹』 java中怎麼判斷指定的數據是字元串是否是數字

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

1.用JAVA自帶的函數

public static boolean isNumeric(String str){for (int i = 0; i < str.length(); 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; }

『貳』 java中驗證字元串是不是數字的四種方法

判斷字元串是不是數字,大家可能會用一些java自帶的方法,也有可能用其他怪異的招式,比如判斷是不是整型數字,將字元串強制轉換成整型,不是數字的就會拋出錯誤,那麼就不是整型的了。但本文介紹的比較好的兩種方法:
1。java類庫自帶的方法:
public boolean isNum(String msg){
if(java.lang.Character.isDigit(msg.charAt(0))){
return true;}return false;}0202更新:發現以上方法寫得不夠到位,現在就改為下面的簡單說明了,至於具體的方法實現字元串判斷是否數字就不寫了。
java.lang.Character.isDigit(char ch) boolean
isDigit 只能作用於char,所以判斷字元串是否為數字,要一個一個拿出char進行判斷。
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;}02
3。用正則表達式

『叄』 Java中判斷字元串是否為數字的幾種方法

1.使用Character.isDigit(char)判斷
char num[] = str.toCharArray();//把字元串轉換為字元數組
StringBuffer title = new StringBuffer();//使用StringBuffer類,把非數字放到title中
StringBuffer hire = new StringBuffer();//把數字放到hire中
for (int i = 0; i < num.length; i++) {
// 判斷輸入的數字是否為數字還是字元
if (Character.isDigit(num[i])) {把字元串轉換為字元,再調用Character.isDigit(char)方法判斷是否是數字,是返回True,否則False
hire.append(num[i]);// 如果輸入的是數字,把它賦給hire} else {title.append(num[i]);// 如果輸入的是字元,把它賦給title}}}
2.使用類型轉換判斷try {String str="123abc";
int num=Integer.valueOf(str);//把字元雹態升串強制轉換為閉睜數字
return true;//如果是數字,返回True
} catch (Exception e) {
return false;//如果拋出異常,返回False}
3.使用正則表達式判斷
String str = "";
boolean isNum = str.matches("[0-9]+");
//+表示1個或多個(如"3"或"225"),*表示0個或多個([0-9]*)(如""或"1"或"22"),?表示0個或1個([0-9]?)(如"源老"或"7")
ps:這個方法只能用於判斷是否是正整數

『肆』 java 怎麼判斷一串字元串,是不是6位的數字

1、首先判斷字元串長度是否是6位

Stringstr="";//需要判斷的字元串
if(str.length==6){//如果長度是6,則可能是6位數字,否則不可能6位的
}

2、通過轉換成數字捕獲異常,如果有異常,則不是數字,沒有異常,則是數字

try{
inta=Integer.valueOf(str);//轉換成數字,如果不是數字,則會拋出異常,進入catch中
System.out.println("是數字");//如果是數字,則會執行該語句。
}catch(Exceptionex){
}

3、結合上面兩個步驟,完成6位數字的判斷

if(str.length==6){//如果長度是6,則可能是6位數字,否則不可能6位的
ry{
inta=Integer.valueOf(str);//轉換成數字,如果不是數字,則會拋出異常,進入catch中
System.out.println("是6位數字");//如果是數字,則會執行該語句。
}catch(Exceptionex){
}
}

『伍』 java 怎麼判斷一個字元串中是否包含數字

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

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中判斷字元串是否為純數字

方法一:利用正則表達式 public class Testone { public static void main(String[] args){ String str="123456"; boolean result=str.matches("[0-9]+"); if (result == true) { System.out.println("該字元串是純數字");}else{System.out.println("該字元串不是純數字");}}}方法二:利用Pattern. import java.util.regex.Matcher; import java.util.regex.Pattern; public class Testone { public static void main(String[] args){ String str="123456"; Pattern pattern = Pattern.compile("[0-9]{1,}"); Matcher matcher = pattern.matcher((CharSequence)str); boolean result=matcher.matches(); System.out.println("該字元串是純數字");}else{System.out.println("該字元串不是純數字");}}}

閱讀全文

與java判斷字元串是否是數字相關的資料

熱點內容
飢荒被伺服器踢出怎麼進 瀏覽:168
c編譯器哪款好 瀏覽:729
快手寶哥發明什麼app 瀏覽:820
張艷玲編譯 瀏覽:66
android展開收起動畫 瀏覽:235
linuxxz文件 瀏覽:160
在游戲中心裏面怎麼玩到解壓神器 瀏覽:482
電腦發到手機裡面照片怎麼解壓 瀏覽:71
虛擬pdf列印機64位 瀏覽:411
支付寶AES加密和解密 瀏覽:379
編譯實驗原理下載 瀏覽:130
加密防偽溯源系統私人定做 瀏覽:222
掃碼給電動車充電的app叫什麼 瀏覽:760
關閉命令提醒 瀏覽:356
雲賬本app伺服器 瀏覽:499
python輸入數字循環 瀏覽:368
未成年人用什麼app 瀏覽:516
程序員出差多久回家 瀏覽:433
安卓如何更改文件名字 瀏覽:1002
程序員女友禿頭圖片 瀏覽:168