In this blog, I will explain you how to jQuery split string to array. In my previous tutorials, I’d explained about how to split string date into day month and year from code-behind, validate checkboxlist using jquery, validate radiobuttonlist using javascript, validate dropdownlist using javascript and other more cracking tutorials on Asp.net, JavaScript, jQuery here.
To split the string into array using jQuery, we can use jQuery built-in split() function. It requires a separator as a parameter to split the string. Now here in this tutorial, I’ll explain How to jQuery Split String to Array
using jquery split() function.
Split String into Array using jQuery split function in Asp.net. We can use any special character as a separator (like #, @, $, &, ; etc) to split the string and based on that specific character it’ll return the string array and then we can use that array as per our need.
jQuery Split Function to Split String into Array
Following is the script block that will split your string into array by using comma (;) separator.
$(document).ready(function () {
$(‘#btnSplitMe’).click(function () {
var strStringToSplit = $(‘#lblStringToSplit’).text();
var arrySplittedString = strStringToSplit.split(‘;’);
$(‘#lblSplittedString’).html(“”);
var splitedText = “”;
for (var i = 0; i < arrySplittedString.length; i++) {
splitedText += (arrySplittedString[i] + “<br />”);
$(‘#lblSplittedString’).html(splitedText);
}return false;
});
$(‘#btnClearMe’).click(function () {
$(‘#lblSplittedString’).html(“”);
return false;
});
});
</script>
HTML Markup To Split String Using Split Function Example – [.aspx]
Following is the complete HTML Markup code for .aspx page:
<head id=”Head1″ runat=”server”>
<title>How to jQuery Split String to Array in Asp .netÂ
</title>
<script type=”text/javascript” src=”http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js”>
</script>
<script type=”text/javascript”>
$(document).ready(function () {
$(‘#btnSplitMe’).click(function () {
var strStringToSplit = $(‘#lblStringToSplit’).text();
var arrySplittedString = strStringToSplit.split(‘;’);
$(‘#lblSplittedString’).html(“”);
var splitedText = “”;
for (var i = 0; i < arrySplittedString.length; i++) {
splitedText += (arrySplittedString[i] + “<br />”);
$(‘#lblSplittedString’).html(splitedText);
}return false;
});
$(‘#btnClearMe’).click(function () {
$(‘#lblSplittedString’).html(“”);
return false;
});
});
</script>
</head>
<body>
<form id=”form1″ runat=”server”>
<div>
<table>
<tr>
<td>String to split</td>
<td>:</td>
<td>
<asp:Label ID=”lblStringToSplit” Text=”Asp.net;HTML;CSS;jQuery;JavaScript”
runat=”server” ClientIDMode=”Static”>
</asp:Label>
</td>
</tr>
<tr>
<td colspan=”3″> </td>
</tr>
<tr>
<td>Result string array</td>
<td>:</td>
<td>
<asp:Label ID=”lblSplittedString” runat=”server” ClientIDMode=”Static”>
</asp:Label>
</td>
</tr>
<tr>
<td colspan=”3″> </td>
</tr>
<tr>
<td colspan=”2″> </td>
<td>
<asp:Button ID=”btnSplitMe” runat=”server” Text=”Split Me!” ClientIDMode=”Static” />
<asp:Button ID=”btnClearMe” runat=”server” Text=”Clear Me!” ClientIDMode=”Static” />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>