SyntaxScrewer's Blog

May 1, 2010

Newline “\n” problem when reading a text file into a dynamic text field. Why do I see 2 new lines instead of 1 ?

Filed under: ActionScript 3.0,Flash — geekymaira @ 1:40 pm

Hello :),

I was working on something really simple the other day, loading up a text file using the URLLoader.
I didn’t even bother testing my script because I thought there was no scope for errors in something this elementary
but I was wrong!

A friend of mine pointed out that the dynamic text field was showing up “Double Line Spacing” while displaying the text.
I checked and was confused. I thought maybe the text file had changed so I checked it. But it looked alright. So why was
I seeing 2 newlines for every one “\n” that my text file had.

Well here’s why!

Apparantly, when URLLoader read text from my text file and encountered a “\n” , it changed it to a “\r\n”. I’m not sure if URLLoader did that
or if it was stored like that in my text file. Anyway, when the dynamic text encountered a “\r\n” it put in 2 line spaces instead of one. I guess one
for the “\r” and one for the “\n”.

Here’s a trick I found while searching to solve the problem. It works well šŸ™‚

var myTextLoader:URLLoader = new URLLoader();
myTextLoader.load(new URLRequest("temp.txt"));

function onDone(evt:Event):void
	var myData:String ="\r\n").join("\n");
	myText.text = myData;

Basically what’s happening up there is that the “\r\n” are getting replaced by a single “\n”.


Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Create a free website or blog at

%d bloggers like this: