Android开发之字符串各种判断-StringUtils

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

//使用ASCII码判断
public static boolean isNumeric(String str){
    for(int i=str.length();--i>=0;){
        int chr=str.charAt(i);
        if(chr<48 || chr>57)
            return false;
    }
   return true;
}
//使用正则,(推荐)
public static boolean isInteger(String str) {  
        Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$");  
        return pattern.matcher(str).matches();  
}
//使用正则
public final static boolean isNumeric(String s) {
    if (s != null && !"".equals(s.trim()))
        return s.matches("^[0-9]*$");
    else
        return false;
}     
判断字符串是否整数
public static boolean isNumeric(String str){
    for (int i = str.length();--i>=0;){  
        if (!Character.isDigit(str.charAt(i))){
            return false;
        }
    }
    return true;
}

public boolean isNumeric(String str){ 
   Pattern pattern = Pattern.compile("[0-9]*"); 
   Matcher isNum = pattern.matcher(str);
   if( !isNum.matches() ){
       return false; 
   } 
   return true; 
}
判断字符串是否纯字母
public boolean isLetter(String str){
    Pattern pattern = Pattern.compile("[a-zA-Z] + "); 
    Matcher isLetter = pattern.matcher(str);
    if( !isLetter.matches() ){
       return false; 
    }
}
判断字符串是否手机号码
public boolean isMobilePhone(String mobiles) {
    Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
    Matcher m = p.matcher(mobiles);
    return m.matches();
}
判断提取字符串中手机号码
private static String checkNum(String num){
      if(num == null || num.length() == 0){return "";}
      Pattern pattern = Pattern.compile("(?<!\\d)(?:(?:1[358]\\d{9})|(?:861[358]\\d{9}))(?!\\d)"); 
      Matcher matcher = pattern.matcher(num); 
      StringBuffer bf = new StringBuffer(64); 
      while (matcher.find()) { 
        bf.append(matcher.group()).append(","); 
      } 
      int len = bf.length(); 
      if (len > 0) { 
        bf.deleteCharAt(len - 1); 
      } 
      return bf.toString();
 } 
判断字符串,手机号码隐藏中间四位
public static String getSafePhone(String phone){
     if(phone == null || phone.length() == 0return;
     String phoneNumber = phone.replaceAll("(\\d{3})\\d{4}(\\d{4})","$1****$2");
     return phoneNumber;
}
判断字符串,银行卡号隐藏中间八位
public static String getSafeCardNum(String bankCard){
        if(bankCard == null || bankCard.length() == 0return;
        int hideLength = 8;//替换位数
        int sIndex = bankCard.length()/2 - hideLength/2;
        String replaceSymbol = "*";
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i<bankCard.length();i++){
            char number = bankCard.charAt(i);
            if (i >= sIndex-1 && i<sIndex+hideLength){
                sBuilder.append(replaceSymbol);
            }else {
                sBuilder.append(number);
            }
        }
        return sBuilder.toString();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值