- 论坛徽章:
- 0
|
java中判断字符串是否为数字的方法:
1.用JAVA自带的函数
public static boolean isNumeric(String str){
for (int i = 0; i
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
类似其它
import org.apache.commons.lang.CharSetUtils;
String safe = CharSetUtils.keep("123456这些不是数字", Constants.DEFAULT_ALLOWED_CHARS);
其中: Constants.DEFAULT_ALLOWED_CHARS="A-Za-z0-9";(悲验证字符串可以是字母和数字)
Constants.DEFAULT_ALLOWED_CHARS="0-9";(悲验证字符串可以是数字)
另附:
lang.*的说明文档:http://jakarta.apache.org/commons/lang/userguide.html#lang__
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/25102/showart_265088.html |
|