简体   繁体   中英

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

那几乎是我的问题,我需要做这样的事情:

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

使用\\"

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

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.

您可以使用转义字符来执行此操作

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM