java中判斷字符串是否為純數字,正則表達式判斷


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("該字符串不是純數字");
}
}
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM