[Super invincible and detailed java notes of Han Shunping] From beginner to proficient---escape characters

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:

  1. Representing a tab character: \t, used to represent a tab character in a string to achieve alignment .
  2. Represents a newline character: \n, used to represent a newline character in a string.
  3. Represents the backslash symbol itself: \\, used to represent a backslash symbol in a string.
  4. Represents a double or single quote: \"or \', used to represent a double or single quote in a string.
  5. Represents a carriage return character: \r, used to represent a carriage return character in a string.
  6. 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");
	}
}

Guess you like

Origin blog.csdn.net/qq_45206556/article/details/131662081