I'd like to condence the following code into fewer calls to .replace()
. It doesn't look like .replace()
will do this. Am I right or am I just reading the documentation wrong?
public void setBody(String body) {
this.body = body.replace("“", "\"").replace("”", "\"").replace("—", "-").replace("’", "'").replace("‘", "'");
}
You should be able to use body.replace(['"', '—', '‘'], ['\"', '-', "'"])
.
You are right. To solve this, you should create a StringBuilder and go through your string 1 character at a time, adding the character to the stringBuilder if it is correct or replacing if it is wrong.
public String replace(CharSequence target, CharSequence replacement)
? As far as I understand from javadocs, your code would be equal tobody.replace("“—‘", "\"-'")
, which is not what is wanted - hyde 2013-01-11 12:49