简体   繁体   English

Java中无效的转义序列

[英]Invalid Escape Sequence in Java

When I create this String : 当我创建这个String

private String chars = " `~1!2@3#4$5%6^7&8*9(0)-_=+qQwWeErRtTyYuUiIoOpP[{]}\|aAsSdDfFgGhHjJkKlL;:'"zZxXcCvVbBnNmM,<.>/?";

Eclipse tells me: "Invalid escape sequence (valid ones are \\b \\t \\n \\f \\r \\" \\' \\ )" Eclipse告诉我:“无效的转义序列(有效的是\\ b \\ t \\ n \\ f \\ r \\“ \\'\\)”

How do I fix this? 我该如何解决?

The \\ is an escape character. \\是转义字符。 You're basically escaping | 您基本上是在逃避| which doesn't need to be escaped at all. 根本不需要逃脱。 If you want to represent an \\ in String, then you need to let it escape itself. 如果要在字符串中表示\\ ,则需要让它自己转义。

private String chars = " `~1!2@3#4$5%6^7&8*9(0)-_=+qQwWeErRtTyYuUiIoOpP[{]}\\|aAsSdDfFgGhHjJkKlL;:'\"zZxXcCvVbBnNmM,<.>/?";

Please note that the " does need to be escaped, otherwise the string value ends too early and the code still won't compile due to all the odd characters thereafter. 请注意, " 确实需要进行转义,否则将字符串值结束太早,代码仍然不会编译由于所有其后奇怪的字符。

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

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