Skip to main content
December 28, 2008
Answered

A string literal must be terminated before the line break

  • December 28, 2008
  • 2 replies
  • 9588 views
I've just started learning flash so I'm suffering some of the more difficult problems that beginners have. I have copied some code from a tutorial only I seem to be getting problems with strings whenever I use newlines. Even putting:

var quoteContent:String = "Whether
this";

results in this error:
"A string literal must be terminated before the line break"
or
"Syntax error: expecting semicolon before plusassign."

whatever that's supposed to mean..

Does this mean that in CS4 that newlines in strings are not allowed? I've also tried concatenating the string lines together using += but that didn't work and was tedious. Also, adding \n to the end or start of lines doesn't seem to work. Why is this such a big deal for flash?

Only a string without newlines works.
This topic has been closed for replies.
Correct answer
As the error indicated AS3 does not allow line breaks in string assignment and the string must be terminated with a quote on the same line it started. In other words, string can be only on one line.

quote:

Originally posted by: Andrei1
As the error indicated AS3 does not allow line breaks in string assignment and the string must be terminated with a quote on the same line it started. In other words, string can be only on one line.



Thanks Andrei1. So Actionscript 3 cannot do multi-line strings at all. Looking at this further I can see why this happened. The original string had multiple lines in it but no line breaks. I then turned off word wrapping to diagnose the problem. My own line breaks were what caused the issue.

I realise that there couldn't be a simpler problem to work out but it still took a while to diagnose given the unhelpful error message.

Solution: Put all your strings on 1 line and turn on word wrapping by going to the drop down in the top right corner of the actions panel and clicking on word wrap (ctrl+shift+w).
The line numbers will allow you to see whether you have line breaks where you shouldn't.

2 replies

kglad
Community Expert
Community Expert
December 29, 2008
use:

var quoteContent:String = "Whether\nthis";
December 29, 2008
quote:

Originally posted by: kglad
use:
var quoteContent:String = "Whether\nthis";



As I said in my first post I've already done that. Here is my string:

var pageContent:String = "<h1>Welcome to Ocean Row Solo</h1>\n
The official website of Wave Vidmar’s solo ocean rows.\n
<h2>News</h2>NorthAtlantic Solo row, launch set for Spring,\n
2007 WAVE is preparing to launch his\n
North Atlantic ocean row this coming Spring 2007 from Cape Cod, Mass.\n
USA. He'll be rowing for some <b>2-5 months</b>, aiming to land in England.\n
During his row you'll be able to follow along, experience pictures/video/audio\n
from the expedition, get free downloads, and enter for giveaways and prizes.";

As you can see, there are no quotes of any kind, \n has been added to the end of every line. Even the following won't work:

var pageContent:String ="test\r\n
\r\ninfo";

or this:

var pageContent:String ="test\n
\ninfo";

or this

var pageContent:String ="test\r
\ninfo";
Correct answer
December 29, 2008
As the error indicated AS3 does not allow line breaks in string assignment and the string must be terminated with a quote on the same line it started. In other words, string can be only on one line.

quote:

Originally posted by: Andrei1
As the error indicated AS3 does not allow line breaks in string assignment and the string must be terminated with a quote on the same line it started. In other words, string can be only on one line.



Thanks Andrei1. So Actionscript 3 cannot do multi-line strings at all. Looking at this further I can see why this happened. The original string had multiple lines in it but no line breaks. I then turned off word wrapping to diagnose the problem. My own line breaks were what caused the issue.

I realise that there couldn't be a simpler problem to work out but it still took a while to diagnose given the unhelpful error message.

Solution: Put all your strings on 1 line and turn on word wrapping by going to the drop down in the top right corner of the actions panel and clicking on word wrap (ctrl+shift+w).
The line numbers will allow you to see whether you have line breaks where you shouldn't.
kglad
Community Expert
Community Expert
December 28, 2008
flash sees that as two lines of code. the first line is an unterminated string and the 2nd is a malformed string.

use "\r" to indicate a carriage return and use "\n" to indicate a new line. (and make sure your textfield is multiline if you're trying to display multiple lines in a dynamic textfield.)
December 28, 2008
I've added \r\n to the end of every line but I still get the error.

Regarding the second point. I think that the textbox is multiline because if I load it with a lot of text then it auto wraps but maybe that's just because of the word wrap property.

For some reason the textbox isn't visible on the stage. It appears when I test the movie but otherwise is invisible, why is this?