How split a string in jQuery

The split() method is used to split a string into an array. You have to pass separator value in split method. The separator value may be space, common, hyphen etc.
In below example I have used split('-') to separate a sting.

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

split('') : If you pass empty string in split method it will split each character into an array.

CODE
<!DOCTYPE html>
<html>
<head>
  <title>jQuery With Example</title>
  <script src="http://code.jquery.com/jquery-1.9.1.js" 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++) {
          alert(countryArray[i]);
        }

      });

    });
  </script>
</head>
<body>
  <div>
    <button class="btnClick">Click</button>
  </div>
</body>
</html>
DISPLAY

7 comments :