【www.shanpow.com--数字歇后语】
java判断字符串是否为数字篇1:Java中判断字符串是否为数字的五种方法
推荐使用第二个方法,速度最快。
方法一:用JAVA自带的函数
Java代码
public static boolean isNumeric(String str){
for (int i = str.length();--i>=0;){
if (!Character.isDigit(str.charAt(i))){
return false;
}
}
return true;
}
方法二:
Java代码
/*
* 判断是否为整数
* @param str 传入的字符串
* @return 是整数返回true,否则返回false
*/
public static boolean isInteger(String str) {
Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");
return pattern.matcher(str).matches();
}
方法三:
Java代码
public static boolean isNumeric(String str){
Pattern pattern = Pattern.compile("[0-9]*");
return pattern.matcher(str).matches();
}
方法四:
Java代码
public final static boolean isNumeric(String s) {
if (s != null && !"".equals(s.trim()))
return s.matches("^[0-9]*$");
else
return false;
}
3方法五:用ascii码
Java代码
public static boolean isNumeric(String str){
for(int i=str.length();--i>=0;){
int chr=str.charAt(i);
if(chr<48 || chr>57)
return false;
}
return true;
}
文章地址:
http://javapub.iteye.com/blog/666544
相关文章:
Java判断是否为整数和浮点数 http://javapub.iteye.com/blog/737689
java判断字符串是否为数字篇2:java 判断字符串是不是数字
java 判断字符串是不是数字
2010-08-30 14:57:01| 分类:
Java
|字号 订阅
判断字符串是不是数字,大家可能会用一些java自带的方法,也有可能用其他怪异的招式,比如判断是不是整型数字,将字符串强制转换成整型,不是数 字的就会抛出错误,那么就不是整型的了。但本文介绍的比较好的两种方法: 1。java类库自带的方法: public boolean isNum(String msg){if(java.lang.Character.isDigit(msg.charAt(0))){return true;}return false;}更新: yining提醒后,我才发现以上方法写得不够到位,现在就改为下面的简单说明了,至于具体的方法实现字符串判断是否数字我就不写了。java.lang.Character.isDigit(char ch) boolean isDigit 只能作用于char,所以判断字符串是否为数字,要一个一个拿出char进行判断。 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;}
java判断字符串是否为数字篇3:java中判断字符串是否为数字的方法的几种方法
ava中判断字符串是否为数字的方法:
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]+”即可匹配所有数字。