As: String To Array/array To String 2011-05-14 09:41:23
As: Main may be dead, but I've always wanted to make a submission to it. :D
Have you ever been stuck wanting to convert a string to array, an array to a string, or both? Well, look no further. This is your one-stop-shop to both of those.
Let's start with converting an array to string, then converting that string back to an array.
First, create a new array. For this tutorial, I'll simply call it "arr". You can do the same if you feel like following this with me giving you all the code. It will all be explained in case you aren't following what's going on, or if you're changing certain things that come along.
var arr:Array = new Array();
"var" states that a variable is about to be defined (on the same line)
"arr:Array" The name of the variable, followed by a colon and its type of variable.
"=" Following that, you'll define what the variable is equal to.
"new Array();" If you're not defining everything now and would rather do it as you go, use this. The semi-colon helps to know that you're done with that line of script.
Now we're going to add to the array. You can do one of two things to accomplish this. We're going to add "Monsters","Robots", and "Aliens" to the array. Here's the easiest way to do this:
Alter the first line, and change it to this:
var arr:Array = ["Monsters","Robots","Aliens"];
"[" opens the array's contents.
For every quotation mark, that defines a new string. The comma divides them into sections 0,1,2, because arrays' first parts are 0, and go on from there.
"]" closes the array's contents.
";" ends the line
Or you can do this second method on the lines following Line 1. (Do not use the first method, and this; use the original Line 1 and this):
arr = "Monsters"; arr = "Robots"; arr = "Aliens";
When defining parts of an array dynamically rather than through the single array line, you use "[#]" to define its parts, starting with 0 which would be the array's first content. You can do this indefinitely, and never end the array this way.
Now, to conver to a string, following the lines above, do this simple step:
"String()" The function.
"(contents)" Contents would be the name of the variable you're converting to a string.
This converts the array into a string. You can use String() to turn any Array or Number to String type. If you decide that you want to see the string in person, add this line to the script, then export your game/app/test:
trace() is a very useful debugging function. You should see its output in the "Output" panel, normally at the bottom of the screen in most versions of Flash.
Now for string to array. Now, when you made the array a string, it outputted "Monsters, Robots, Aliens", which would be the same as simply tracing the array. But now it's a string. The commas are still there to make sure you know that parts are divided. It's useful for returning it to its Array form.
Because we have these commas, now we can divide these parts to return them to their array form. To do so, we'll use a function called "split()".
var newarr = new Array(); var mystring = String(arr); newarr = mystring.split(",");
What this three-line code does, is make two new variables, one for the new array that will contain our old array converted, and one that's a string that makes it easier for spliting our new stringed old array called "arr".
The third line utilizes the split() function, and inside it we have ",". Anything in the quotation marks will define what separates the string. Each comma will then erase itself from the string, and each word between commas, after the last comma, and before the first comma will become the arr, arr, or arr parts of the newarr. If you then trace the newarr, you will output "Monsters, Robots, Aliens", and if you trace each part of the array individually like so
trace(arr;//you can change the 0 to 1 or 2
you will output the individual parts.
So you should get the gist of it, and should know how to do each individually for later use. Thanks again, and don't forget to visit As: Main for your needs. ;)