AS3 has never had very good support for multi-line strings… until now. Today’s article discusses the proper and improper ways of writing multi-line strings and delves into the bytecode so you really understand what’s going on.

Perhaps the most natural way to write a multi-line string would be like this:

var str:String = "A
	B
	C";

But if you know anything about AS3, you’ll know that doesn’t work. You’re greeted with several compiler errors:

/Users/jackson/Desktop/MultiLineString.as:28
Syntax error: A string literal must be terminated before the line break.
			var str:String = "A\n
			                     ^
 
/Users/jackson/Desktop/MultiLineString.as:29
Syntax error: Expecting either a 'semicolon' or a 'new line' here.
				B\n
				  ^
 
/Users/jackson/Desktop/MultiLineString.as:29
Error: Unexpected character. '\' is not allowed here
				B\n
				 ^
 
/Users/jackson/Desktop/MultiLineString.as:29
Error: Access of possibly undefined property B.
				B\n
				^
 
/Users/jackson/Desktop/MultiLineString.as:29
Error: Access of possibly undefined property n.
				B\n
				  ^
 
/Users/jackson/Desktop/MultiLineString.as:30
Syntax error: A string literal must be terminated before the line break.
				C\n";
				     ^
 
/Users/jackson/Desktop/MultiLineString.as:30
Syntax error: Expecting either a 'semicolon' or a 'new line' here.
				C\n";
				  ^
 
/Users/jackson/Desktop/MultiLineString.as:30
Error: Unexpected character. '\' is not allowed here
				C\n";
				 ^
 
/Users/jackson/Desktop/MultiLineString.as:30
Error: Access of possibly undefined property C.
				C\n";
				^
 
/Users/jackson/Desktop/MultiLineString.as:30
Error: Access of possibly undefined property n.
				C\n";
				  ^

Instead, AS3 programmers resort to one of two methods. The first is to concatenate multiple strings with the + operator:

var str:String = "A"
	+ "B"
	+ "C";

The second is to manually concatenate the strings to avoid the runtime expense of string concatenation:

var str:String = "ABC";

This latter approach is to simply avoid multi-line string altogether. But do we need to go to these extremes to avoid a performance problem? As it turns out, the ASC 2.0 compiler is pretty smart when using full release-mode settings:

--verbose-stacktraces=false -debug=false -optimize=true

As an example, consider the following multi-line string:

var str:String = "A"
	+ "B"
	+ "C";

Here’s the bytecode that gets generated by ASC 2.0:

pushstring "ABC"    
coerce_s

Happily, the compiler was smart enough to concatenate the strings at compile time rather than run time.

Now for a word of warning: do not omit the + operators as is allowed in C or C++ code. The strings will not by concatenated. You won’t even get a compiler error or warnings. For example, consider this erroneous attempt at a multi-line string:

var str:String = "A"
	"B"
	"C";

The compiler interprets this as the first line:

var str:String = "A"

Which is alright since semicolons are optional in AS3. Then there are two more statements, one of which has a semi-colon:

"B"
"C";

So the compiler generates the following code (annotations mine):

// Push "A" and save it to the "str" local variable
pushstring "A"
coerce_s
setlocal1
 
// Push "B" and then immediately pop it
pushstring "B"
pop
 
// Push "C" and then immediately pop it
pushstring "C"
pop

Not only is the multi-line string not concatenated properly, but two more strings are properly created, pushed, and popped for absolutely no reason whatsoever.

In summary, this is how to create a multi-line string in AS3. The compiler will concatenate the string for you at compile time.

var str:String = "A"
	+ "B"
	+ "C";

And this is how to not create a multi-line string in AS3. The compiler will not concatenate the string for you at compile time, will not generate code that concatenates the string at run time, and will instead generate a SWF with the unused “B” and “C” strings that pointlessly pushes and pops them on the stack at run time.

///////////////////////
// Note: no + operators
///////////////////////
var str:String = "A"
	"B"
	"C";

Spot a bug? Have a question or suggestion? Post a comment!