Online JavaScript Tutorials

JavaScript tutorials which will help you use javaScripts on your web pages, even beginners. This javascript tutorial also includes links to other programming sites.

JavaScript Equivalent of PHP Explode Function

In PHP we can easily break a long string into smaller parts by using the explode() function of PHP. In run rime, this function works like this:

$string_to_Explode="JavaScript Equivalent of PHP Explode Function";
$explodestring=explode(" ", $string_to_Explode);

After the execution of the second command the variable $explodestring is an array such that,

$explodestring[0]="JavaScript"
$explodestring[1]="Equivalent"
$explodestring[2]="of"
$explodestring[3]="Explode"
$explodestring[4]="Function"

and so on. So how do we do it in JavaScript. In JavaScript there is a split() function that achieves the same objective, although the syntax is a bit different.

var string_to_explode="JavaScript Equivalent of PHP Explode Function";
var explodestring=string_to_explode.split(" ");


Now the Array explodestring has all those words such that,

explodestring[0]="JavaScript"
explodestring[1]="Equivalent"
explodestring[2]="of"
explodestring[3]="Explode"
explodestring[4]="Function"

No comments: