17
String delimiter = "\\*\\*";
String html = "<html><head></head><body>**USERNAME** AND **PASSWORD**</body></html>";
Map<String, String> mp = new HashMap<String, String>();
mp.put("USERNAME", "User A");
mp.put("PASSWORD", "B");
for (Entry<String, String> entry : mp.entrySet()) {
  html.replace(delimiter + entry.getKey()+ delimiter, entry.getValue());
}

That should usually replace those both strings, but it does not. Does anyone has an idea?

1
  • 1
    How do you know it's not working? You are not printing or storing it anywhere.
    – CoolBeans
    Jun 2, 2011 at 19:21

6 Answers 6

50

String is immutable, which means that the html reference doesn't change, rather the replace method returns a new String object that you have to assign.

html = html.replace(delimiter + entry.getKey()+ delimiter, entry.getValue());
6
  • Just tested, but not seccessfully.. The new strings just wont be replaced. --Edit: went wonderfull, after I have adjusted my delimiter.
    – Vilius
    Jun 2, 2011 at 19:23
  • This is correct answer. Jan 16, 2014 at 7:00
  • Immutability was the culprit! Aug 13, 2014 at 11:18
  • String longitudeString=siteResult.getString("longitude").toString().replaceAll(" ",""); this is a jdbc example. there are not working "replace or replaceAll"
    – withoutOne
    Jan 22, 2020 at 13:11
  • @withoutOne Then you have a different problem. Your code will result in longitudeString to not having any spaces in it.
    – Yishai
    Jan 22, 2020 at 14:48
6

The replace method returns its result, which you're discarding.

4

You don't need to escape * character. Difference between replace and replaceAll is that replace escapes any regex metacharacters for us automatically:

String delimiter = "**";
1
  • Thanks a lot - I had to merge you hint with the one of Yishai.
    – Vilius
    Jun 2, 2011 at 19:24
1

as said you are discarding the results and replace doesn't take a regex only a literal char sequence to be replaced so you don't need to escape in the delimiter

but replaceAll and replaceFirst do take a regex string (bad design that)

and as an aside it's advisable to use Patter.quote(String) and Matcher.quoteReplacement(String) to ensure no weird things are happening when using regex (it's a bit easier and ensures there's no error in escaping the chars)

here's for when only one occurrence must be replaced

String delimiter = "**";
String html = "<html><head></head><body>**USERNAME** AND **PASSWORD**</body></html>";
Map<String, String> mp = new HashMap<String, String>();
mp.put("USERNAME", "User A");
mp.put("PASSWORD", "B");
for (Map.Entry<String, String> entry : mp.entrySet()) {
  html = html.replace(delimiter + entry.getKey()+ delimiter, entry.getValue());
}

and here's for when multiple occurrences must be replaced

String delimiter = "**";//unescaped because I'm handling that in my replace 
String html = "<html><head></head><body>**USERNAME** AND **PASSWORD**</body></html>";
Map<String, String> mp = new HashMap<String, String>();
mp.put("USERNAME", "User A");
mp.put("PASSWORD", "B");
for (Map.Entry<String, String> entry : mp.entrySet()) {
  html = html.replaceAll(Pattern.quote(delimiter + entry.getKey()+ delimiter), Matcher.quoteReplacement(entry.getValue()));
}
1
  • those two examples do the exact same thing
    – user102008
    Jun 20, 2011 at 9:46
1

Analogy:

If you have an immutable file on your desktop. To make some changes you do a copy and replace. This leads to a state wherein you can not access the old file unless you have a backup.

In the same way in most computer languages like Java, JavaScript, python and C# the replace method does not replace/modify the String. It only operates over the former String and returns a new String with all the changes.

Now if you really want to store the changes you'll need to get the returned String in the same variable (if your situation permits) or in a new variable.

1

String is an immutable type,so we should new a String object to save the new String returned by the replace Method

html = html.replace(delimiter + entry.getKey()+ delimiter, entry.getValue());

Not the answer you're looking for? Browse other questions tagged or ask your own question.