This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to include quotes ( Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "How to include quotes (") inside String?" Watch "How to include quotes (") inside String?" New topic
Author

How to include quotes (") inside String?

Paul Drallos
Greenhorn

Joined: Aug 07, 2006
Posts: 3
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

Joined: Nov 24, 2005
Posts: 14672
    
  11

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


[My Blog]
All roads lead to JavaRanch
Paul Drallos
Greenhorn

Joined: Aug 07, 2006
Posts: 3
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: How to include quotes (") inside String?
 
Similar Threads
Byte to string Conversion
ArrayIndexOutOfBoundException while reading the file
how can i convert char [] to unsigned char [] to connect to an app writen in 'C'
BadPaddingException with DES
Subdivide string in bytes to a byte array