1

It might sound a basic question for you. But I am stuck here. I want to replace all double quotes of a string with its equivalent Unicode value (" with \u0022).

In C# it is possible. But don't know how to do it in Java.

C# - C# Working snippet

Java -Java Non working snippet

NOTE: In Java I can use \\u0022. But in this case, its escaping the \ not the double quote.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
Chandra Sekhar
  • 18,914
  • 16
  • 84
  • 125
  • 2
    http://stackoverflow.com/questions/4012918/problem-parsing-unicode-escape-in-a-java-6-string-literal Here's an excellent explanation – xiaofeng.li Sep 01 '16 at 05:54
  • Maybe I'm missing something, but the C# snippet posted seems to replace a character with the exact same character but expressed differently? The (unicode, ascii) character `"` is `\u0022`. – clstrfsck Sep 01 '16 at 06:00
  • @msandiford, any way to do the same in java? – Chandra Sekhar Sep 01 '16 at 06:02
  • 2
    Yes, but why? The resulting string has *exactly* the same contents. – clstrfsck Sep 01 '16 at 06:03
  • There is a limitation in libjson library we are using in server side (Written in C, C++). So when we send a request from Java, they are expecting \u0022 instead of ". – Chandra Sekhar Sep 01 '16 at 06:05
  • very good point form @LukeLee. So maybe instead use Character to create Unicode... look at my solution below. – PKey Sep 01 '16 at 06:16
  • Ah. Ok. So you want the literal text `\u0022`? Not the actual character represented by unicode code point `0x0022` (ie `"`)? The answer from chrisl08 below covers this. – clstrfsck Sep 01 '16 at 06:17
  • @msandiford I don' think he wants a literal text - that was proposed and rejected ... – PKey Sep 01 '16 at 06:19
  • @Plirkee OK, then I'm still thinking that this is an fancy way of doing nothing. Try running this code and see what it prints: `System.out.println('\"' == '\u0022');` – clstrfsck Sep 01 '16 at 06:28
  • @msandiford I can understand that. I am doing nothing but just representing in a different way. I have to do it, because of the limitation of libjson library used. – Chandra Sekhar Sep 01 '16 at 06:30
  • @msandiford Nice one - *"fancy way of doing nothing"* ;-) – PKey Sep 01 '16 at 06:39

7 Answers7

2

As explained in the other question:

The problem is that the Unicode replacement is done very early in compilation. Unicode escapes aren't just valid in strings and character literals (as other escape sequences such as \t are) - they're valid anywhere in code. -- Jon Skeet

So "\u0022" is actually equivalent to """, which is syntactically wrong in Java.

This will work:

System.out.println(xyz.replaceAll("\"", ""+'\u0022'));

And if you are only replacing chars:

System.out.println(xyz.replace('\"', '\u0022'));

But, \u0022 is just another form of the " character. If you are after a general solution, most of the characters won't give you this problem in the first place, because they are not messing with the string literals like " does.

Community
  • 1
  • 1
xiaofeng.li
  • 8,237
  • 2
  • 23
  • 30
2

We can't represent the same string with a single Unicode escape. """ same as "\u0022" in java, you can do it in this way

public static void main(String args[])
{
    System.out.println("Hello, World!");
    String xyz = "Hello \"World";
    System.out.println(xyz.replaceAll("\"", "\u005c\u0022"));
}
Eric
  • 193
  • 2
  • 8
1

Here ya go:

public static void main(String args[]){
      String yourJsonString = "Test\"TEST";
      yourJsonString = yourJsonString.replaceAll("\"", "\\\\u0022");
      System.err.println(yourJsonString);
}

Will print Test\u0022TEST

Durgpal Singh
  • 11,481
  • 4
  • 37
  • 49
chrisl08
  • 1,658
  • 1
  • 15
  • 24
1

Try this:

import java.util.*; import java.lang.*;

class Rextester {  
    public static void main(String args[])
    {
        System.out.println("Hello, World!");
        String xyz = "Hello \"World";
        System.out.println(xyz.replaceAll("\"", Character.toString((char)0x0022)));
    } }
PKey
  • 3,715
  • 1
  • 14
  • 39
1

You can also do it like this -

String s = "Hello \"world";    
System.out.println(s.replace('\"', (char) (0x22)));

It is important to represent the char's value as hex value, by adding 0x in front of it.

TDG
  • 5,909
  • 3
  • 30
  • 51
1

No in a java source text " and \u0022 are not only the same, they are identical, as with reading \u0022 is replaced with the corresponding char ".

You could write:

public \u0063lass C {

If you want to write JSON text with the same u-escaping:

s = s.replace("\"", "\\u0022");

However it might very well be that also some JSON reader might recognize that as ". So, maybe:

s = s.replace("\"", "\\\"");

might be more successful.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
-1

One way you can do this:

public class MainTester {

    public static void main(String args[])
    {
        System.out.println("Hello, World!");
        String xyz = "Hello \"World";
        System.out.println(xyz.replaceAll("\"", "\u005c\u0022"));
    }
}

Output:

Hello "World

pidev
  • 131
  • 1
  • 1
  • 10