I am trying to replace the following text in some HTML:
[merchant]
I tried to use myString = myString.replace("\\[merchant\\]", "newText");
The string is not being replaced. Any ideas?
Try this -
String myString = "My Sampel String [merchant]. Another line with [merchant]";
myString = myString.replace("[merchant]", "newText");
System.out.println(myString);
You don't have to use \\
here.
String#replace
uses string literals, not regexes, so you shouldn't escape any special characters like [
or ]
:
myString = myString.replace("[merchant]", "newText");
replace
doesnt use a regular expression. Just use