java中判断字符串是否为纯数字

时间:2025-03-11 22:23:14

public class Testone {
public static void main(String[] args){
String str="123456";
boolean result=("[0-9]+");
if (result == true) {
("该字符串是纯数字");
}else{
("该字符串不是纯数字");
}
}
}

方法二:利用Pattern.

import ;
import ;

public class Testone {
public static void main(String[] args){
String str="123456";
Pattern pattern = ("[0-9]{1,}");
Matcher matcher = ((CharSequence)str);
boolean result=();
if (result == true) {
("该字符串是纯数字");
}else{
("该字符串不是纯数字");
}
}
}