Java

TOPIC 08 – KEYBOARD INPUT & STRINGS

AP EXTRA

 

 

AP LESSON NOTE

 

 

ESCAPE CHARACTERS

 

You may have wondered how to have a String that itself contains double quotes in it.  Because the quotes denote the start and end of Strings, we cannot simply use quotes inside of Strings like we use any other characters.

 

However, we can include quotes by using the escape character \ (backslash) before the quotes. 

 

EXAMPLE


Consider the following code:

    String s = "Patrick \"The cool guy\" Campeau";      System.out.println(s);

 

It outputs:

 

    Patrick "The cool guy" Campeau

 

LIST OF ESCAPE CHARACTERS

 

There are several escape characters that can be included inside strings.  They all start by using the backslash.

 

\t   Insert a tab in the text at this point.

\b   Insert a backspace in the text at this point.

\n   Insert a newline in the text at this point.

\r   Insert a carriage return in the text at this point.

\f   Insert a formfeed in the text at this point.

\'   Insert a single quote character in the text at this point.

\"   Insert a double quote character in the text at this point.

\\   Insert a backslash character in the text at this point.