在做导入/导出功能时,客户要求导出数字类型的值时,将excel相应单元格属性设为number型,由此需判断字符串值是否为数字,代码如下:

public static boolean isNumber(String number) {int index = number.indexOf(".");
if (index < 0) {
return StringUtils.isNumeric(number);
} else {
String num1 = number.substring(0, index);
String num2 = number.substring(index + 1); return StringUtils.isNumeric(num1) && StringUtils.isNumeric(num2);
}
}

网上查"java 判断字符串为数字"阅资料,大部分资料都在讲字符串转为整数的情况,很少资料提及关于负数和小数的情况,最终决定采用StringUtils.isNumberic这个方法差别,

在测试导出时发现有报错,用debug模块一点点追踪,发现StringUtils.isNumeric会将空字符串返回true,先这样解决一下:

public static boolean isNumber(String number) {
if(number==null || "".equals(number))
return false;
int index = number.indexOf(".");
if (index < 0) {
return StringUtils.isNumeric(number);
} else {
String num1 = number.substring(0, index);
String num2 = number.substring(index + 1); return StringUtils.isNumeric(num1) && StringUtils.isNumeric(num2);
}
}

网上继续查找,这个方法怎么会有这种情况,最终在官网上找到其他程序员提出的这个问题及java开发团队的回答(原文链接:https://issues.apache.org/jira/browse/LANG-428 ),可能在未来某个版本中会把这个问题给解决掉,在此记录下。

StringUtils.isNumeric使用的更多相关文章

  1. 肯爹的 StringUtils.isNumeric(String str)

    在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...

  2. StringUtils.isNumeric(String str) 的一个坑(转)

    在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...

  3. StringUtils.isNumeric()的特殊点

    String str = "-1"; StringUtils.isNumeric(str) 返回的是false StringUtils.isNumeric()方法在判断字符串是否是 ...

  4. StringUtils工具类

    StringUtils源码,使用的是commons-lang3-3.1包.下载地址 http://commons.apache.org/lang/download_lang.cgi 以下是String ...

  5. 12. Android框架和工具之 StringUtils(字符串操作)

    1. StringUtils介绍: StringUtils是apache commons lang库(http://commons.apache.org/proper/commons-lang/dow ...

  6. commons-lang3之StringUtils

    字符串是一种在开发中经常使用到的数据类型,对字符串的处理也变得非常重要,字符串本身有一些方法,但都没有对null做处理,而且有时可能还需要做一些额外处理才能满足我们的需求,比如,要判断某个字符串中是否 ...

  7. StringUtils常用方法+StringUtils详细介绍

    StringUtils常用方法+StringUtils详细介绍   StringUtils用法+StringUtils详细介绍博文来源:http://yijianfengvip.blog.163.co ...

  8. StringUtils详细介绍

    StringUtils详细介绍 public static void TestStr(){ #null 和 "" 操作~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ...

  9. org.apache.commons.lang3.StringUtils中的StringUtils常用方法

    https://my.oschina.net/funmo/blog/615202?p=1 public static void TestStr(){ //null 和 ""操作~~ ...

随机推荐

  1. mysql快速上手3

    上一章给大家说的是数据库的视图,存储过程等等操作,这章主要讲索引,以及索引注意事项,如果想看前面的文章,url如下: mysql快速上手1 mysql快速上手2 索引简介 索引是对数据库表中一个或多个 ...

  2. javascript代码复用--继承

    由于javascript没有类的概念,因此无法通过接口继承,只能通过实现继承.实现继承是继承实际的方法,javascript中主要是依靠原型链要实现. 原型链继承 原型链继承是基本的继承模式,其本质是 ...

  3. windows下apache配置https

    1.下载带有openSSL的apache安装包,我下载的为apache_2.2.11-win32-x86-openssl-0.9.8i.msi,安装后确认一下bin路径下的openssl.exe,ss ...

  4. 解析php开发中的中文编码问题

    其实php开发中的中文编码并没有想像的那么复杂,虽然定位和解决问题没有定规,各种运行环境也各不尽然,但后面的原理是一样的. 了解字符集的知识是解决字符问题的基础. PHP程序设计中中文编码问题曾经困扰 ...

  5. ADO.NET的五个主要对象

    优秀文章链接:http://www.cnblogs.com/xianspace/archive/2009/02/21/1395307.html http://www.cnblogs.com/aito/ ...

  6. 移植Oracle procedure 到 postgresql

    1.登录postgresql psql -h 192.168.137.131 -p 5432 postgres satusc@6789#JKL 2.创建用户 CREATE USER name thun ...

  7. C# 生成简单验证码

    网站登录总是会用到验证码,生成验证码对于C#来说很简单.因为有专门封装好的GDI+类可以直接调用使用具体代码如下 using System; using System.Collections.Gene ...

  8. Android屏幕像素密度适配详解

    讲到像素密度,我们先要搞明白什么是像素密度,像素密度的字面上的意思为手机屏幕上一定尺寸区域内像素的个数.在Android开发中, 我们一般会使用每英寸像素密度(dpi)这样一个单位来表示手机屏幕的像素 ...

  9. TAG的用法和用途[转]

    用一个例子来说明:一个combobox控件...一个textBox控件...一个datagridview控件!datagridview控件是连接数据库的...combobox和textBox是联合查询 ...

  10. mouse_driver

    1:function.h #ifndef FUNCTION_H#define FUNCTION_H #define DRIVER_FUNCTION_ADD_DEVICE#define DRIVER_F ...