简体   繁体   中英

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 " ).

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 \\" . But in this case, its escaping the \\ not the double quote.

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 """ 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, " 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.

We can't represent the same string with a single Unicode escape. """ same as """ 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"));
}

Here ya go:

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

Will print Test"TEST

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

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.

No in a java source text " and " are not only the same, they are identical, as with reading " 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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM