gpt4 book ai didi

java - Java 中的 Unicode 转义语法

转载 作者:行者123 更新时间:2023-12-02 02:28:20 25 4
gpt4 key购买 nike

在 Java 中,我了解到以下语法可用于提及不在键盘上的 Unicode 字符(例如非 ASCII 字符):

(\u)(u)*(HexDigit)(HexDigit)(HexDigit)(HexDigit)

我的问题是:上述语法中 (u)* 的用途是什么?

我理解的在 Java 中代表日元符号的一个用例是:

char ch = '\u00A5';

最佳答案

有趣的问题。 Section 3.3 of the JLS says :

UnicodeEscape:
\ UnicodeMarker HexDigit HexDigit HexDigit HexDigit

UnicodeMarker:
u
UnicodeMarker u

翻译为\\u+\p{XDigit}{4}

If an eligible \ is followed by u, or more than one u, and the last u is not followed by four hexadecimal digits, then a compile-time error occurs.

所以你是对的,反斜杠后面可以有一个或多个u。原因如下:

The Java programming language specifies a standard way of transforming a program written in Unicode into ASCII that changes a program into a form that can be processed by ASCII-based tools. The transformation involves converting any Unicode escapes in the source text of the program to ASCII by adding an extra u - for example, \uxxxx becomes \uuxxxx - while simultaneously converting non-ASCII characters in the source text to Unicode escapes containing a single u each.

This transformed version is equally acceptable to a Java compiler and represents the exact same program. The exact Unicode source can later be restored from this ASCII form by converting each escape sequence where multiple u's are present to a sequence of Unicode characters with one fewer u, while simultaneously converting each escape sequence with a single u to the corresponding single Unicode character.

所以这个输入

 \u0020ä

变成了

 \uu0020\u00e4

第一个 uu 在这里表示“这是一个 unicode 转义序列”,而第二个 u 表示“自动工具将非 ASCII 字符转换为unicode 转义。”

当您想要从 ASCII 转换回 unicode 时,此信息非常有用:您可以恢复尽可能多的原始代码。

关于java - Java 中的 Unicode 转义语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39586321/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com