Chandra Sekhar
Chandra Sekhar

Reputation: 19492

Double Quote escape Java

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.

Upvotes: 1

Views: 5243

Answers (7)

TDG
TDG

Reputation: 6151

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.

Upvotes: 1

Joop Eggen
Joop Eggen

Reputation: 109547

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.

Upvotes: 1

pidev
pidev

Reputation: 131

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

Upvotes: -1

PKey
PKey

Reputation: 3841

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)));
    } }

Upvotes: 1

Eric
Eric

Reputation: 193

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"));
}

Upvotes: 2

xiaofeng.li
xiaofeng.li

Reputation: 8587

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.

Upvotes: 2

chrisl08
chrisl08

Reputation: 1658

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

Upvotes: 1

Related Questions