Question

I have a text file that I read using the usual URLRequest and URLloader functions. It consists of a series of names, each separated by \r\d. I want to create an array of those names, but I want to eliminate both the \r and the \d. This code does a great job at splitting the names into arrays, but it leaves the carriage return in the string.

names = testfile.split(String.fromCharCode(10));

And this leaves the new line:

names = testfile.split(String.fromCharCode(13));

I'm mainly a C/C++/assembly programmer, AS3 has some things that seem rather odd to me. Is there a way to do this? I've tried searching the resulting string array members but I get errors from the compiler. Very easy to do in C/C++/assembly, but I haven't quite figured AS3 out yet.

Was it helpful?

Solution

You should be able to use a RegExp to do this. Something like:

var noLines:String = withLines.replace( /[\r\n]/g, "" );

That'll remove all new lines from your string; whether you want to do that before or after splitting it up to you.

If your string is in the form:

name1
name2
name3

Then you might even be able to get away with splitting using a RegExp:

var names:Array = withLines.split( /[\r\n]/ );

You can test out the RegExp provided here: http://regexr.com?38dmk (click on the replace tab and clear the replace input)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top