如何用“&”创建一个字符串?它的性格? (JAVA) [英] How do I make a string with a " character in it? (Java)

查看:81
本文介绍了如何用“&”创建一个字符串?它的性格? (JAVA)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


推荐答案

你正在寻找一种叫做的东西转义序列,这是一种告诉Java将特定字符解释为默认值之外的其他方式的方法。在您的情况下,您可以通过在其前面添加斜杠来创建包含双引号的Java字符串:

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\")";

Java中有许多其他转义序列。例如, \\ 代表斜杠字符本身(而不是另一个转义序列的开头!); \'代表单引号;和 \ n 代表换行符。还有很多其他的;有关详细信息,请参阅Java参考。

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.

这篇关于如何用“&”创建一个字符串?它的性格? (JAVA)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆