Replace "\'" with any other character with String's replace()

java, replace, special-characters, string

Solution

Are you sure of the value of `s`? `'` isn't a meaningful escape character, so if you write `String s = "t'est\'"`, the value of `s` will just be `"t'est'"`. To include the additional `\` character, you need to escape it by writing `String s = "t'est\\'"`. Then, I think `"\\\\'"` would be the regular expression to use to find it.

Problem

I can't do a simple operation with String, replace \' with *. Example: `t'est\'` -> `t'est*` I have tried with replace and replaceAll methods: String s has the value: `"t'est\'"`; ``` s.replaceAll("\'", "*"); -> result: t*est* s.replaceAll("\\'", "*"); -> result: t*est* s.replaceAll("\\\'", "*"); -> result: t*est* s.replaceAll("\\\\'", "*"); -> result: t'est' s.replace("\'", "*"); -> result: t'est' s.replace("\\'", "*"); -> result: t'est' s.replace("\\\'", "*"); -> result: t'est' s.replace("\\\\'", "*"); -> result: t'est' ``` But I don't get the result t'est* in any case.

Original source