141

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
  • 67,400
  • 29
  • 193
  • 254
jimmy
  • 8,121
  • 11
  • 36
  • 40

10 Answers10

264

In Java, you can escape quotes with \:

String value = " \"ROM\" ";
Ian Henry
  • 22,255
  • 4
  • 50
  • 61
20

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
  • 18,244
  • 7
  • 53
  • 79
15

\ = \\

" = \"

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

taabulator = \t

Oleksandr Samsonov
  • 1,067
  • 2
  • 14
  • 29
  • 5
    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 Mar 18 '15 at 08:50
15

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
  • 27,575
  • 16
  • 91
  • 128
12

Just escape the quotes:

String value = "\"ROM\"";
7

In Java, you can use char value with ":

char quotes ='"';

String strVar=quotes+"ROM"+quotes;
codefreaK
  • 3,584
  • 5
  • 34
  • 65
kumar
  • 2,905
  • 5
  • 22
  • 26
  • 2
    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 `""`. – Duncan Jones Mar 18 '15 at 08:55
5

Here is full java example:-

public class QuoteInJava {     

public static void main (String args[])
    {
            System.out.println ("If you need to 'quote' in Java");
            System.out.println ("you can use single \' or double \" quote");
    }
}

Here is Out PUT:-

If you need to 'quote' in Java
you can use single ' or double " quote

enter image description here

Vipul Gulhane
  • 761
  • 11
  • 16
1

Look into this one ... call from anywhere you want.

public String setdoubleQuote(String myText) {
    String quoteText = "";
    if (!myText.isEmpty()) {
        quoteText = "\"" + myText + "\"";
    }
    return quoteText;
}

apply double quotes to non empty dynamic string. Hope this is helpful.

Madan Sapkota
  • 25,047
  • 11
  • 113
  • 117
0

This tiny java method will help you produce standard CSV text of a specific column.

public static String getStandardizedCsv(String columnText){

    //contains line feed ?
    boolean containsLineFeed = false;
    if(columnText.contains("\n")){
        containsLineFeed = true;
    }

    boolean containsCommas = false;
    if(columnText.contains(",")){
        containsCommas = true;
    }

    boolean containsDoubleQuotes = false;
    if(columnText.contains("\"")){
        containsDoubleQuotes = true;
    }

    columnText.replaceAll("\"", "\"\"");

    if(containsLineFeed || containsCommas || containsDoubleQuotes){
        columnText = "\"" + columnText + "\"";
    }

    return columnText;
}
mifthi
  • 151
  • 8
  • result of `columnText.replaceAll("\"", "\"\"");` is ignored - needs to be `columnText = columnText.replaceAll("\"", "\"\"");` – Mic Aug 18 '22 at 06:57
-4

suppose ROM is string variable which equals "strval" you can simply do

String value= " \" "+ROM+" \" ";

it will be stored as

value= " "strval" ";    
  • 3
    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. – Stephen C Apr 13 '17 at 03:37