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="">
<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>
<html lang="">
<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>
0 comments:
Post a Comment
Please do not enter any spam link in the message box.