问题
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