| by Arround The Web | No comments

Escaping characters in Java | Explained

An escape sequence indicates an alternative interpretation of a series of characters. In Java, a character before a backslash (\) is an escape sequence. It is utilized to perform actions, including inserting the characters on the new line, carriage return, and inserting a tab between string characters. It also provides the facility to represent the characters that have some special meaning in double quotes and so on.

This write-up will state about the escaping characters in Java.

What are Escaping Characters in Java?

Java provides the facility to utilize the escape characters to signal an alternative interpretation of a series of characters. Java provides the facility to precede the characters by using the backslash. The compiler of Java takes an escape sequence as a single character which has some special meaning. There are various escape characters are available in Java listed below:

How to Utilize the Escaping Characters in Java

To use the escaping characters in Java, check out the following examples stated below:

Example 1: Use of \t

\t” is used to insert a tab between the two words in a defined string. To do so, we will define a string variable and pass a string as the value of the variable:

String str1 = "Linuxhint\t TSL LTD UK";

 
Invoke the “println()” method and pass the defined string variable as the argument of this method to display the output on the console:

System.out.println(str1);

 
It can be observed that the tab is inserted successfully between the string words:


Example 2: Use of \’

This “\’” escape character splits the Java String into different parts by inserting a single quote. For practical purposes, make a string, and set the “\’” in the string words to separate the words. Then, store this string in a variable:

String str1 = "Linuxhint \' TSL LTD UK \' Lh LTD UK";

 
Invoke the “println()” method to display the result on the screen:

System.out.println(str1);

 
As a result, the string words are separated with the single quote:


Example 3: Use of \n

The “\n” escape character is utilized to move the carriage down in the new blank line. For that purpose, the “\n” is set in between the string words:

String str1 = "Linuxhint \n TSL LTD UK \n Lh LTD UK";

 
Then, print the output on the console with the help of the “println()” method:

System.out.println(str1);

 
Output


Example 4: Use of \”

The \” escape character is used to insert double quotes in the Java String. For demonstration, double quotes are added around the TSL LTD UK:

String str1 = "Linuxhint \" TSL LTD UK \" Lh LTD UK";
System.out.println(str1);

 

Example 5: Use of \r

The “\r” is used to move the carriage left in the new line. For that purpose, we will define a string and store it in a variable. Inside the string, we will set the “\r” to move the text to the new blank line:

String str1 = "Linuxhint \r TSL LTD UK \r LH";
System.out.println(str1);

 

Example 6: Use of \\

The “\\” is utilized to put a backslash in the string. To do so, the “\\” is placed between the words:

String str1 = "Linuxhint \\ TSL LTD UK";

 
Now, invoke the “println()” method to show the result on the console:

System.out.println(str1);

 
Output


Example 7: Use of \f

For moving to a new page from a certain point in a string while printing, the “\f” escape is used. A small example is shown below:

String str1 = "Linuxhint \f TSL LTD UK \f LH";

 
Then, invoke the “println()” method to show the result on the screen:

System.out.println(str1);

 

That’s all about the escaping characters in Java.

Conclusion

There are various escape characters used in Java, including “\t”, “\’”, “\n”, “\’’”, “\r”, “\\”, “\f”, etc. For example, the “\t” inserts the tab between the string words, and “\n” moves the cursor to the new line. This post has stated most known escape characters in Java.

Share Button

Source: linuxhint.com

Leave a Reply