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

后端 未结 4 997
生来不讨喜
生来不讨喜 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:50

    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.

提交回复
热议问题