2

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?

6
  • What does That isn't working mean? Commented Jun 7, 2015 at 20:29
  • The String is not being replaced Commented Jun 7, 2015 at 20:31
  • Please show us the string. Commented Jun 7, 2015 at 20:31
  • Also, what does replace do? Commented Jun 7, 2015 at 20:32
  • returns a new string that replaces all instances of the first parameter in this string with the second parameter. Commented Jun 7, 2015 at 20:37

3 Answers 3

3

replace doesnt use a regular expression. Just use

myString = myString.replace("[merchant]", "newText");
Sign up to request clarification or add additional context in comments.

1 Comment

I will accept your answer momentarily, thanks
1

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.

Comments

1

String#replace uses string literals, not regexes, so you shouldn't escape any special characters like [ or ]:

myString = myString.replace("[merchant]", "newText");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.