How to enter quotes in a Java string?

540,301

Solution 1

In Java, you can escape quotes with \:

String value = " \"ROM\" ";

Solution 2

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.

Solution 3

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

Solution 4

\ = \\

" = \"

new line = \r\n OR \n\r OR \n (depends on OS) bun usualy \n enough.

taabulator = \t

Solution 5

In Java, you can use char value with ":

char quotes ='"';

String strVar=quotes+"ROM"+quotes;
Share:
540,301
jimmy
Author by

jimmy

Updated on January 26, 2020

Comments

  • jimmy
    jimmy over 4 years

    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?

  • Duncan Jones
    Duncan Jones about 9 years
    Down-vote from me. This answer doesn't help address the original question any more thoroughly than the existing answers posted a year before this.
  • Duncan Jones
    Duncan Jones about 9 years
    Your code doesn't compile, you forgot the variable name. Also, your code is needlessly complex. This would do fine: String foo = quotes + "ROM" + quotes;. No need for all those extra "".
  • Stephen C
    Stephen C about 7 years
    This does not answer the question any better that the accepted answer. Please don't go adding answers to old questions like this. It doesn't help.