Replace #
with \u0023
in a Java String which looks like below:
{subjectCategory:"s123", subjectId:"111222333", content:"test #comment999", ownerId:"111", ownerName:"tester"}
String.replace("#","\\u0023");
I've tried the above function, but it doesn't seem to work.
You need to apply the
replace
on the string instance you want to replace, not the static method inString
:You need to escape the backslash with another backslash:
Test:
Don't forget to assign to a variable:
Probably, you expect to use same string after
replace()
call. But, strings are immutable, so a new string will be created withreplace()
call. You need to use it, so usetoUse
variable.Note: As said in comments, you can also use old variable again, instead of declaring new one. But ensure to assign result of
replace
call to it: