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

独自空忆成欢 提交于 2019-12-12 14:42:32

问题


I know that you can assign a multi-line string to a variable like this:

MyVar = 
(
this
is 
a 
string with multiple
lines
)

But is there a way to assign the above string to an object property? I tried doing it like this but I received an error:

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

The error says:

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

I just want to be able to open my script in a text editor and copy and paste multiple-line strings directly into the editor as properties of objects.


回答1:


You have to use the proper assignment operator := with Objects, likewise your text needs be enclosed by Quotes.

Try:

obj := {}

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

MsgBox % obj["key"]

Or you can do this below:

x = 
(
this
is 
a 
string with multiple
lines
)

obj["key"] := x

MsgBox % obj["key"]

You can also build a multi-line object like so:

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

MsgBox % obj["key"]



回答2:


Using a raw multiline string assignment like the following it tends to defeat any indenting you may have cultivated in your script.

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

Although that will work. If you're looking to preserve your code indenting then you can create a multiline string by delimiting the lines with `n like this:

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


来源:https://stackoverflow.com/questions/37558551/is-there-a-way-to-assign-a-multi-line-string-to-a-object-property

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!