(十九)Java工具类StringUtils的replace、replaceEach、replaceEachRepeatedly、replaceFirst方法详解

1.replace方法替换字符串中的某一部分字符串
    /**
     * 
     * 方法描述 替换指定字符串方法
     *
     * @param text--字符串
     * @param searchString--被替换的字符串
     * @param replacement--替换字符串
     * @param max 替换字符串searchString的最大个数
     * @param ignoreCase 是否忽略大小写
     * @return
     * 
     * @author yaomy
     * @date 2018年2月6日 下午5:50:28
     */
    private static String replace(String text, String searchString, String replacement, int max, boolean ignoreCase)
    {
      if ((isEmpty(text)) || (isEmpty(searchString)) || (replacement == null) || (max == 0)) {
        return text;
      }
      String searchText = text;
      //如果忽略大小写text、searchString都转换为小写
      if (ignoreCase) {
        searchText = text.toLowerCase();
        searchString = searchString.toLowerCase();
      }
      int start = 0;
      //searchString 的起始索引
      int end = searchText.indexOf(searchString, start);
      if (end == -1) {
        return text;
      }
      //搜索字符串的长度
      int replLength = searchString.length();
      //搜索字符串和替换字符串的差值,如果小于0,字符串增量就为0
      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, start, end).append(replacement);
        start = end + replLength;
        max--; if (max == 0) {
          break;
        }
        end = searchText.indexOf(searchString, start);
      }
      buf.append(text, start, text.length());
      return buf.toString();
    }
2.replace方法替换字符串中指定的字符并且指定替换的个数,区分大小写
    public static String replace(String text, String searchString, String replacement, int max)
    {
      return replace(text, searchString, replacement, max, false);
    }
3.replace方法替换字符串中指定的字符串,区分大小写,替换所有匹配到的字符串
    public static String replace(String text, String searchString, String replacement)
    {
      return replace(text, searchString, replacement, -1);
    }
4.replace方法替换掉所有正则表达式匹配的字符串
    public static String replaceAll(String text, String regex, String replacement)
    {
      if ((text == null) || (regex == null) || (replacement == null)) {
        return text;
      }
      return text.replaceAll(regex, replacement);
    }
5.replaceEach和replaceEachRepeatedly使用数组批量指定要替换的字符串
//暂时未发现两个方法有什么异同
StringUtils.replaceEach(searchText, new String[] {"ni","ma","yao"}, new String[] {"NI","Ma","LL"})
StringUtils.replaceEachRepeatedly(searchText, new String[] {"ni","ma","yao"}, new String[] {"NI","Ma","LL"})
6.replaceFirst方法替换掉第一个匹配正则表达式的子字符串
    public static String replaceFirst(String text, String regex, String replacement)
    {
      if ((text == null) || (regex == null) || (replacement == null)) {
        return text;
      }
      return text.replaceFirst(regex, replacement);
    }
7.replaceOnce方法替换掉字符串的子字符串中匹配到的字符串一次
    public static String replaceOnce(String text, String searchString, String replacement)
    {
      return replace(text, searchString, replacement, 1);
    }
8.replacePattern方法替换掉与正则表达式匹配的子字符串
    public static String replacePattern(String source, String regex, String replacement)
    {
      if ((source == null) || (regex == null) || (replacement == null)) {
        return source;
      }
      return Pattern.compile(regex, 32).matcher(source).replaceAll(replacement);
    }
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值