How do I make a string with a " character in it? (Java)

后端 未结 4 962
生来不讨喜
生来不讨喜 2021-01-19 12:47

Well that was pretty much my question, I need to do something like this:

String scriptContent = \"print(\"Hello World\")\";
相关标签:
4条回答
  • 2021-01-19 13:38

    You can use escape character to do this

    String scriptContent = "print(\"Hello World\")";
    
    0 讨论(0)
  • You're looking for something called an escape sequence, which is a way of telling Java to interpret a particular character as something other than what it means by default. In your case, you can make a Java string containing a double-quote by prefixing it with a slash:

    String scriptContent = "print(\"Hello World\")";
    

    There are many other escape sequences in Java. For example, \\ stands for a slash character itself (instead of the start of another escape sequence!); \' stands for a single quote; and \n stands for a newline. There are many others; consult a Java reference for more details.

    0 讨论(0)
  • 2021-01-19 13:50

    Use \".

    String scriptContent = "print(\"Hello World\")";
    
    0 讨论(0)
  • 2021-01-19 13:51

    apache commons has a StringEscapeUtils http://commons.apache.org/lang/api-2.4/org/apache/commons/lang/StringEscapeUtils.html

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