1

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.

1 Answer 1

3

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)

Sign up to request clarification or add additional context in comments.

3 Comments

You can split with regex, but remove the bracket expression or it will split on each single occurrence of \r and \n rather than on \r\n. testfile.split(/\r\n/);
good point, the only reason I added the [] was because RegExr wasn't matching anything when I set \r\n. You might be able to get away with just a /\r/ or a /\n/, or event /\r\n?/, depending on the file in question, and how far you want to delve into the bowels of regular expressions :)
You both got it right. I can either take the array strings and use replace or split with /\r\n/.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.