java 判断字符串是否为数字 十进制 十六进制
2013-06-28 23:50
1406 查看
package com.regex.first; /** * @ClassName: regexTest1 * @Description: java判断字符串是否为数字。 * @author amosli * @date 2013-6-28 下午11:46:50 * @Email:amosli@infomorrow.com */ public class RegexNumberValidate { public static void main(String[] args){ String[] values = new String[]{ "10","32768","9999","ati","905Af","ffff" }; for(String value:values){ System.out.println("Validating value:\t"+value); if(isOctNumberRex(value)){ System.out.println("this is a Octnumber:"+value); }else { System.out.println("this isn't a Octnumber:"+value); } if(isHexNumberRex(value)){ System.out.println("this is a Hexnumber:"+value); }else { System.out.println("this isn't Hexnumber:"+value); } } } //十进制 private static boolean isOctNumber(String str) { boolean flag = false; for(int i=0,n=str.length();i<n;i++){ char c = str.charAt(i); if(c=='0'|c=='1'|c=='2'|c=='3'|c=='4'|c=='5'|c=='6'|c=='7'|c=='8'|c=='9'){ flag =true; } } return flag; } //十六进制 private static boolean isHexNumber(String str){ boolean flag = false; for(int i=0;i<str.length();i++){ char cc = str.charAt(i); if(cc=='0'||cc=='1'||cc=='2'||cc=='3'||cc=='4'||cc=='5'||cc=='6'||cc=='7'||cc=='8'||cc=='9'||cc=='A'||cc=='B'||cc=='C'|| cc=='D'||cc=='E'||cc=='F'||cc=='a'||cc=='b'||cc=='c'||cc=='c'||cc=='d'||cc=='e'||cc=='f'){ flag = true; } } return flag; } private static boolean isOctNumberRex(String str){ String validate = "\\d+"; return str.matches(validate); } private static boolean isHexNumberRex(String str){ String validate = "(?i)[0-9a-f]+"; return str.matches(validate); } } /*************print***********************/ Validating value: 10 this is a Octnumber:10 this is a Hexnumber:10 Validating value: 32768 this is a Octnumber:32768 this is a Hexnumber:32768 Validating value: 9999 this is a Octnumber:9999 this is a Hexnumber:9999 Validating value: ati this isn't a Octnumber:ati this is a Hexnumber:ati Validating value: 905Af this is a Octnumber:905Af this is a Hexnumber:905Af Validating value: ffff this isn't a Octnumber:ffff
相关文章推荐
- java 判断字符串是否为数字 十进制 十六进制
- java中判断字符串是否为数字的三种方法
- java中判断字符串是否为数字的方法
- java中判断字符串是否为数字的方法的几种方法
- java中判断字符串是否为数字的方法的几种方法
- java python oracle判断字符串是否为数字的函数
- Java中判断输入的字符串是否为数字常用方法
- Java中判断字符串是否为数字的五种方法
- java中判断字符串是否为数字的三种方法
- java判断字符串是否是纯数字
- java中判断字符串是否为数字的方法的几种方法
- java中判断字符串是否为数字的方法的几种方法
- java中判断字符串是否为数字的三种方法
- java判断输入的字符串是否为数字的方法
- java中判断字符串是否为数字的方法的几种方法
- java中判断字符串是否为数字的方法
- java中用正则表达式判断中文字符串中是否含有英文或者数字
- JAVA判断字符串是否为数字
- Java中判断字符串是否为数字的方法:
- java中判断字符串是否为数字的方法的几种方法