xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js string format All In One

js string format All In One

/*
String.prototype.format = function() {
    var s = this;
    for (var i = 0, j = arguments.length; i < j; i++)
        s = s.replace("{" + (i) + "}", arguments[i]);
    return (s);
}
*/

String.prototype.format = function() {
    let str = this;
    console.log('str =', str);
    console.log('arguments =', arguments);
    for (let i = 0; i < arguments.length; i++) {
        str = str.replace("{" + i + "}", arguments[i]);
    }
    return str;
}

"{0}={1};expires={2};path={3}".format('name', 'value', 365, '/path');
// 'name=value;expires=365;path=/path'

image

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-07-07 22:26  xgqfrms  阅读(20)  评论(0编辑  收藏  举报