views:

124

answers:

4

I want to initialize a String in Java, but that string needs to include quotes; for example: "ROM"

I tried doing String value = " "ROM" ";, but that doesn't work. How can I include "s within a string?

+9  A: 

Not sure what language you're using (you didn't specify), but you should be able to "escape" the quotation mark character with a backslash: "\"ROM\""

Shaggy Frog
same in Java too lemme try
salman
+4  A: 

In Java, you can escape quotes with \:

String value = " \"ROM\" ";
Ian Henry
What if ROM is not a value, if it is String type Variable i meanString ROM = " Java Programming ";String value "\" ROM\" "; Can i use like this.
salman
String value = "\"" + ROM + "\"";
Shaggy Frog
Thanks to all u guys
salman
+2  A: 

Just escape the quotes:

String value = "\"ROM\"";
+1  A: 

In reference to your comment after Ian Henry's answer, I'm not quite 100% sure I understand what you are asking.

If it is about getting double quote marks added into a string, you can concatenate the double quotes into your string, for example:

String theFirst = "Java Programming";
String ROM = "\"" + theFirst + "\"";

Or, if you want to do it with one String variable, it would be:

String ROM = "Java Programming";
ROM = "\"" + ROM + "\"";

Of course, this actually replaces the original ROM, since Java Strings are immutable.

If you are wanting to do something like turn the variable name into a String, you can't do that in Java, AFAIK.

GreenMatt