Question

How can I use a quote " character within a string?

Answer

When we use literal strings in Java, we use the quote (") character to indicate the beginning and ending of a string. For example, to declare a string called myString, we could this :-

String myString = "this is a string";

But what if we wanted to include a quote (") character WITHIN the string. We can use the \ character to indicate that we want to include a special character, and that the next character should be treated differently. \" indicates a quote character, not the termination of a string.

public static void main (String args[])
{
        System.out.println ("If you need to 'quote' in Java");
        System.out.println ("you can use single \' or double \" quote");
}

This allows us to include quote characters within a string. Its not something you'll need to do often, but when you need it, this tip will come in handy!


Back