I have a string of the format:
"aaa\\u2022bbb\\u2014ccc"
I'd like to display the two special charactes, but to be able to do that, I have to first convert the string to this format:
"aaa\u2022bbb\u2014ccc"
I've tried writing this, but it gives a compilation error:
String encodedInput = input.replace("\\u", "\u");
This has got to be something straightforward, but I just cannot get it. Any ideas?
=>
This because "\" is a special character in a string, so you need to quote it as well. "\" == "\".
In addition to escaping your escapes -- as other people (e.g. barsju) have pointed out -- you must also consider that the usual conversion of the
\uNNNN
notation to an actual Unicode character is done by the Java compiler at compile-time.So even once you sort out the backslash escaping issue, you may very well have have further trouble getting the actual Unicode character to display because you appear to be manipulating the string at run-time, not at compile-time.
This answer provides a method to replace
\uNNNN
escape sequences in a run-time string with the actual corresponding Unicode characters. Note that the method has some TODOs left with regard to error handling, bounds checking, and unexpected input.(Edit: I think the regex-based solutions provided here by e.g. dash1e would be better than the method I linked, as they are more polished with regards to handling unexpected input data).
You need to escape your escapes:
Unfortunately I do not know of a sort of eval.
Try