Java String replace not working

41,770

Solution 1

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());

Solution 2

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

Solution 3

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

String delimiter = "**";

Solution 4

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());

Solution 5

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.

Share:
41,770

Related videos on Youtube

Vilius
Author by

Vilius

Updated on July 09, 2022

Comments

  • Vilius
    Vilius almost 2 years
    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?

    • CoolBeans
      CoolBeans about 13 years
      How do you know it's not working? You are not printing or storing it anywhere.
  • Vilius
    Vilius about 13 years
    Just tested, but not seccessfully.. The new strings just wont be replaced. --Edit: went wonderfull, after I have adjusted my delimiter.
  • Vilius
    Vilius about 13 years
    Thanks a lot - I had to merge you hint with the one of Yishai.
  • user102008
    user102008 about 13 years
    those two examples do the exact same thing
  • codingscientist
    codingscientist over 10 years
    This is correct answer.
  • Muhammad Babar
    Muhammad Babar almost 10 years
    Immutability was the culprit!
  • withoutOne
    withoutOne over 4 years
    String longitudeString=siteResult.getString("longitude").toString()‌​.replaceAll(" ",""); this is a jdbc example. there are not working "replace or replaceAll"
  • Yishai
    Yishai over 4 years
    @withoutOne Then you have a different problem. Your code will result in longitudeString to not having any spaces in it.
  • withoutOne
    withoutOne over 4 years
    @Yishai I found problem, this not a whitespace, this will tab-space '\t'. but i dont know about "..." replace.

Related