本文目錄一覽:
- 1、Java中怎樣判斷一個字符串是否是數字
- 2、Java中判斷字符串是否為數字的幾種方法
- 3、java中驗證字符串是不是數字的四種方法
- 4、java中判斷字符串是否為純數字
- 5、java中怎麼判斷指定的數據是字符串是否是數字?
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”);
下面的解釋:
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中判斷字符串是否為數字的幾種方法
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中驗證字符串是不是數字的四種方法
判斷字符串是不是數字,大家可能會用一些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中判斷字符串是否為純數字
方法一:利用正則表達式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中怎麼判斷指定的數據是字符串是否是數字?
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是一門面向對象編程語言,不僅吸收了C++語言的各種優點,還摒棄了C++里難以理解的多繼承、指針等概念,因此Java語言具有功能強大和簡單易用兩個特徵。Java語言作為靜態面向對象編程語言的代表,極好地實現了面向對象理論,允許程序員以優雅的思維方式進行複雜的編程 。
Java具有簡單性、面向對象、分布式、健壯性、安全性、平台獨立與可移植性、多線程、動態性等特點 。Java可以編寫桌面應用程序、Web應用程序、分布式系統和嵌入式系統應用程序等。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/286836.html