18

Let's say we have something like:

&firstString=someText&endString=OtherText

And I would like to replace "someText" with something else. What is the best way to do this considering the fact that I do not know what someText might be (any string) and all I know is that it will be surrounded with &firstString= and &endString=

Edit: sorry looks like this is not clear enough. I do not know what "someText" might be, the only information I have is that it will be between &firstString= and &endString=

I was thinking about using split multiple times but it sounded ugly ..

anubhava
  • 761,203
  • 64
  • 569
  • 643
user220755
  • 4,358
  • 16
  • 51
  • 68

6 Answers6

26

You can use String#replaceAll that has support for regex like this:

String newstr = str.replaceAll("(&firstString=)[^&]*(&endString=)", "$1foo$2");
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • 2
    public static String replaceBetween(String theString, String value, String open, String close) { return theString.replaceAll("(" + open + ")[^&]*(" + close + ")", "$1" + value + "$2"); } – markthegrea Sep 07 '18 at 19:20
  • What would be the time performance as compared to using sed or perl commands to replace the string? – Abhishek Attri Jan 15 '19 at 09:01
  • 1
    This is Java question so obviously there is no comparison with Unix tools – anubhava Jan 15 '19 at 10:28
5

The easiest to understand way to do it is to search for the delimiters, and cut out a substring between their positions, like this:

String str = "&firstString=someText&endString=OtherText";
String firstDelim = "&firstString=";
int p1 = str.indexOf(firstDelim);
String lastDelim = "&endString=";
int p2 = str.indexOf(lastDelim, p1);   // look after start delimiter    
String replacement = "quick_brown_fox";
if (p1 >= 0 && p2 > p1) {
    String res = str.substring(0, p1+firstDelim.length())
               + replacement
               + str.substring(p2);
    System.out.println(res);
}
Greg Valcourt
  • 691
  • 6
  • 13
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
3
yourString.replace("someText", "somethingElse");

EDIT based on clarification

String x = "&firstString=someText&endString=OtherText";
int firstPos = x.indexOf("&firstString=") + "&firstString=".length();
int lastPos = x.indexOf("&endString", firstPos);
String y = x.substring(0,firstPos) + "Your new text" + x.substring(lastPos);
System.out.println(y);

Output:

&firstString=Your new text&endString=OtherText
Greg Valcourt
  • 691
  • 6
  • 13
dcp
  • 54,410
  • 22
  • 144
  • 164
1

As I understood the question, you should do something like this, but I'm not sure I totally got what you asked:

yourString = firstString + replacingString + endString;

If you want the "replaced" string:

replacedString = wholeString.substring(0, wholeString.lastIndexOf(endString) - 1);
replacedString = tempString.substring(firstString.length() + 1);
İsmet Alkan
  • 5,361
  • 3
  • 41
  • 64
0

This solution checks that we're not going of the end of the string and will do it for multiple occurrences.

 int startIndex = text.indexOf(startDelim);
 while (startIndex > -1)
 {
    int endIndex = text.indexOf(endDelim, startIndex);
    String endPart = "";

    if ((endIndex + endDelim.length()) < text.length())
       endPart = text.substring(endIndex + endDelim.length());

    text = text.substring(0, startIndex) + replacementText + endPart;
    startIndex = text.indexOf(startDelim);
 }
hveiga
  • 6,725
  • 7
  • 54
  • 78
Greg Valcourt
  • 691
  • 6
  • 13
-1

just replacing the whole &firstString=someText& since this includes the outer barriers which I assume to be URL related?

Omnaest
  • 3,096
  • 1
  • 19
  • 18