I'm trying to analyze some text and I need to swap two substring in a string, for example in the following text I wanna swap "nice to see you" and "how are you"
Hi nice to see you? I'm fine Nice! how are you some other text
so the result should be :
Hi how are you? I'm fine Nice! nice to see you some other text
First I wrote this method and works fine for this simple example:
public static String Swap(String source, String str1, String str2) {
source=source.replace(str1, str2);
source=source.replaceFirst(str2, str1);
return source;
}
I need to use this method for more complex texts like the following one but as replaceFirst uses regex it cannot swap using my method.
f(f(x))*g(g(x))
I wanna swap f(x) and g(x), but it won't word.
is there any other way to do this?