Close

Java - How to replace a String between two substrings?

[Last Updated: May 22, 2018]

Java String Manipulation Regex Java 

Following code shows how to replace a part of input string between two given substrings (recursively). We are doing that in two ways: with Java Regex and without regex.

package com.logicbig.example;

import java.util.regex.Pattern;

public class ReplaceBetween {
  public static String replaceBetween(String input,
                                      String start, String end,
                                      String replaceWith) {
      return replaceBetween(input, start, end, false, false, replaceWith);
  }

  public static String replaceBetween(String input,
                                      String start, String end,
                                      boolean startInclusive,
                                      boolean endInclusive,
                                      String replaceWith) {
      start = Pattern.quote(start);
      end = Pattern.quote(end);
      return input.replaceAll("(" + start + ")" + ".*" + "(" + end + ")",
              (startInclusive ? "" : "$1") + replaceWith + (endInclusive ? "" : "$2"));
  }

  //without regex
  public static String replaceBetweenWithoutRegex(String str,
                                                  String start, String end,
                                                  boolean startInclusive,
                                                  boolean endInclusive,
                                                  String replaceWith) {
      int i = str.indexOf(start);
      while (i != -1) {
          int j = str.indexOf(end, i + 1);
          if (j != -1) {
              String data = (startInclusive ? str.substring(0, i) : str.substring(0, i + start.length())) +
                      replaceWith;
              String temp = (endInclusive ? str.substring(j + end.length()) : str.substring(j));
              data += temp;
              str = data;
              i = str.indexOf(start, i + replaceWith.length() + end.length() + 1);
          } else {
              break;
          }
      }
      return str;
  }

  public static void main(String[] args) {
      String input = "<div>this is a <b>good</b> apple</div>";
      System.out.println(input);
      String s = replaceBetween(input, "<b>", "</b>", "big");
      System.out.println(s);
      s = replaceBetween(input, "<b>", "</b>", true, true, "big");
      System.out.println(s);

      String input2 = "there's more than one way to skin a cat";
      System.out.println(input2);
      String s2 = replaceBetween(input2, "more", "skin a", " to ");
      System.out.println(s2);
      s2 = replaceBetween(input2, "more", "skin a", true, true, "no");
      System.out.println(s2);

      System.out.println("-- without regex --");
      replaceBetweenWithoutRegex(input2, "more", "skin a", true, true, "no");
      System.out.println(s2);
  }
}
<div>this is a <b>good</b> apple</div>
<div>this is a <b>big</b> apple</div>
<div>this is a big apple</div>
there's more than one way to skin a cat
there's more to skin a cat
there's no cat
-- without regex --
there's no cat

See Also