使用Xxx.valueOf(xxx)来进行类型转换时的注意事项
2014-04-15 09:35
375 查看
// 1用JAVA自带的函数 public static boolean isNumeric(String str){ for (int i = str.length();--i>=0;){ if (!Character.isDigit(str.charAt(i))){ return false; } } return true; } // 2用正则表达式 public static boolean isNumeric(String str){ Pattern pattern = Pattern.compile("[0-9]*"); return pattern.matcher(str).matches(); } // 3用ascii码 public static boolean isNumeric(String str){ for(int i=str.length();--i>=0;){ int chr=str.charAt(i); if(chr<48 || chr>57) return false; } return true; }
本文归纳了下简单数据类型与String间的类型转换所要注意的一些问题。
1、Xxx.valueOf(string),
(1) 为空判断,android提供和很好用操作字符串的工具类TextUtils,其中包含的就有判断为空的方法:isEmpty(string);java判断的话要使用 null ==string || "".equals(string) 两个语句来判断;不做为为空判断的话,将可能报java.lang.NumberFormatException:
Invalid long: "null";也可能是"";
(2)如果Xxx 是 Long
Integer 或者 Double ,,还要做string是否是 Long Integer 或者 Double 的判断,不然也可能报错 NumberFormatException;
下面介绍几种判断字符串是否是数字的方法
相关文章推荐
- jq ui.dialog.js简介
- extjs frame:true有什么用
- IOS7 UIScrollView & UINavigationController
- UIButton使用方法汇总
- LigerUI 界面中,加上文本框限制输入长度功能!
- 1.注册或登录页面设计:UILabel,UIButton,UITextField
- 1.注册或登录页面设计:UILabel,UIButton,UITextField
- ueditor 编辑器的配置 实现了上传图片与文件功能---附效果图
- Xcode警告:Multiple build commands for output file
- HashMap和HashTable,HashMap中key和value的原理 - 跳刀的兔子 - 博客园
- break、continue跳出循环的深入学习
- Codeforces 416A Guess a number!(水题)
- Unique Paths II
- egit 出现问题 The current branch is not configured for pull No value for key branch.master.merge found i
- Unique Paths
- 在UEFI GPT分区下 用U盘安装win7X64的方法(win8也是类似)
- Longest Consecutive Sequence
- POJ 2778 DNA Sequence
- Web Design:给实验室UI们的一堂课(下)
- 利用addChildViewController组织viewController---类似UITabBarViewController的效果