如何在同一字符串中使用单引号和普通引号?

问题描述 投票:0回答:2

我正在一个项目中,您在其中输入输入句子,并且我需要能够在句子中使用“和”,例如Input =“我说,”你好吗?“打印(输入),我得到一个错误。如果有人知道如何解决这个问题,那就太好了。

lua
2个回答
2
投票

请参见https://www.lua.org/pil/2.4.html。 Lua具有非常有趣的功能,可以使用方括号声明字符串:

input = [[I said, "Hi what's up?"]]
input = "I said, \"Hi what's up?\""
input = 'I said, "Hi what\'s up?"'

0
投票

除了上面@Darius所说的之外,我还会讲一些其他内容

[当您尝试在字符串中添加一个加号时,Lua解释器会感到困惑,并在下一个引号之后打断您的字符串,而不会到达行尾。这就是错误的原因。

尝试通过以下代码了解它

str = "Hello I"m somebody" -- here the interpreter will think str equals to "Hello I" at first, and then it will find some random characters after which may make it confused (as m somebody is neither a variable nor a keyword)"

-- you can also see the way it got confused by looking at the highlighted code

--What you can do to avoid this is escaping the quotes
str = "Hello I\"m somebody" -- here the interpreter will treat \" as a raw character (") and parse the rest.

您还可以将转义符()与\'\"\[\n(换行符),\t(制表符)等其他字符一起使用。

© www.soinside.com 2019 - 2024. All rights reserved.