Repeat Character N Times

后端 未结 23 2875
栀梦
栀梦 2020-11-22 11:51

In Perl I can repeat a character multiple times using the syntax:

$a = \"a\" x 10; // results in \"aaaaaaaaaa\"

Is there a simple way to ac

23条回答
  •  暖寄归人
    2020-11-22 12:16

    String.prototype.repeat = function (n) { n = Math.abs(n) || 1; return Array(n + 1).join(this || ''); };
    
    // console.log("0".repeat(3) , "0".repeat(-3))
    // return: "000" "000"
    

提交回复
热议问题