添加Unicode \u0022时发生编译时错误 [英] Compile time error while adding unicode \u0022

查看:178
本文介绍了添加Unicode \u0022时发生编译时错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用unicode在字符串中添加双引号,但是这样做时我遇到了编译时间错误。

I'm trying to add double quotes in a string using the unicode, but when I do so i get the compilation time error.

String unicCode = "\u0022"; //This line gives a compile time error.

编译错误是我得到的是

字符串文字没有被双引号正确关闭。

String literal is not properly closed by a double-quote.

有人可以帮助我了解附加双引号()的unicode所需使用的转义字符。

Could some one help me to understand what are the escape characters required to be used to append a unicode of a double quotation mark (").

推荐答案

 public static void main(String[] args) {
        String quote = "\u005c\u0022";
        System.out.println(quote);
    }

输出

"







public static void main(String[] args) {
        String quote = "\u005c\u0022" + "abc" + "\u005c\u0022";
        System.out.println(quote);
    }

输出

"abc"

如果想要将两个双引号字符放入字符串文字中,可以使用常规转义序列来完成。但是您不能使用 Unicode转义,因为Java没有对字符串文字中的Unicode转义提供特殊处理。

If you wanted to put the two double quote chars into the string literal, you can do it with normal escape sequences. But you can't do with Unicode escapes because Java provides no special treatment for Unicode escapes within string literals.

String quote = "\"";

//

// \u0022 具有完全相同的含义对编译器而言,其含义为。

// \u0022 has exactly the same meaning to the compiler as ".

//下面的字符串变为 :后跟一个空字符串

// The string below turns into """: an empty string followed

//由未终止的字符串产生编译错误。

// by an unterminated string, which yields a compilation error.

String quote = "\u0022";

这篇关于添加Unicode \u0022时发生编译时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆