Sunday, 2 February 2014

How to split the string using Jquery?

Description:

In this article, I will explain how to split the string using Jquery.

Split() method divide a string in smaller parts.

This method used to split a string into an array.

Syntax for Split() method is,

String.split([separator],[limit]);                    
        
Separator – space,hyphen,comma etc.

Limit – used to specify number of limits to be found.

Split(‘’) – if we pass empty string in split method, it will split each character into an array.

Split(‘ ‘) – if we pass blank space in split method, it will split each individual word into an array.

Example Program:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title></title>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $("#btnSplitdot").click(function () {
                var text = "www.jqueryjohn.com";
                var splitText = text.split('.');
                for (var i = 0; i < splitText.length; i++) {
                    alert(splitText[i]);
                }
                return false;
            });
            $("#btnsplitspace").click(function () {
                var text = "welcome to jqueryjohn website";
                var splitText = text.split(' ');
                for (var i = 0; i < splitText.length; i++) {
                    alert(splitText[i]);
                }
                return false;
            });
            $("#btnsplitcharacter").click(function () {
                var text = "jqueryjohn";
                var splitText = text.split('',5);
                for (var i = 0; i < splitText.length; i++) {
                    alert(splitText[i]);
                }
                return false;
            });
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="btnSplitdot" runat="server" Text="Split String by Dot" />
        <asp:Button ID="btnsplitspace" runat="server" Text="Split String by space" />
        <asp:Button ID="btnsplitcharacter" runat="server" Text="Split String by empty string" />
    </div>
    </form>
</body>
</html>

While splitting string by empty string, I am passing an empty string with split() method and assigned a limit as 5.

This will return only five individual characters.

How to use split function with jquery , Split function using Jquery, Jquery split method, How to split a string, Use split method in jquery, Javascript split string example, Split method examples, Split string using Javascript

No comments:

Post a Comment