一个判断字符串是否是数字的正则表达式
生活随笔
收集整理的這篇文章主要介紹了
一个判断字符串是否是数字的正则表达式
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
http://www.blogjava.net/Javaphua/archive/2007/06/05/122131.html
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;
}
?
?
?
==========================
正則表達式
^-?[\d]*[.]?[\d]*
總結
以上是生活随笔為你收集整理的一个判断字符串是否是数字的正则表达式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 忘记Windows XP登录密码的9种解
- 下一篇: commons-lang(time应用)