is supported in part by

July 17, 2008

TextField.text Gotcha: \n becomes \r

Here's a little TextField quirk: when you assign the string "\n" (newline) to a TextField's text variable, ActionScript automatically converts it to a "\r" character. For example,

var t:TextField = new TextField()
t.text = "Hello\nworld";

trace(t.text.indexOf("\r")); // 5
trace(t.text.indexOf("\n")); // -1

So if you're hunting for a "\n" you've added to a text field, you'll need to search for "\r", not "\n". The docs for TextField's text variable actually point out that "\r" is used:

"Lines are separated by the carriage return character ('\r', ASCII 13)."

But the docs fail to mention that "\n" is converted to "\r".

Posted by moock at July 17, 2008 12:52 PM