The unescape() method

By default, the raw string literals don't interpret the escape sequences. However, what if you want them to do so? When used with a raw string, the unescape() method will match the sequence of the characters following with the sequence of Unicode escapes and escape sequences, as defined in the Java Language Specifications (JLS). If a match is found, the escape sequence will not be used as a regular combination of letters; it will be used as an escape sequence.

The following code doesn't interpret as a newline escape sequence:

System.out.print("eJava"); 
System.out.print("\n");             
System.out.print("Guru"); 

The output of the preceding code is as follows:

eJava
Guru 

However, the following code will interpret as a newline escape sequence:

System.out.print("eJava"); 
System.out.print(`
`.unescape());      // Don't ignore the escape char 
System.out.print("Guru"); 

The output of the preceding code will provide the eJava and Guru string values on separate lines, as follows:

eJava 
Guru 

When interpreted as escape sequences, a combination of letters that is used to represent them, is counted as a control character of the length 1. The output of the following code will be 1:

System.out.print(`
`.unescape().length());

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.122.11