Home Asp.net How to jQuery Split String to Array in Asp.net

How to jQuery Split String to Array in Asp.net

43
0
jQuery Split String to Array in asp.net
jQuery Split String to Array in asp.net

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:

<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>

HTML Markup To Split String Using Split Function Example – [.aspx]

Following is the complete HTML Markup code for .aspx page:

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>How to jQuery Split String to Array in Asp.net</title>
    <script type="text/javascript" src="https://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">&nbsp;</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">&nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2">&nbsp;</td>
                    <td>
                        <asp:Button ID="btnSplitMe" runat="server" Text="Split Me!" ClientIDMode="Static" />
                        &nbsp;
                        <asp:Button ID="btnClearMe" runat="server" Text="Clear Me!" ClientIDMode="Static" />
                    </td>
                </tr>
            </table>
        </div>
    </form>
</body>
</html>

Example Result

jQuery Split String to Array in asp.net
jQuery Split String to Array in asp.net

Download Example

That’s it, this is one of the simplest way to split string into array using jquery split function in asp.net.

Let me know if you’ve any questions or doubts about this tutorial by writing down your queries in comment below. I would be happy to provide you my feedback as soon as possible.

Happy Coding!

Previous articleDropDownList Validation: Validate DropDownList using JavaScript in Asp.net
Next articleCheckBoxList Validation: Validate CheckBoxList using JavaScript in Asp.net
Hi there, I am Mayank, the man behind Technical Mack. I started AspnetO with a motive to educate people on various programming languages ranging from beginners to expert level. Through this blog, I aim to provide more insightful content.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

four × 5 =