字符串问题

判断字符串是否为null或者空
1 public static boolean isEmpty(final CharSequence s) {
2         return s == null || s.length() == 0;
3 }

 

判断去除空格后的字符串是否为null或者空
1 public static boolean isTrimEmpty(final String s) {
2         return (s == null || s.trim().length() == 0);
3 }
判断字符串是否为null或者空
 1 public static boolean isSpace(final String s) {
 2         if (s == null) return true;
 3         for (int i = 0, len = s.length(); i < len; ++i) {
 4             if (!Character.isWhitespace(s.charAt(i))) {
 5                 return false;
 6             }
 7         }
 8         return true;
 9 }
判断忽略大小写情况下,字符串是否equals
1 public static boolean equalsIgnoreCase(final String s1, final String s2) {
2         return s1 == null ? s2 == null : s1.equalsIgnoreCase(s2);
3 }
判断字符串长度
1 public static int length(final CharSequence s) {
2         return s == null ? 0 : s.length();
3 }
避免字符串null时,空指针问题
1 public static String null2Length0(final String s) {
2         return s == null ? "" : s;
3  }
字符串首字母大写
1 public static String upperFirstLetter(final String s) {
2         if (isEmpty(s) || !Character.isLowerCase(s.charAt(0))) return s;
3         return String.valueOf((char) (s.charAt(0) - 32)) + s.substring(1);
4 }

首字母小写

1 public static String lowerFirstLetter(final String s) {
2         if (isEmpty(s) || !Character.isUpperCase(s.charAt(0))) return s;
3         return String.valueOf((char) (s.charAt(0) + 32)) + s.substring(1);
4 }

判断字符串是否为纯数字

 1 public static boolean isNumeric(String str){
 2     for (int i = str.length();--i>=0;){  
 3         if (!Character.isDigit(str.charAt(i))){
 4             return false;
 5         }
 6     }
 7     return true;
 8 }
 9 
10 //使用ASCII码判断
11 public static boolean isNumeric(String str){
12     for(int i=str.length();--i>=0;){
13         int chr=str.charAt(i);
14         if(chr<48 || chr>57)
15             return false;
16     }
17    return true;
18 }
19 //使用正则,(推荐)
20 public static boolean isInteger(String str) {  
21         Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
22         return pattern.matcher(str).matches();  
23 }
24 //使用正则
25 public final static boolean isNumeric(String s) {
26     if (s != null && !"".equals(s.trim()))
27         return s.matches("^[0-9]*$");
28     else
29         return false;
30 }

判断字符串是否为纯字母

 1 public static boolean isNumeric(String str){
 2     for (int i = str.length();--i>=0;){  
 3         if (!Character.isDigit(str.charAt(i))){
 4             return false;
 5         }
 6     }
 7     return true;
 8 }
 9 
10 public boolean isNumeric(String str){ 
11    Pattern pattern = Pattern.compile("[0-9]*"); 
12    Matcher isNum = pattern.matcher(str);
13    if( !isNum.matches() ){
14        return false; 
15    } 
16    return true; 
17 }

判断字符串是不是手机号

1 public boolean isMobilePhone(String mobiles) {
2     Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
3     Matcher m = p.matcher(mobiles);
4     return m.matches();
5 }

剩余部分https://blog.csdn.net/rockykou/article/details/79951907

转载于:https://www.cnblogs.com/atmlj/p/9831391.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值