java中如何判断字符串是否为纯数字?
一、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("该字符串不为纯数字");}}}
二、利用表达式判断法:
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("该字符串不为纯数字");}}}