JavaScript equivalent to printf/String.Format

前端 未结 30 2497
囚心锁ツ
囚心锁ツ 2020-11-21 04:27

I\'m looking for a good JavaScript equivalent of the C/PHP printf() or for C#/Java programmers, String.Format() (IFormatProvider for .

30条回答
  •  鱼传尺愫
    2020-11-21 05:06

    I did not see the String.format variant:

    String.format = function (string) {
        var args = Array.prototype.slice.call(arguments, 1, arguments.length);
        return string.replace(/{(\d+)}/g, function (match, number) {
            return typeof args[number] != "undefined" ? args[number] : match;
        });
    };
    

提交回复
热议问题