ReplaceAll and & quot; does not replace

Can someone tell me how the first if it works, and the second not? I am puzzled why the second if-clause is not working. I would like a tip, thanks.

String msg = o.getTweet();
        if (msg.indexOf("&") > 0) {
            msg = msg.replaceAll("&", "&");// vervangt & door &
        }
        if (msg.indexOf(""") > 0) {
            msg = msg.replaceAll(""", "aa"); //vervangt " door "
        }
+5
source share
2 answers

Because it ZEROis a very valid index. Try it,

    String msg = o.getTweet();
    if (msg.indexOf("&") != -1) {
        msg = msg.replaceAll("&", "&");// vervangt & door &
    }
    if (msg.indexOf(""") != -1) {
        msg = msg.replaceAll(""", "aa"); //vervangt " door "
    }

Explanation:

The documentation String.indexOf(String str)explains that "if a string argument occurs as a substring inside this object, then the index of the first character of the first such substring is returned if it does not occur as a substring, 1". - [link to documents]

, : OpenSauce .

msg = msg.replace("&", "&").replace(""", "\"");

:

+16

, , replace replaceAll , . , replace replaceAll - , , .

msg = msg.replace("&", "&").replace(""", "\"");

, replace , . replace replaceAll , arg .

+10

All Articles