3

I am working in java and I want to take the following string:

String sample = "This is a sample string for replacement string with other string";

And I want to replace second "string" with "this is a much larger string", after some java magic the output would look like this:

System.out.println(sample);
"This is a sample string for replacement this is a much larger string with other string"

I do have the offset of where the text starts. In this case, 40 and the text getting replaced "string".

I could do a:

int offset = 40;
String sample = "This is a sample string for replacement string with other string";
String replace = "string";
String replacement = "this is a much larger string";

String firstpart = sample.substring(0, offset);
String secondpart = sample.substring(offset + replace.length(), sample.length());
String finalString = firstpart + replacement + secondpart;
System.out.println(finalString);
"This is a sample string for replacement this is a much larger string with other string"

but is there a better way to do this other then using substring java functions?

EDIT -

The text "string" will be in the sample string at least once, but could be in that text numerous times, that the offset would dictate which one gets replaced (not always the second). So the string that needs to get replaced is always the one at the offset.

2
  • Are you just trying to efficiently make "This is a sample string for replacement this is a much larger string with other string" or are you looking for a way to replace a particular instance of "string" in an unknown string? Commented Sep 11, 2014 at 18:49
  • 1
    To solve the problem "Replace the M characters starting at offset N of a source string with a replacement string", I'd say no--there isn't a better way than substring that I know of. Unless there's something in Apache Commons or some other third-party library. Commented Sep 11, 2014 at 18:50

4 Answers 4

2

One way you could do this..

String s = "This is a sample string for replacement string with other string";
String r = s.replaceAll("^(.*?string.*?)string", "$1this is a much larger string");
//=> "This is a sample string for replacement this is a much larger string with other string"
Sign up to request clarification or add additional context in comments.

Comments

2

Try the following:

sample.replaceAll("(.*?)(string)(.*?)(string)(.+)", "$1$2$3this is a much larger string$5");

$1 denotes the first group captured inside parentheses in the first argument.

Comments

2

Use overloaded version of indexOf(), which takes the starting indes as 2nd parameter:

str.indexOf("string", str.indexOf("string") + 1);

To get the index of 2 string ... and then replace it with this offset ... Hopefully this helps.

Comments

1

You could use

str.indexOf("string", str.indexOf("string") + 1);

instead of your offset, and still use your substring to replace it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.