免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 954 | 回复: 0
打印 上一主题 下一主题

判断字符串是否为数字 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-03-26 14:41 |只看该作者 |倒序浏览
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
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP