1 Commonly used escape characters in Java
Escape characters are often used to represent some special characters in strings. The following are some common application cases:
- Representing a tab character:
\t
, used to represent a tab character in a string to achieve alignment . - Represents a newline character:
\n
, used to represent a newline character in a string. - Represents the backslash symbol itself:
\\
, used to represent a backslash symbol in a string. - Represents a double or single quote:
\"
or\'
, used to represent a double or single quote in a string. - Represents a carriage return character:
\r
, used to represent a carriage return character in a string. -
System.out.println("好好读书\r 教育");
In addition to the above applications, there are other escape characters that can also be used in strings, and the specific application depends on the situation.
public class ChangeChar{
public static void main(String [] args){
System.out.println("福州\t天津\t上海");
System.out.println("jack\nchoclin\ncc");
System.out.println("C:\\Windows\\System32\\cmd.exe");
System.out.println("输出两个斜杆\\\\");
System.out.println("我说:\"要好好学习\"");
System.out.println("我说:\'要好好学习\'");
System.out.println("好好读书\r教育");
}
}
In the console, enter the tab key to complete command
Case:
Requirement: Please use an output statement to achieve the effect of inputting the following graphics
Code:
public class javaTest {
public static void main(String [] args){
System.out.println("书名\t作者\t价格\t销量\n三国\t罗贯中\t120\t1000");
}
}