开发者

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

开发者 https://www.devze.com 2023-03-23 09:01 出处:网络
Well that was pretty much my question, I 开发者_高级运维need to do something like this: String scriptContent = \"print(\"Hello World\")\";

Well that was pretty much my question, I 开发者_高级运维need to do something like this:

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


Use \".

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.


You can use escape character to do this

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号