• 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
  • 相关阅读:
    苹果 macOS 安装 Source Code Pro
    C# 中代码执行 ping 操作
    WPF 中 Path 使用虚线
    查看 Java Web 开发环境软件是 32 位还是 64 位
    linux 磁盘io监控
    Linux下系统如何监控服务器硬件、操作系统、应用服务和业务
    ORA-00257: archiver error. Connect internal only, until freed 错误的处理方法
    完全卸载oracle11g步骤
    用JDBC连接SQL Server2017数据库
    oracle INS-13001 环境不满足最低要求
  • 原文地址:https://www.cnblogs.com/peijyStudy/p/7851342.html
Copyright © 2020-2023  润新知