如何在Java中显示\和"?

How to display \ and " in Java?

如何显示字符 \ 和 "?

一个反斜杠 (\) + 和 + "(这是需要的输出)

我得到了 smth 附近但没有 " 。 网站上的答案显示无效答案...

System.out.println('\' and '\"');

我假设这是 Java

//this code
System.out.println("Backslash is \ and quote is \". The end.");

//will print this string into the console:
Backslash is \ and quote is ". The end.

因为\用于将nt等普通字符转换为NEWLINE\n或TAB\t等特殊字符。如果你想打印出一个反斜杠,你不能只在字符串中单独写一个反斜杠,因为 Java 会查看反斜杠后的下一个字符来知道该怎么做。因为 java 需要在反斜杠后添加另一个字符,所以语言规则是,如果你想实际打印反斜杠,你必须在第一个反斜杠后放置 second 反斜杠。如果您想连续打印两个反斜杠,则必须在代码中连续写入四个反斜杠。

//this code
System.out.println("Double Backslash is \\. The end.");

//will print this string into the console:
Double Backslash is \. The end.

因为 " 用于在代码中开始和结束字符串,您还需要一种方法向 Java 指示 "i'm going to write a quote but it's to be printed literally, it doesn't stop the string",为此您在 [=17] 之前=] 带有反斜杠,例如 \"

@Caius Jard 告诉你方法,转义这个字符。

System.out.println("\" + and + "\""); // and is a variable