java判断字符串是否为数字或中文或字母

  1. 1.判断字符串是否仅为数字:  
  2. 1>用JAVA自带的函数  
  3. public 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.  }  
  11. 2>用正则表达式  
  12. public static boolean isNumeric(String str){  
  13.     Pattern pattern = Pattern.compile("[0-9]*");  
  14.     return pattern.matcher(str).matches();     
  15.  }  
  16. 3>用ascii码  
  17. public 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.     
  26. 2.判断一个字符串的首字符是否为字母  
  27. public   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.     
  41. public     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.     
  52. 3 .判断是否为汉字  
  53. public 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 &&    
  65. ints[1]>=0x40 && ints[1]<=0xFE){   
  66.                                         isGB2312=true;   
  67.                                         break;   
  68.                             }   
  69.                 }   
  70.     }   
  71.     return isGB2312;   
  72. }
  73. 转载来源:http://blog.csdn.net/kevinitheimablog/article/details/51304952
原文地址:https://www.cnblogs.com/peijyStudy/p/7851342.html