user220755
user220755

Reputation: 4446

Replace a String between two Strings

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 ..

Upvotes: 18

Views: 37610

Answers (6)

Greg Valcourt
Greg Valcourt

Reputation: 731

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

Upvotes: 0

dcp
dcp

Reputation: 55434

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

Upvotes: 3

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

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

Upvotes: 5

İsmet Alkan
İsmet Alkan

Reputation: 5447

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

Upvotes: 0

anubhava
anubhava

Reputation: 784998

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

String newstr = str.replaceAll("(&firstString=)[^&]*(&endString=)", "$1foo$2");

Upvotes: 26

Omnaest
Omnaest

Reputation: 3096

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

Upvotes: -1

Related Questions