String Literals vs. String Values Posted on Thu Jul 26 2007 in ActionScript by Tim
A common problem that new ActionScript programmers have is understanding when to use "\\" and "\" in strings. The confusion stems from an incomplete knowledge of the difference between string literals and string values. It's not that difficult once you understand what's going on in the compiler and why it's necessary...
A string literal is a sequence of characters between single or double quotes. Compilers use string literals to create string values that are stored in string variables.
When code containing a string literal is compiled, the compiler turns the literal into a string value. In the example below the string literals are converted to string value and assigned to their respective variables.
var s1 = "i am a string literal";
var s2 = 'i am a string literal too';
In most cases you don't have to worry about the difference between literals and values until you want to insert a quotation mark or a special non-printable character (like a newline or a tab) into your string.
You can't just put a newline in the middle of a string in your code. If you've ever done something like the example below, then you know the compiler will complain quite loudly.
var s = "Can't put newline
characters in strings";
Compilers get confused easily so to help the compiler, special sequences called escape sequences were invented to take the place of the troublesome characters to allow them to be represented in string literals.
An escape sequence starts with a special character which signals the compiler to jump (or escape) out of its normal string processing code and treat the next character(s) a little differently. Escape sequences in ActionScript start with a backslash (\) character.
The character(s) that follow the backslash tell the compiler what special character to insert. The following table summarize the valid escape sequences in Flash and the characters they represent.
|Escape Sequence||Character Represented|
|\b||backspace character (ASCII 8)|
|\f||form-feed character (ASCII 12)|
|\n||line-feed character (ASCII 10)|
|\r||carriage return character (ASCII 13)|
|\t||tab character (ASCII 9)|
|\"||double quotation mark|
|\'||single quotation mark|
|\000 .. \377||a byte specified in octal|
|\x00 .. \xFF||a byte specified in hexadecimal|
|\u0000 .. \uFFFF||a 16-bit Unicode character specified in hexadecimal|
Armed with this knowledge, we can fix our example above by using the \n escape sequence instead of pressing the enter key in the middle of our string literal. This makes the compiler much happier.
var s = "Can't put newline\ncharacters in strings";
One unavoidable side effect of making the backslash a special character in string literals is that you can't just use a backslash in a string literal. If you do, the compiler will try to interpret the character following the backslash as part of an escape sequence. This can result in unexpected characters in your string or the compiler could just swallow the character following the backslash.
This example looks like it should work, but the backslash is a special character to the compiler so it swallows up the the P and the A and the trace statement says that path contains c:\rogram Files\wesome, certainly not what we intended.
var path = "c:\Program Files\Awesome";
To get a backslash into a string value you need to use the "\\" escape sequence. Now the output in the trace window says that path contains c:\Program Files\Awesome, exactly what we wanted.
var path = "c:\\Program Files\\Awesome";
Text that you load into a string variable from a file (via loadVariables, loadVars or XML) does not need to contain escape sequences. Input that you get from users via text fields does not need to contain escape sequences. Only string literals, quoted strings in your ActionScript code, need to use escape sequences to create string values.
Leave a Comment
Comments are now closed for this post.