How to enter quotes in a Java string?

后端 未结 10 1161
不知归路
不知归路 2020-11-22 05:38

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

String value = \" \"ROM\" \         


        
相关标签:
10条回答
  • 2020-11-22 06:13

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

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

    it will be stored as

    value= " "strval" ";    
    
    0 讨论(0)
  • 2020-11-22 06:17

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

    0 讨论(0)
  • 2020-11-22 06:19

    In Java, you can escape quotes with \:

    String value = " \"ROM\" ";
    
    0 讨论(0)
  • 2020-11-22 06:21

    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.

    0 讨论(0)
提交回复
热议问题