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. 2.判断一个字符串的首字符是否为字母
  26. public static boolean test(String s)
  27. {
  28. char c = s.charAt(0);
  29. int i =(int)c;
  30. if((i>=65&&i<=90)||(i>=97&&i<=122))
  31. {
  32. return true;
  33. }
  34. else
  35. {
  36. return false;
  37. }
  38. }
  39. public static boolean check(String fstrData)
  40. {
  41. char c = fstrData.charAt(0);
  42. if(((c>='a'&&c<='z') || (c>='A'&&c<='Z')))
  43. {
  44. return true;
  45. }else{
  46. return false;
  47. }
  48. }
  49. 3 .判断是否为汉字
  50. public boolean vd(String str){
  51. char[] chars=str.toCharArray();
  52. boolean isGB2312=false;
  53. for(int i=0;i<chars.length;i++){
  54. byte[] bytes=(""+chars[i]).getBytes();
  55. if(bytes.length==2){
  56. int[] ints=new int[2];
  57. ints[0]=bytes[0]& 0xff;
  58. ints[1]=bytes[1]& 0xff;
  59. if(ints[0]>=0x81 && ints[0]<=0xFE &&
  60. ints[1]>=0x40 && ints[1]<=0xFE){
  61. isGB2312=true;
  62. break;
  63. }
  64. }
  65. }
  66. return isGB2312;
  67. }
  68. 转载来源:http://blog.csdn.net/kevinitheimablog/article/details/51304952