在Java中,可以使用反斜杠()来转义特殊字符。以下是一些常见的特殊字符转义序列:
例如,要在字符串中插入一个换行符,可以使用以下代码:
String str = "Hello\nWorld"; System.out.println(str);
输出结果为:
Hello World
注意,如果要在字符串中插入一个反斜杠,需要使用两个反斜杠来进行转义:
String str = "Hello\\World"; System.out.println(str);
Hello\World