• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to include quotes (") inside String?  RSS feed

 
Paul Drallos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to generate a String that will be enclosed by quotes (") on each end but I want the quotes to be part of the string. Conceptually, I'm trying to do something like:

String str = ""This is a string"";

so that System.out.println(str) would produce the output:
"This is a string" (quotes are printed).

I tried something like this:

Byte[] bytes = new Byte[1];
bytes[0] = 00100011; // I think this is the correct unicode for "
String quote = new String(bytes);
String str = new String(quote + "This is a string" + quote);

But I can't get this to compile. I keep getting an error saying that it can't find the symbol String(bytes[]). Maybe I'm not creating the byte array properly.

Does anyone know how to do this?

Thanks
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to escape the double quotation with a slash
String hi = "\"Hello\"";

And please set you name to a valid name. Read the following instructions :
http://www.javaranch.com/name.jsp
 
Paul Drallos
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much for the help. I read all through the String section of the Sun Java 2 Tutorial and didn't see anything about using \" for a quote. After your reply, I looked through it again and still don't see it mentioned there.

Thanks
[ August 08, 2006: Message edited by: Paul Drallos ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Paul Drallos:
...I read all through the String section of the Sun Java 2 Tutorial and didn't see anything about using \" for a quote...

Escape sequences (\+char) apply to characters, so the tutorial covers them under the characters section.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!