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

字符串--java中判断字符串是否为数字的方法的几种方法?的更多相关文章

  1. (转载)java中判断字符串是否为数字的方法的几种方法

    java中判断字符串是否为数字的方法: 1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < ...

  2. java中判断字符串是否为数字的方法的几种方法

    1.用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = 0; i < str.length(); i++){ ...

  3. java中判断字符串是否相等有两种方法:

    1.用“==”运算符,该运算符表示指向字符串的引用是否相同,比如: String a="abc";String b="abc",那么a==b将返回true.这是 ...

  4. java 中判断字符串相等

    今天写Java代码时遇到一个问题,就是关于判断两个字符串是否相等的问题.(刚尝试用SSH框架) 在大多编程中,通常比较两个字符串是否相同的表达式是“==”,但在java中不能这么写.在java中,用的 ...

  5. c# 数组 字符串 C#中判断字符串中包含某个字符

    string str = "1,2,3,4,5,6,7";            string[] strArray = str.Split(','); //字符串转数组      ...

  6. Java中获取项目根路径和类加载路径的7种方法

    引言 在web项目开发过程中,可能会经常遇到要获取项目根路径的情况,那接下来我就总结一下,java中获取项目根路径的7种方法,主要是通过thisClass和System,线程和request等方法. ...

  7. Java中判断字符串是否为数字

    转载:https://blog.csdn.net/u013066244/article/details/53197756 用JAVA自带的函数 public static boolean isNume ...

  8. Java中判断字符串是否为数字的五种方法

    //方法一:用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ ...

  9. Java中判断字符串是否为数字的五种方法 (转)

    推荐使用第二个方法,速度最快. 方法一:用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = str.length( ...

随机推荐

  1. 移动 Web 开发技巧之(后续)

    昨天的<移动 Web 开发技巧>的这篇文章,大家反响不错,因为这些问题在大家日常写移动端的页面时经常遇到的.所以那个文章还是超级实用的,那么我们今天继续来分享一下移动端的web开发技巧吧, ...

  2. css关系选择器

    1.包含选择符(E F) 选择所有被E元素包含的F元素,中间用空格隔开示例:<!DOCTYPE html><html> <head> <meta charse ...

  3. get请求乱码

    1.get请求不管用什么编码都是乱码, 2.由于http请求最先到达的是容器,所以先要检查容器的unrencodeing <Connector port="80" proto ...

  4. 黑马程序员——OC语言 类和对象

    Java培训.Android培训.iOS培训..Net培训.期待与您交流! (以下内容是对黑马苹果入学视频的个人知识点总结) (一)类 1)类的声明 代码编写 ①定义一个Car类,拥有2个属性:轮子数 ...

  5. ubuntu下命令行打开pdf/doc/ppt文件

    1  打开pdf evince   *.pdf 2 打开ppt libreoffice  *.ppt3 打开doc libreoffice  *.doc

  6. 例子:Background Agent Sample

    通过本例程学习: 后台代理Agent的使用方法 定期代理(PeriodicTask)来说,限制了: 有一些API不能使用,并不是说你不调用就可以了,只要你在同一个程序集里使用了这些API,就不会通过验 ...

  7. Swift语言—常量、变量、类型赋值

    常量:常量在作用域内不能被赋值且不会发生改变,只读不写,每个常量都必须有唯一的名字和内存空间:关键字:let   变量:变量在作用区域内可以它的值可以被改变,可读可写可覆盖,每个常量都必须有唯一的名字 ...

  8. 搭建Tomcat6源代码阅读环境

    目标:使用MyEclipse8.5阅读Tomcat6源码. 第一步:在MyEclipse8.5中集成SVN插件. 第二步:从地址http://svn.apache.org/repos/asf/tomc ...

  9. JQuery源码解析(十)

    默认回调对象设计 不传入任何参数,调用add的时候将函数add到内部的list中,调用fire的时候顺序触发list中的回调函数: function fn1(val) { console.log('f ...

  10. SQL 变量

    1.变量可以暂时储存数据 --定义变量:  declare @xxx int --变量赋值:  set @xxx=1  select @xxx=3 --变量的使用:  print @xxx 2.--全 ...