str工具

public class Str {
    
    //判断为空
    public static boolean isEmpty(CharSequence cs) {
        return (cs == null) || (cs.length() == 0);
    }
    
    //去空格
    public static String trim(String str) {
        return str == null ? null : str.trim();
     }
    
    //判断字符相等
    public static boolean equals(CharSequence cs1, CharSequence cs2) {
        return cs1 == null ? false : cs2 == null ? true : cs1.equals(cs2);
    }
    
    
    //去最后的一个字符串的最后一个字符
    public static String substringBeforeLast(String str, String separator) {
        if ((isEmpty(str)) || (isEmpty(separator))) {
            return str;
        }
        int pos = str.lastIndexOf(separator);
        if (pos == -1) {
            return str;
        }
        return str.substring(0, pos);
    }
    
    //替换字符
    public static String remove(String str, String remove) {
        if ((isEmpty(str)) || (isEmpty(remove))) {
            return str;
        }
        return replace(str, remove, "", -1);
    }
    //默认字符串
    public static String defaultString(String str) {
        return str == null ? "" : str;
    }
    
    //默认字符串
    public static String defaultString(String str, String defaultStr) {
        return str == null ? defaultStr : str;
    }
    
    public static String replace(String text, String searchString, String replacement, int max) {
        if ((isEmpty(text)) || (isEmpty(searchString)) || (replacement == null) || (max == 0)) {
            return text;
        }
        int start = 0;
        int end = text.indexOf(searchString, start);
        if (end == -1) {
            return text;
        }
        int replLength = searchString.length();
        int increase = replacement.length() - replLength;
        increase = increase < 0 ? 0 : increase;
        increase *= (max > 64 ? 64 : max < 0 ? 16 : max);
        StringBuilder buf = new StringBuilder(text.length() + increase);
        while (end != -1) {
            buf.append(text.substring(start, end)).append(replacement);
            start = end + replLength;
            max--;
            if (max == 0) {
                break;
            }
            end = text.indexOf(searchString, start);
        }
        buf.append(text.substring(start));
        return buf.toString();
    }

    
  public static void main(String[] args) {
     String dd= m.remove("zxcvvvzx","zx");
     System.err.println(dd);
    
  }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值