Javascript Split Function

The Javascript split() function works much the same as the php split() or explode() functions. Basically you take a string at break it up everywhere the delimiter character occurs and return the new strings in an array. So for example, let's say you have a string like this:

[script]var colorString = "red|orange|blue|white|black|brown";[/script]

Which is obviously a simple a pipe-delimited string of colors. So to make this into an array we would use the split function.

[script]var colorArray = colorString.split("|");[/script]

Now you have a simple array of colors and can access them as you would any array, either in a loop or by the numeric index, whatever...

[script]
var message = "My favorite color is " + colorArray[3] ;
alert(message);
[/script]

These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • Reddit
  • Furl
  • NewsVine
  • Simpy
  • Slashdot
  • Spurl
  • StumbleUpon
  • YahooMyWeb
  • TailRank

Home | JavaScript | Javascript Split Function