sponsor

Showing posts with label split. Show all posts
Showing posts with label split. Show all posts

Tuesday, February 19, 2008

Javascript Explode Implode

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="My Name Is Wahyu Pramusinto";
$string2=explode(" ", $string);


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

$string2[0]="My"
$string2[1]="Name"
$string2[2]="Is"
$string2[3]="Wahyu"
$string2[4]="Pramusinto"

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="My Name Is Wahyu Pramusinto";
var string2=string.split(" ");

Now the variable string2 has all those words.