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]+”即可匹配所有数字。

最新文章

  1. Node.js学习-1
  2. 创建支持多种屏幕尺寸的Android应用
  3. 遍历map集合
  4. HDU 2010
  5. web前端入门:一小时学会写页面
  6. saltstack之(四)远程执行及常用模块
  7. hdu 4617 Weapon(叉积)
  8. 树-哈夫曼树(Huffman Tree)
  9. php用正则表达式获取网站的标题内容
  10. Ajax实现的长轮询不阻塞同一时间内页面的其他Ajax请求(同域请求)
  11. HTTPS 加密算法原理机制解析
  12. DevCloud让代码检查更科学
  13. [知了堂学习笔记]_JSON数据操作第1讲(初识JSON)
  14. Vux配置指南
  15. 好代码是管出来的——.Net Core集成测试与数据驱动测试
  16. 菜鸟教程之工具使用(五)——JRebel与Windows服务的Tomcat集成
  17. Paxos算法1-算法形成理论[转载]
  18. nodejs windows安装
  19. Englis - 英文字母和音标
  20. 常用工具说明--jsdoc 前端文档输出工具

热门文章

  1. RabbitMQ 的安装----windows环境
  2. 发布-订阅消息系统Kafka简介
  3. Unity发布WebGl注意事项
  4. ubuntu 使用cron设置定时启动任务
  5. TestLink工具使用手册介绍
  6. 温度转换-java
  7. Date.parse()转化日期为时间戳,ios与Android兼容写法
  8. Linux装agent
  9. Web.xml详解分析
  10. java 程序编译和运行过程