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

原文链接:https://blog.csdn.net/yaomingyang/article/details/79273849

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();
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49

2.replace方法替换字符串中指定的字符并且指定替换的个数,区分大小写

    public static String replace(String text, String searchString, String replacement, int max)
    {
      return replace(text, searchString, replacement, max, false);
    }
  • 1
  • 2
  • 3
  • 4

3.replace方法替换字符串中指定的字符串,区分大小写,替换所有匹配到的字符串

    public static String replace(String text, String searchString, String replacement)
    {
      return replace(text, searchString, replacement, -1);
    }
  • 1
  • 2
  • 3
  • 4

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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

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"})
  • 1
  • 2
  • 3
  • 4

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);
    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

7.replaceOnce方法替换掉字符串的子字符串中匹配到的字符串一次

    public static String replaceOnce(String text, String searchString, String replacement)
    {
      return replace(text, searchString, replacement, 1);
    }
  • 1
  • 2
  • 3
  • 4

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);
    }

猜你喜欢

转载自blog.csdn.net/jarniyy/article/details/80429451