How to add " " quotes around printed String?

91,904

Solution 1

Because double quotes delimit String values, naturally you must escape them to code a literal double quote, however you can do it without escaping like this:

System.out.println('"' + s + '"');

Here, the double quote characters (") have been coded as char values. I find this style easier and cleaner to read than the "clumsy" backslashing approach. However, this approach may only be used when a single character constant is being appended, because a 'char' is (of course) exactly one character.

Solution 2

As quotes are used in the Java source code to represent a string, you need to escape them to create a string that contains a quote

 System.out.println("\""+s+"\"");

Solution 3

You must escape the quotes: \"

Solution 4

Assuming that by "Inverted" quotes you meant "Left" and "Right" specific quotation marks, you could do it like this:

System.out.println('\u201C'+s+'\u201D'); // Prints: “s”
System.out.println('"'+s+'"');           // Prints: "s"

Solution 5

If you are really looking for inverted quotes, use this:

System.out.println('\u201C' + s + '\u201D');

It'll output “hi”, not "hi".

You need to have a font installed, though, that supports this, otherwise you might get a box or something instead. Most Windows fonts do.

Share:
91,904
user2150249
Author by

user2150249

Updated on July 09, 2022

Comments

  • user2150249
    user2150249 almost 2 years

    I want to print inverted quotes in java. But how to print it?

    for(int i=0;i<hello.length;i++) {
        String s=hello[i].toLowerCase().trim();
        System.out.println(""+s+"");
    }
    

    expected OP: "hi".....

  • Stephen C
    Stephen C about 11 years
    s/must/could/p .... there are other alternatives