0

I have a string which has the value String s="D\"Souza"

Now before sending this i want to replace \" with \\u022 as a requirement. How to do this ?

    public static String escapeJS(String string) {
        String escapes[][] = new String[][]{
                {"\\", "\\\\"},
                {"\"", "\\u0022"},
                {"\n", "\\n"},
                {"\r", "\\r"},
                {"\b", "\\b"},
                {"\f", "\\f"},
                {"\t", "\\t"}
        };
        for (String[] esc : escapes) {
            string = string.replace(esc[0], esc[1]);
        }
        return string;
    }
}

But here after escaping when we pass this to a library the string we get conatins u0022 instead of quotes.

Any suggestions how to use the escaping correctly.

Note the library does not process correctly( basically a bug in it) \" and hence we try to use \\u0022

buddy
  • 805
  • 1
  • 15
  • 30
  • 1
    You could define it as `char c = '\u0022'` and use the value as a character. – SomeJavaGuy Aug 09 '16 at 09:47
  • 3
    If the library doesn't accept `\"` it won't accept the `\\u0022` version either. The bytecode generated is in fact identical for the two. – aioobe Aug 09 '16 at 09:49
  • 2
    `\u0022` is equivalent to `"`, so `\"` is written as `\\u0022`. Keep in mind that [the unicode translation takes place before any other lexical analysis](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed/30727799#30727799), so just writing `\u0022` would actually terminate the string literal if you don't put a ``\`` in front of it. – aioobe Aug 09 '16 at 09:56
  • The library accepts \" but it interprets wrongly, basically there is a bug in it, but it accepts \u0022 properly and parses correctly hence we are here – buddy Aug 09 '16 at 10:03
  • Yes @aioobe we put \\u0022 as you suggested. – buddy Aug 09 '16 at 10:05
  • I've read your code, and if I understand you correctly, you want the string containing `abc"def` to be translated to `abc\u0022def`, and [it does](http://ideone.com/tDsguO). So, either I don't understand you correctly (if so, please clarify) or you misunderstood how your code works. – aioobe Aug 09 '16 at 10:11
  • If you already depend on Commons Lang you could use [`StringEscapeUtils.escapeJava`](https://commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/apache/commons/lang/StringEscapeUtils.html#escapeJava%28java.lang.String%29) and, if need be, the corresponding `unescapeJava`. – aioobe Aug 09 '16 at 10:14
  • yes it gets replaced as a string abc\u0022def , its interpreted as 6 character string in the library while, we just need this to be interpreted as a single character in the library say as ". But its interpreted litreally as 6 charactered \u0022. That's the problem here. – buddy Aug 09 '16 at 10:14
  • If we use StringEscapeUtils.escapeJava quotes get escaped as \" but we dont want that type of escaping we want the \u0022 conversion. – buddy Aug 09 '16 at 10:17
  • Ok. Well, `\u0022` is not a "magical extra character" that represents `"` but is not `"`. If the library can't handle `"` in the input, replace `"` with another character, such as `_` or something. (Besides, `"\t"` is one character, while `"\\t"` is two, so I don't see how the other ones work either.) Have a look at the example in [this answer](http://stackoverflow.com/questions/30727515/why-is-executing-java-code-in-comments-with-certain-unicode-characters-allowed/30727799#30727799) and you'll understand that `\u...` notation is only relevant before lexical analysis. `\u0022` *is* `"`. – aioobe Aug 09 '16 at 10:17

0 Answers0