11.判断字符串是否仅为数字: 21>用JAVA自带的函数 3public static boolean isNumeric(String str){ 4 for (int i = str.length();--i>=0;){ 5 if (!Character.isDigit(str.charAt(i))){ 6 return false; 7 } 8 } 9 return true; 10 } 112>用正则表达式 12public static boolean isNumeric(String str){ 13 Pattern pattern = Pattern.compile("[0-9]*"); 14 return pattern.matcher(str).matches(); 15 } 163>用ascii码 17public static boolean isNumeric(String str){ 18 for(int i=str.length();--i>=0;){ 19 int chr=str.charAt(i); 20 if(chr<48 || chr>57) 21 return false; 22 } 23 return true; 24} 25 262.判断一个字符串的首字符是否为字母 27public static boolean test(String s) 28 { 29 char c = s.charAt(0); 30 int i =(int)c; 31 if((i>=65&&i<=90)||(i>=97&&i<=122)) 32 { 33 return true; 34 } 35 else 36 { 37 return false; 38 } 39 } 40 41public static boolean check(String fstrData) 42 { 43 char c = fstrData.charAt(0); 44 if(((c>='a'&&c<='z') || (c>='A'&&c<='Z'))) 45 { 46 return true; 47 }else{ 48 return false; 49 } 50 } 51 523 .判断是否为汉字 53public boolean vd(String str){ 54 55 char[] chars=str.toCharArray(); 56 boolean isGB2312=false; 57 for(int i=0;i<chars.length;i++){ 58 byte[] bytes=(""+chars[i]).getBytes(); 59 if(bytes.length==2){ 60 int[] ints=new int[2]; 61 ints[0]=bytes[0]& 0xff; 62 ints[1]=bytes[1]& 0xff; 63 64 if(ints[0]>=0x81 && ints[0]<=0xFE && 65ints[1]>=0x40 && ints[1]<=0xFE){ 66 isGB2312=true; 67 break; 68 } 69 } 70 } 71 return isGB2312; 72}
java判断字符串是否为数字或中文或字母
Wesley13
2021-10-11
1350 1 0
点赞
收藏
评论区
加载中...