I would like to replace this code with a more efficient one:
String s = "The brown fox is jumping over a fence";
s = s.replace(s, "brown", "black");
s = s.replace(s, "fox", "dog");
s = s.replace(s, "fence", "pond");
System.out.println(s);
The above code is running though the long text 3 times and creating new strings every time. Is there some library that can do the same job with only one iteration through the long text.
I would use StringBuilder, go through the text char by char and try to find one of the search phrases and then try to efficiently create a new text.
But I don’t want to implement myself, I’m looking for a already existing library method for this. (Apache, Google, ….)
Thank you.