Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • split() method in JavaScript String

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 257
    Comment on it

    JavaScript String split() method : The split() method breaks the string into array of strings for a given expression. The string breaks from every point where the expression matched. For example if we give the blank expression then it will break the string from each character and return the array of all characters. It doesn't change the original string.

    Syntax of split() method :

    string.split(separator, limit)
    

    seperator : This is optional parameter. String and regular expression can be passed to the method which is used to split the string. If not passed then same string will be returned with one element.

    limit : This is also optional and this integer value is used to restrict the number of splits.


    Example of split() method : Following are some examples of the split() method.

    Example1.html

    <!-- Here we split the string from the space or blank string  -->
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Split and display the array values of string, click the button.</p>
    <button onclick="splitString()">split</button>
    <p id="container"></p>
    
    <script>
    function splitString() {
        var msg = "This is very beautiful painting.";
        var result = msg.split(" ");
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output : This , is , very , beautiful , painting.
    

    Example2.html

    <!-- Here we split the string from the each character-->
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Split and display the array values of string, click the button.</p>
    <button onclick="splitString()">split</button>
    <p id="container"></p>
    
    <script>
    function splitString() {
        var msg = "This is very beautiful painting.";
        var result = msg.split("");
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output : T , h , i , s ,  , i , s ,  , v , e , r , y , , b , e , a , u , t , i , f , u , l , , p , a , i , n , t , i , n , g , .
    

    Example3.html

    <!-- Here we split the string from the blank space with limit parameter-->
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Split and display the array values of string, click the button.</p>
    <button onclick="splitString()">split</button>
    <p id="container"></p>
    
    <script>
    function splitString() {
        var msg = "This is very beautiful painting.";
        var result = msg.split(" ", 3);
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output :  This , is , very
    

    Example4.html

    <!-- Here we split the string using the letter-->
    <!DOCTYPE html>
    <html>
    <body>
    
    <p>Split and display the array values of string, click the button.</p>
    <button onclick="splitString()">split</button>
    <p id="container"></p>
    
    <script>
    function splitString() {
        var msg = "This is very beautiful painting.";
        var result = msg.split("i");
        document.getElementById("container").innerHTML = result;
    }
    </script>
    
    </body>
    </html>
    
    Output : Th,s ,s very beaut,ful pa,nt,ng.
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: