http://xiangai.taobao.com
http://shop148612228.taobao.com

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("该字符串不是纯数字");
}
}
}

posted @ 2021-05-26 20:16  万事俱备就差个程序员  阅读(7197)  评论(0编辑  收藏  举报

http://xiangai.taobao.com
http://shop148612228.taobao.com
如果您觉得对您有帮助.领个红包吧.谢谢.
支付宝红包
微信打赏 支付宝打赏