Posts Tagged ‘string format’

When I am working with strings in JQuery I sometimes miss the ol’ “String.Format” in C#. I decided to create a quick global method that makes life just a little bit easier!

Usage:

$.stringFormat("{0} {1}!", ["Hello", "world"]);

Method:

jQuery.stringFormat = function(format, arguments) {
    var str = format;
    for (i = 0; i < arguments.length; i++) {
        str = str.replace('{' + i + '}', arguments[i]);
    }
    return str;
};