肯爹的 StringUtils.isNumeric(String str)
在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下面的代码是判断一个参数非空,且为整数:
if(StringUtils.isNumeric(str) && StringUtils.isNotBlank(str)){
// do sth
}
在简单不过的代码,却隐藏着bug !
因为如果 str = "-1"; StringUtils.isNumeric(str) 返回的是 false! 真是肯爹不偿命啊。
下面是测试:
public static void main(String[] args)
{
System.out.println(StringUtils.isNumeric("-1"));
}
运行结果:false
肯爹吧?用正则表达式实现不是很简单吗?怎么会这样,看了下源码:
public static boolean isNumeric(String str) {
if (str == null) {
return false;
}
int sz = str.length();
for (int i = 0; i < sz; i++) {
if (Character.isDigit(str.charAt(i)) == false) {
return false;
}
}
return true;
}
继续跳进去:
public static boolean isDigit(char ch) {
return isDigit((int)ch);
}
继续:
public static boolean isDigit(int codePoint) {
boolean bDigit = false;
if (codePoint >= MIN_CODE_POINT && codePoint <= FAST_PATH_MAX) {
bDigit = CharacterDataLatin1.isDigit(codePoint);
} else {
int plane = getPlane(codePoint);
switch(plane) {
case(0):
bDigit = CharacterData00.isDigit(codePoint);
break;
case(1):
bDigit = CharacterData01.isDigit(codePoint);
break;
case(2):
bDigit = CharacterData02.isDigit(codePoint);
break;
case(3): // Undefined
case(4): // Undefined
case(5): // Undefined
case(6): // Undefined
case(7): // Undefined
case(8): // Undefined
case(9): // Undefined
case(10): // Undefined
case(11): // Undefined
case(12): // Undefined
case(13): // Undefined
bDigit = CharacterDataUndefined.isDigit(codePoint);
break;
case(14):
bDigit = CharacterData0E.isDigit(codePoint);
break;
case(15): // Private Use
case(16): // Private Use
bDigit = CharacterDataPrivateUse.isDigit(codePoint);
break;
default:
// the argument's plane is invalid, and thus is an invalid codepoint
// bDigit remains false;
break;
}
}
return bDigit;
}
在下面一步失败:
static boolean isDigit(int ch) {
int type = getType(ch);
return (type == Character.DECIMAL_DIGIT_NUMBER);
}
也就是说他的实现完全没有考虑到 - + 前缀的问题,这不是傻叉吗?
下面的结果都是 false:
public static void main(String[] args)
{
System.out.println(StringUtils.isNumeric("-1"));
System.out.println(StringUtils.isNumeric("+1"));
}
这是他的方法注释:
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
只能包含 unicode 的数字, +, -, . 三者都不能算作是unicode 数字。
肯爹的 StringUtils.isNumeric(String str)的更多相关文章
- StringUtils.isNumeric(String str) 的一个坑(转)
在项目中遇到一处bug,调试的结果竟然是StringUtils.isNumeric(String str) 在捣鬼(采用的是org.apache.commons.lang.StringUtils),下 ...
- StringUtils.isNumeric()的特殊点
String str = "-1"; StringUtils.isNumeric(str) 返回的是false StringUtils.isNumeric()方法在判断字符串是否是 ...
- StringUtils.isNumeric使用
在做导入/导出功能时,客户要求导出数字类型的值时,将excel相应单元格属性设为number型,由此需判断字符串值是否为数字,代码如下: public static boolean isNumber( ...
- 关于String str =new String("abc")和 String str = "abc"的比较
String是一个非常常用的类,应该深入的去了解String 如: String str =new String("abc") String str1 = "abc&qu ...
- 经典String str = new String("abc")内存分配问题
出自:http://blog.csdn.net/ycwload/article/details/2650059 今天要找和存储管理相关的一些知识,网上搜了半天也没有找到完善的(30%的程度都不到),没 ...
- Asp.net中使用Server.HtmlDecode(string str)的使用
前言: 在使用Visual Studio开发web页面时,需要在GridView中绑定Table数据,并加入了CommandField, 试图,点击详情按钮是,获取GridView中Rows中Cell ...
- compareTo(String str)与compareToIgnoreCase(String str)
一.compareTo(String str)方法 返回值:如果参数字符串等于此字符串,则返回值 0:如果此字符串按字典顺序小于字符串参数,则返回一个小于 0 的值:如果此字符串按字典顺序大于字符串参 ...
- String str=new String("a")和String str = "a"有什么区别?
问:String str=new String("a")和String str = "a"有什么区别? 答:String str = "a" ...
- String str 与 String str=new String("") 区别
1.当使用String str="abc",这种方式时,先去内存的Heap中找是否存在"abc"这个字符串,若存在,则将地址引用.若不存在则创建. 2.当使用S ...
随机推荐
- UIScrollView子控件的布局
scorllView内部子控件添加约束的注意点: 1.子控件的尺寸不能通过UIScrollView来计算 *比如可以设置固定值 (width==100 height ==100) *比如可以相对于UI ...
- 手动测试——MTM
在Test Manager中,测试计划用于管理某个迭代的整个测试工作.包括测试用例.测试结果,计划测试的配置. Test Center分为4个主要活动区域: Plan---用于管理整个测试计划,包括计 ...
- Sprint第三个冲刺(第六天)
一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图:
- MVC怎么在当前视图中,传递参数给到另外一个视图?
在TransData.cshtml视图中: <div> <!--在一个视图中,请求另外一个视图,并且将数据传到另外一个视图--> <!--视图中调用无返回值的方法,需要加 ...
- jquery选择器(原创)<二>
jquery选择器,选择接着学: 前面学习了基本选择器中的CSS选择器,现在学层级选择器: 1.子元素选择器 子元素选择器,用于在给定的父元素下,查找这个父元素下面的所有的子元素,语法格式,如下: $ ...
- How to Convert Subversion Repo to Git
用了比较长时间的 SVN,但现在新的项目都采用Git.之前的项目又不得不维护,那么能不能将项目从SVN迁移到Git呢.答案是肯定的,网上的方案是 git-svn,或者更高级的封装 svn2git. 方 ...
- 取值:webconfig中的key
String rootUrl = System.Configuration.ConfigurationManager.AppSettings["SiteDomain"].ToStr ...
- 重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML
[源码下载] 重新想象 Windows 8.1 Store Apps (87) - TTS: Speak Text, Speak SSML 作者:webabcd 介绍重新想象 Windows 8.1 ...
- 关于我的OI生涯(AFO){NOIP2016 后}
这篇我就随意写啦~不用统一的“题解”形式.♪(^∀^●)ノ 也分好几次慢慢更吧~ 对于NOIP2016的总结,我本想善始善终back回,但是心情不足以支撑我,那就只能有始有终了......下面进入我的 ...
- AI - Ideas
Idea: Comprehend code and re-factory code to more readable. The AI program can comprehend source cod ...