Click to See Complete Forum and Search --> : Need an example of the Split command in JavaScript


mpoincare
April 11th, 2003, 10:08 PM
I need a clear explanation and example of the use of the Split method in a JavaScript function.

nategrover
April 14th, 2003, 10:00 AM
It's very similar to a StringTokenizer in java, at least in terms of purpose and effect.

It is an inherent method available on any variable that is of type String. You pass a string to it and get an array of strings that are composed of all the pieces of the original string that were split up at the point where the argument was found.

So, if you have a string like this:

var myString = "a,b,c,d,e,f-g,h,i,j,k";
var splits = myString.split("-");

splits will be an array with two elements, "a,b,c,d,e,f" and "g,h,i,j,k";

Now you could go further if you like with :

var partOne = splits[0].split(",");
var partTwo = splits[1].split(",");

partOne will be an array with 6 elements
partOne[0] = "a"
partOne[1] = "b"
partOne[2] = "c"
partOne[3] = "d"
partOne[4] = "e"
partOne[5] = "f"

You can probably guess what partTwo would be.

Hope this helps.