如何将任何一种白色 space 转换为字符?
How to convert any kind of white space to a char?
我使用 String.strip()
(Java 11) 从字符串中删除尾部和前导白色 spaces。有 25 种不同的 white spaces in a String。我想用这 25 种白色 space.
中的一些来测试我的代码
我有一个代码示例,它将特定类型的白色 space(例如 \u2002
)转换为 char
,然后在字符串中使用它。当我尝试将另一个白色 space 类型(如 \u000A
)转换为 char 时,出现编译器错误。为什么会发生这种情况以及如何解决?
public static void main(String...args){
char chr = '\u2002';//No problem.
//Compiler error :
//Intellij IDEA compiler - Illegal escape character in character literal.
//Java compiler - java: illegal line end in character literal.
chr = '\u000a';
String text = chr + "hello world" + chr;
text = text.strip();
System.out.println(text);
}
您确定您没有看到此错误吗?
error: illegal line end in character literal
像\u000a
这样的转义序列在编译过程的早期就被处理了。 \u000a
被替换为实际的换行符(代码点 10)。
就好像你这样写:
chr = '
';
这就是为什么当我尝试使用 JDK 11.0.8 编译您的代码时,出现“非法行尾”错误。
此早期转换在 Java Language Specification:
中进行了描述
Because Unicode escapes are processed very early, it is not correct to write '\u000a'
for a character literal whose value is linefeed (LF); the Unicode escape \u000a
is transformed into an actual linefeed in translation step 1 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence '\n'
(§3.10.6). Similarly, it is not correct to write '\u000d'
for a character literal whose value is carriage return (CR). Instead, use '\r'
.
我使用 String.strip()
(Java 11) 从字符串中删除尾部和前导白色 spaces。有 25 种不同的 white spaces in a String。我想用这 25 种白色 space.
我有一个代码示例,它将特定类型的白色 space(例如 \u2002
)转换为 char
,然后在字符串中使用它。当我尝试将另一个白色 space 类型(如 \u000A
)转换为 char 时,出现编译器错误。为什么会发生这种情况以及如何解决?
public static void main(String...args){
char chr = '\u2002';//No problem.
//Compiler error :
//Intellij IDEA compiler - Illegal escape character in character literal.
//Java compiler - java: illegal line end in character literal.
chr = '\u000a';
String text = chr + "hello world" + chr;
text = text.strip();
System.out.println(text);
}
您确定您没有看到此错误吗?
error: illegal line end in character literal
像\u000a
这样的转义序列在编译过程的早期就被处理了。 \u000a
被替换为实际的换行符(代码点 10)。
就好像你这样写:
chr = '
';
这就是为什么当我尝试使用 JDK 11.0.8 编译您的代码时,出现“非法行尾”错误。
此早期转换在 Java Language Specification:
中进行了描述Because Unicode escapes are processed very early, it is not correct to write
'\u000a'
for a character literal whose value is linefeed (LF); the Unicode escape\u000a
is transformed into an actual linefeed in translation step 1 (§3.3) and the linefeed becomes a LineTerminator in step 2 (§3.4), and so the character literal is not valid in step 3. Instead, one should use the escape sequence'\n'
(§3.10.6). Similarly, it is not correct to write'\u000d'
for a character literal whose value is carriage return (CR). Instead, use'\r'
.