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();
if (result == true) {
System.out.println("該字符串是純數字");
}else{
System.out.println("該字符串不是純數字");
}
}
}