12

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".....

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
user2150249
  • 155
  • 1
  • 1
  • 7
  • 1
    Please search first: http://stackoverflow.com/questions/3844595/how-can-i-make-java-print-quotes-like-hello , http://stackoverflow.com/a/2018583/166390 –  Mar 09 '13 at 01:09
  • The question here asks for **inverted** quotes, which is not a duplicate of the links above. – Markus A. Mar 09 '13 at 01:18

5 Answers5

18

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.

Bohemian
  • 412,405
  • 93
  • 575
  • 722
16

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+"\"");
nos
  • 223,662
  • 58
  • 417
  • 506
4

You must escape the quotes: \"

Mike Dinescu
  • 54,171
  • 16
  • 118
  • 151
3

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"
Cory Kendall
  • 7,195
  • 8
  • 37
  • 64
2

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.

Markus A.
  • 12,349
  • 8
  • 52
  • 116