views:

937

answers:

3

How do you specify a multiple line string literal in Actionscript 3?

+4  A: 

There is one example from this site: Multi-line strings in Actionscript 3

Because actionscript is based on javascript, you can use the cdata tags.

private var myString:String = ( <![CDATA[

Lorem ipsum dolor sit amet, consectetur adipiscing elit. 
Maecenas dui lacus, sollicitudin nec laoreet a, vestibulum a 
odio. Sed et lorem mauris, non porttitor ligula. Aliquam 
convallis dolor rutrum justo semper nec aliquet orci....

]]> ).toString();
Nick Sonneveld
A: 

wow, very clever ... actually, i think this won't even work in most browser when it comes to JavaScript ...

i just wanted to amend an explanation of what actually happens: AS3 allows inline xml declarations through xml literals (which should be part of E4X) ... what you do, is declare an XML literal and then convert it to a String ... likewise, you could write:

private var myString:String = ( [
"Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
"Maecenas dui lacus, sollicitudin nec laoreet a, vestibulum a",
"odio. Sed et lorem mauris, non porttitor ligula. Aliquam",
"convallis dolor rutrum justo semper nec aliquet orci....",
] ).join("\n");

that would be declaring an Array literal and converting it to a String ...

so in the end, you instruct the flash player to create an XML object with one text node containing your text, and then use the String representation of that object ...

(little side note: it is bad practice to declare String content in your code ... this should be loaded externally at runtime)

back2dos
"it is bad practice to declare String content in your code ... this should be loaded externally at runtime" - why's that? If I have some static string, I don't see why I shouldn't declare it in my code.
Tom
@Tom: the emphasis is on "content" ... it is often necessary to have string keys and identifiers in an app ... however content and functionality should always be seperated for a number of reasons ... if you don't know them, I suggest you ask a question on it, since this is hardly the topic here ... :)
back2dos
A: 

This worked great for me:

private var myString:String = "Lorem ipsum dolor sit amet, consectetur adipiscing elit."+"\n"+ "Maecenas dui lacus, sollicitudin nec laoreet a, vestibulum a";
Nullificator