Coldapoy's Technical Blog

"Programming is like sex: one mistake and you have to support it for the rest of your life"

Javascript String Split Function

 

The ability to split up a string into separate chunks has been supported in many programming languages and it is available in Javascript as well. If you had a long string like "Bobby Susan Tracy Jack Phil Yannis" and wanted to store each name separately, you could specify the space character " " and have the split function create a new chunk every time it saw a space.

Split Function: Delimiter

The space character " " we mentioned will be our delimiter and is used by the split function as a way of breaking up the string. Every time it sees the delimiter that we specified it will create a new element in an array. The first argument of the split function is the delimiter.

Simple Split Function Example

Let's start off with a little example that takes a string of numbers and splits when it sees the number 5. That means the delimiter for this example is 5. Notice that the split function returns an array that we store into mySplitResult.

Javscript Code:

<script type="text/javascript">

var myString = "123456789";

 

var mySplitResult = myString.split("5");

 

document.write("The first element is " + mySplitResult[0]);

document.write("<br /> The second element is  " + mySplitResult[1]);

</script>

Display:

The first element is 1234
The second element is 6789

Make sure you realize that because we chose the 5 to be our delimiter, it is not in our result. This is because the delimiter is removed from the string and those remaining characters are separated by the chasm of space that the 5 used to occupy.

Larger Split Function Example

Below we have created a split example to illustrate how this function works with many splits. We have created a string with numbered words zero through four. The delimiter in this example will be the space character " ".

Javscript Code:

<script type="text/javascript">
var myString = "zero one two three four";
 
var mySplitResult = myString.split(" ");
 
for(i = 0; i < mySplitResult.length; i++){
               document.write("<br /> Element " + i + " = " + mySplitResult); 
}
</script>

Display:

Element 0 = zero
Element 1 = one
Element 2 = two
Element 3 = three
Element 4 = four

 

Here is the Link to the original Article : CLick Here

 

Comments

coldapoy said:

cool split method() ^_^

# July 26, 2008 9:47 PM

ламинат said:

x Een plaatje zegt alles, toch ? u  Het volledige rapport is hier te vinden. Lees natuurlijk p  de blogposting. g o

<a href=http://skuper.ru>паркет и ламинат </a> 2x

# August 12, 2008 3:08 AM

ламинат said:

hag Een plaatje zegt alles, toch ? ahw  Het volledige rapport is hier te vinden. Lees natuurlijk y  de blogposting. w q

Thanks for interesting post! gjy

<a href=http://skuper.ru>паркетная доска</a> 9d

# August 13, 2008 3:21 AM

сайдинг said:

2mThank's.7g I compleatly agree with last post.

<a href="all-siding.ru/index.php цены</a> 5c

<a href="all-siding.ru/index.php продажа</a> 6d

# August 18, 2008 4:08 AM