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

 我来答
千锋教育
2015-10-22 · 做真实的自己 用良心做教育
千锋教育
千锋教育专注HTML5大前端、JavaEE、Python、人工智能、UI&UE、云计算、全栈软件测试、大数据、物联网+嵌入式、Unity游戏开发、网络安全、互联网营销、Go语言等培训教育。
向TA提问
展开全部
Java中判断字符串是否为数字的方法:

1.用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = 0; i < str.length(); i++){
System.out.println(str.charAt(i));
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}

2.用正则表达式
首先要import java.util.regex.Pattern 和 java.util.regex.Matcher
public boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
Matcher isNum = pattern.matcher(str);
if( !isNum.matches() ){
return false;
}
return true;
}

3.使用org.apache.commons.lang
org.apache.commons.lang.StringUtils;
boolean isNunicodeDigits=StringUtils.isNumeric("aaa123456789");
http://jakarta.apache.org/commons/lang/api-release/index.html下面的解释:
isNumeric
public static boolean isNumeric(String str)Checks if the String contains only unicode digits. A decimal point is not a unicode digit and returns false.
null will return false. An empty String ("") will return true.
StringUtils.isNumeric(null) = false
StringUtils.isNumeric("") = true
StringUtils.isNumeric(" ") = false
StringUtils.isNumeric("123") = true
StringUtils.isNumeric("12 3") = false
StringUtils.isNumeric("ab2c") = false
StringUtils.isNumeric("12-3") = false
StringUtils.isNumeric("12.3") = false

Parameters:
str - the String to check, may be null
Returns:
true if only contains digits, and is non-null

上面三种方式中,第二种方式比较灵活。

第一、三种方式只能校验不含负号“-”的数字,即输入一个负数-199,输出结果将是false;

而第二方式则可以通过修改正则表达式实现校验负数,将正则表达式修改为“^-?[0-9]+”即可,修改为“-?[0-9]+.?[0-9]+”即可匹配所有数字。
TableDI
2024-07-18 广告
在Excel中匹配两列数据,通常有以下几种方法:1. **使用VLOOKUP函数**:该函数可根据指定值,在另一列或区域中查找并返回对应的结果。例如,在C1单元格输入`=VLOOKUP(A1,B:B,1,FALSE)`,即可查找A1在B列中... 点击进入详情页
本回答由TableDI提供
最喜欢淼淼DQ
2013-10-10 · 超过70用户采纳过TA的回答
知道答主
回答量:163
采纳率:80%
帮助的人:54.9万
展开全部
方法一:利用正则表达式
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();
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
圣鸾OJ
2015-10-03 · TA获得超过1629个赞
知道小有建树答主
回答量:1136
采纳率:96%
帮助的人:107万
展开全部
方法一:利用正则表达式
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("该字符串不是纯数字");
}
}
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式