Javascript String Split Function
A string can be splitted in javascript by using the split() function. The split function takes the delimiter as an arguement. Whenever it sees the delimiter the string is splitted and stored in the array.
sample code:
<script type=”text/javascript”>
var myString = “Javascript string split function”;
var mySplitString = myString.split(” “);
for(i = 0; i < mySplitString.length; i++)
{
document.write(”<br /> Element ” + i + ” = ” + mySplitString[i]);
}
</script>
In the above example the delimiter used is the space character ” “. Thus the split function splits the given string whenever it finds the space character and stores the result in the array. The result can be viewed by looping through the elements of the array with its length.
Result:
Element 0 = Javascript
Element 1 = string
Element 2 = split
Element 3 = function
