Chinaunix首页 | 论坛 | 博客
  • 博客访问: 255905
  • 博文数量: 170
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1709
  • 用 户 组: 普通用户
  • 注册时间: 2014-05-06 18:01
文章分类

全部博文(170)

文章存档

2016年(11)

2015年(130)

2014年(29)

分类: Java

2015-03-20 16:59:15

 

Validate if a given string is numeric.

Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

Note: It is intended for the problem statement to be ambiguous. You should gather all requirements up front before implementing one.




public boolean isNumber(String s) {
        if(s==null)
return false;
 
if(s.trim().isEmpty()){  
           return false;  
       }  
       String regex = "[-+]?(\\d+\\.?|\\.\\d+)\\d*(e[-+]?\\d+)?";  
       if(s.trim().matches(regex)){  
           return true;  
       }else{  
           return false;  
       }  
    }
阅读(161) | 评论(0) | 转发(0) |
0

上一篇:Combinations

下一篇:Permutation Sequence

给主人留下些什么吧!~~