JQuery to Split a String with Single or Multiple Delimeter in Asp.Net C# VB.Net

Sometimes we need to break a string based on a character or a set of character. Each language provides us these functionality by a method named SPLIT. In JQuery we can also use SPLIT method to split a string. Here in this article I will explain “How one can Split string using JQuery“.

JQuery code to Split a string using Single Delimiter:

    $(document).ready(function () {
        var MonthName = 'January,February,March';
        var MonthArray = MonthName.split(',');

        for (var i = 0; i < MonthArray.length; i++) {
            alert(MonthArray[i]);
        }
    });

JQuery code to Split a string using Single Delimiter with Limit:

    $(document).ready(function () {
        var MonthName = 'January,February,March';
        var MonthArray = MonthName.split(',',1);

        for (var i = 0; i < MonthArray.length; i++) {
            alert(MonthArray[i]);
        }
    });

Note: If you provide a Limit size you will get only the elements mentioned in this Limit. In the example the output will be only “January”.

JQuery code to Split a string using Multiple Delimiter:

    $(document).ready(function () {
        var MonthName = 'January-February,March';
        var separators = [' ','-',','];
        var MonthArray = MonthName.split(new RegExp(separators.join('|'), ''));

        for (var i = 0; i < MonthArray.length; i++) {
            alert(MonthArray[i]);
        }
    });

Hope now you can split a string by a single delimiter or multiple delimiter as per your requirement.

Download Code Example C#        Download Code Example VB.Net

Leave a Reply

Your email address will not be published. Required fields are marked *

AlphaOmega Captcha Classica  –  Enter Security Code