How to enter quotes in a Java string?

后端 未结 10 1121
不知归路
不知归路 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:00

    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.

提交回复
热议问题