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.
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
thank you dear....
ReplyDeleteThanks
ReplyDeletegood
ReplyDeletehow to split a string with : and \n
ReplyDeleteYou have to use multiple separator.
Deletehttp://www.jquerywithexample.com/2015/11/split-string-with-multiple-separator-in.html
thanks ....
ReplyDeleteThis is not JQuery. The code is showing Javascript way of splitting.
ReplyDeleteVery nice article!
ReplyDeleteThanks.
Ravi (DotNetTec)