Monday 29 August 2016

Split String using JQuery split() method

In this post, I will explain how to split a string using JQuery.

A split() method is used to split a string into an array. You have to pass separator value in the split method. The separator value may be space, comma, hyphen etc.

In below example, I have used split('-') to separate a string.

var countryName = 'India-Japan-Pakistan';
var countryArray = countryName.split('-');

split(''): If you pass an empty string in a split method it will split each character into an array.
<!DOCTYPE html>
<html lang="">
  <title>JQuery With Example</title>
  <script src="" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $('.btnClick').click(function () {
        var countryName = 'India-Japan-Pakistan';
        var countryArray = countryName.split('-');
        for (var i = 0; i < countryArray.length; i++) {
    <button class="btnClick">Click</button>


Post a Comment

Please do not enter any spam link in the message box.