View comments | RSS feed

String.split()

Availability

Flash Player 5.

Usage

my_str.split("delimiter":String, [limit:Number]) : Number

Parameters

delimiter A string; the character or string at which my_str splits.

limit A number; the number of items to place into the array. This parameter is optional.

Returns

An array; an array containing the substrings of my_str.

Description

Method; splits a String object into substrings by breaking it wherever the specified delimiter parameter occurs and returns the substrings in an array. If you use an empty string ("") as a delimiter, each character in the string is placed as an element in the array.

If the delimiter parameter is undefined, the entire string is placed into the first element of the returned array.

Example

The following example returns an array with five elements:

var my_str:String = "P,A,T,S,Y";
var my_array:Array = my_str.split(",");
for (var i = 0; i<my_array.length; i++) {
   trace(my_array[i]);
}
/* output:
   P
   A
   T
   S
   Y
*/

The following example returns an array with two elements, "P" and "A":

var my_str:String = "P,A,T,S,Y";
var my_array:Array = my_str.split(",", 2);
trace(my_array); // output: P,A

The following example shows that if you use an empty string ("") for the delimiter parameter, each character in the string is placed as an element in the array:

var my_str:String = new String("Joe");
var my_array:Array = my_str.split("");
for (var i = 0; i<my_array.length; i++) {
   trace(my_array[i]);
}
/* output:
   J
   o
   e
*/

An example is also in the Strings.fla file in the HelpExamples folder. The following list gives typical paths to this folder:

See Also

Array.join()

Comments


MotionMaker said on Aug 20, 2004 at 8:57 AM :
// When the split string is the last character
// you receive an array of 2

splitText = "="
textToParse = "The time for all good="

var splitArray = textToParse.split( splitText )
trace("splitArray.length = " + splitArray.length)
for (var i = 0; i < splitArray.length ; i++)
{
trace(i + ". " + splitArray[i])
}

// Output:
// splitArray.length = 2
// 0. The time for all good
// 1.
Mister Neb said on Aug 27, 2004 at 3:13 PM :
The Usage section lists the return type as Number, but it should be Array.
Francis Cheng said on Aug 27, 2004 at 4:29 PM :
Thanks for pointing out the incorrect return value in the Usage section. It is now fixed in the source file.
No screen name said on Mar 5, 2005 at 11:01 PM :
I need to split() using " and join using \" - where in this manual can I find
out how to use wildcard characters to split and join?

I want to turn name="myname" into name=\"myname\" and I'm having a
hell of a time trying to do it.
dfsolley said on Sep 14, 2005 at 1:49 PM :
For Flash MX 2004 Pro, the delimiter is limited to a single character. With previous flash versions and same code and file worked.

The input to the function is a string that may include placeholder for other defined parameters. The parameter name is embedded into the string by using the parameter name surrounded by "##".

If the parameter exists:
myFile="names.txt"

and the function is executed
trace(TextInputParameters("The File is ##myFile##"));
you should get
The File is names.txt

It used to work, now it doesn't.

CODE:
function TextInputParameters (myText) {
// replace ##parm## in the text with the value of the "parm" parameter
var inx;
var temp = myText.split("##");
for (inx = 1 ; inx < temp.length ; inx += 2) {
var val = eval(temp[inx]);
if (val == null) temp[inx] = "##"+temp[inx]+"##";
else temp[inx] = val;
}
return(temp.join(""));
}

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/mx2004/main_7_2/00001715.html