00:00
00:00
Upgrade Your Account!

Hey! To start this decade right, we believe Newgrounds should be ad-free for EVERYONE. For this to work, we need people who have the means to become paying supporters. For just $3 per month or $25 for the year, you can help this happen and get some nice perks!

For the month of January we have disabled ads on all E-M content. We need 179 people to reach our January goal of 3,000 active supporters. If we hit that, we'll continue with a new goal in February!

Please check out our Supporter Upgrade and consider coming aboard!

As: String To Array/array To String

4,283 Views | 3 Replies
New Topic Respond to this Topic

------------

As: Main may be dead, but I've always wanted to make a submission to it. :D

Basic Knowledge (You'll need this before we begin):
-(Intermediate) Arrays: Link 1 (by Denvish
), Link 2 (by Creepy
)
-Strings
-The concept of Variable Type Conversion

-------------

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.

Line 1:

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[0] = "Monsters";
arr[1] = "Robots";
arr[2] = "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(arr);

"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(String(arr));

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[0], arr[1], or arr[2] 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[0];//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. ;)


06-17-11: User of the Day

07-25-11: 1500th Post

08-09-11: Level 13!

Response to As: String To Array/array To String 2011-05-14 09:44:59


Your code should look like this:

(Method 1)

var arr:Array = new Array();
arr[0] = "Monsters";
arr[1] = "Robots";
arr[2] = "Aliens";
String(arr);
var newarr = new Array();
var mystring = String(arr);
newarr = mystring.split(",");
trace(arr[0];//you can change the 0 to 1 or 2

(Method 2)

var arr:Array = ["Monsters","Robots","Aliens"];
String(arr);
var newarr = new Array();
var mystring = String(arr);
newarr = mystring.split(",");
trace(arr[0];//you can change the 0 to 1 or 2

06-17-11: User of the Day

07-25-11: 1500th Post

08-09-11: Level 13!

Response to As: String To Array/array To String 2011-05-14 09:47:04


Sorry for the triple-post, but don't forget to switch

trace(arr[0;

to

trace(arr[0]);

06-17-11: User of the Day

07-25-11: 1500th Post

08-09-11: Level 13!

Response to As: String To Array/array To String 2011-05-15 05:13:14


also, instead of casting to a string, you might want to use:

array.join(",");

It's the function that toString() calls I believe but you can specify a separator. This will help solve problems with some values which might contain a comma.


You can ask me anything about AS3, PHP, HTML, CSS, JavaScript, Ruby, Python, Java, XML, JSON, Photoshop, and some more....