Home » JavaScript » JavaScript String Split Example

About Era Balliu

Era Balliu
Era is a Telecommunications Engineering student, with a great passion for new technologies. Up until now she has been coding with HTML/CSS, Bootstrap and other front-end coding languages and frameworks, and her recent love is Angular JS.

JavaScript String Split Example

When building or maintaining an interactive website, it may happen that the input you get from the user can be correct in meaning, but written as a long string separated by special characters such as commas or semicolons.

In these cases, JavaScript string split functions come to the rescue. You can split the long string into small ones and save them as an array.

Here’s how you can do that.

JavaScript Interview Coming Up?

Subscribe to our newsletter and download the Ultimate JavaScript interview questions and answers collection right now!

In order to get you prepared for your next JavaScript interview, we have compiled a huge list of relevant questions and their respective answers. Besides studying them online you may download the eBook in PDF format!

 

The split() method

You can split a string using the split() method. It takes a delimiter as a variable, which can be either a comma, a semicolon, a quotation mark, or whatever special character that is used to separate the string. You can use it like this:

var namelist = "Anne,Ben,Cindy,Diane,Era ";

var nameArray = namelist.split(',');

First, we have declared a string variable which contains some random names (you can use whatever string you want). Then we used the split() method on the string, by placing a comma as a delimiter. That means that the string will be divided into one more string than the number of the commas, and these will be respectively the strings before and after each comma. The result of the previous example will be this:

nameArray=['Anne','Ben','Cindy','Diane','Era'];

The delimiter argument to this method is optional, which means that if you leave it blank, the method will split the string after it finds a NULL character. Practically, it means that your string will not be divided.

The join() method

What is often called the reverse of the split() method is the join() method. It is used to put together into one single string the elements of an array, using a delimiter as a joint. The code snippet below will show you how exactly it is used:

var array = ['This','is','an','apple.'];

var sentence= array.join(' ');

We have declared and given string values to an array, and then used the join() method on this array to turn it into a single string, using a blank space as a joint.
The result will be this:

sentence="This is an apple.";

If you omit the joint character in this method, a comma will be used instead.

This is how you split or join strings using JavaScript.

Download the source code

This was an example of string split in JavaScript.

Download
Download the full source code of this example here: StringSplit

Do you want to know how to develop your skillset to become a Web Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. Building web apps with Node.js

2. HTML5 Programming Cookbook

3. CSS Programming Cookbook

4. AngularJS Programming Cookbook

5. jQuery Programming Cookbook

6. Bootstrap Programming Cookbook

 

and many more ....

 

 

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Want to take your WEB dev skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • PHP Programming Cookbook
  • jQuery Programming Cookbook
  • Bootstrap Programming Cookbook
  • Building WEB Apps with Node.js
  • CSS Programming Cookbook
  • HTML5 Programming Cookbook
  • AngularJS Programming Cookbook