Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add " " quotes around printed String? [duplicate]

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

like image 722
user2150249 Avatar asked Mar 09 '13 01:03

user2150249


People also ask

How do I print double quotes in print?

\" - escape sequence Since printf uses ""(double quotes) to identify starting and ending point of a message, we need to use \" escape sequence to print the double quotes.

How do you show text in double quotes?

to show double quote you can simple use escape character("\") to show it.


1 Answers

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.

like image 151
Bohemian Avatar answered Nov 06 '22 01:11

Bohemian