有没有办法将多行字符串分配给对象属性?

Is there a way to assign a multi-line string to a object property?

我知道您可以像这样将多行字符串分配给变量:

MyVar = 
(
this
is 
a 
string with multiple
lines
)

但是有没有办法将上面的字符串赋给一个对象属性?我尝试这样做,但收到错误消息:

Array := {}
Array["key"] = 
(
this
is 
a 
string with multiple
lines
)

错误说:

The following variable name contains an illegal character
"this
is
a
string"

我只想能够在文本编辑器中打开我的脚本,然后将多行字符串作为对象的属性直接复制并粘贴到编辑器中。

您必须对对象使用正确的赋值运算符 :=,同样,您的文本需要用引号引起来。

尝试:

obj := {}

obj["key"] := 
( 
"this
is 
a 
string with multiple
lines"
)

MsgBox % obj["key"]

或者您可以执行以下操作:

x = 
(
this
is 
a 
string with multiple
lines
)

obj["key"] := x

MsgBox % obj["key"]

您还可以像这样构建多行对象:

obj := {"key": 
(
"this
is 
a 
string with multiple
lines"
)}

MsgBox % obj["key"]

使用像下面这样的原始多行字符串赋值往往会破坏您可能在脚本中培养的任何缩进。

str := {"Lines":
(
"first
second
third"
)}

虽然那会起作用。如果您希望保留代码缩进,则可以通过使用 `n 分隔行来创建多行字符串,如下所示:

str := {"Lines": "first`nSecond`nThird"}