Try Out New Website Button- Maker Button-Maker

Best way to split string in javascript

Split string in Javascript. This tutorial will show the Best Way to split string in javascript, how you can split a string or words by using the built

Split string in Javascript.

This tutorial will show the Best Way to split string in javascript, how you can split a string or words by using the built in split function in Javascript To split a string in Javascript is pretty simple, you will just need to using the following syntax. There will be two parameters available:

Best way to split string in javascript
Split string in javascript
Code
string.split(separator_symbol, no_of_splits)

Note: the last parameter which is no_of_splits is optional

Code
var sampleString = "one;two;three;four;five";
var arrString = sampleString.split(";");

for(i = 0; i < arrString.length; i++){
    alert(arrString[i]);
}

/* The above for loop function will return the following 5 alert message box: */
one
two
three
four
five

Here is another example that you want to get the first two splits only.

Code
var sampleString = "100,200,300,400,500";
var arrString = sampleString.split(",", 2);

for(i = 0; i < arrString.length; i++){
    alert(arrString[i]);
}

/* The above for loop function will return the following 2 alert message box: */
100
200

Rate this article

Loading...

Post a Comment

Cookies Consent

This website uses cookies to ensure you get the best experience on our website.

Cookies Policy

We employ the use of cookies. By accessing Lantro UI, you agreed to use cookies in agreement with the Lantro UI's Privacy Policy.

Most interactive websites use cookies to let us retrieve the user’s details for each visit. Cookies are used by our website to enable the functionality of certain areas to make it easier for people visiting our website. Some of our affiliate/advertising partners may also use cookies.