简体   繁体   English

双引号转义Java

[英]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 " ). 我想用其等效的Unicode值( " with " )替换字符串的所有双引号。

In C# it is possible. 在C#中是可能的。 But don't know how to do it in Java. 但是不知道如何用Java做到这一点。

C# - C# Working snippet C# -C#工作片段

Java - Java Non working snippet Java- Java非工作片段

NOTE: In Java I can use \\" . 注意:在Java中,我可以使用\\" 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替换是在编译的早期完成的。 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. Unicode转义不仅在字符串和字符文字中有效(与其他转义序列,例如\\ t一样),它们在代码中的任何地方均有效。 -- Jon Skeet 乔恩·斯基特

So """ is actually equivalent to """ , which is syntactically wrong in Java. 因此, """实际上等效于""" ,这在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. 我们无法使用单个Unicode转义来表示相同的字符串。 """ same as """ in java, you can do it in this way “”与Java中的“ \\ u0022”相同,您可以通过这种方式进行

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 将打印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. 重要的是,将char的值表示为十六进制值,方法是在其前面添加0x

No in a java source text " and " are not only the same, they are identical, as with reading " is replaced with the corresponding char " . 否一个Java源文本""不仅是一样的,它们是相同的,与读取"被替换为对应的字符"

You could write: 您可以这样写:

public \u0063lass C {

If you want to write JSON text with the same u-escaping: 如果要使用相同的u换码形式编写JSON文本:

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

However it might very well be that also some JSON reader might recognize that as " . So, maybe: 但是,很可能有些JSON阅读器也可能将其识别为" 。因此,也许:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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