Repeat Character N Times

后端 未结 23 2548
栀梦
栀梦 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:21

    In ES2015/ES6 you can use "*".repeat(n)

    So just add this to your projects, and your are good to go.

      String.prototype.repeat = String.prototype.repeat || 
        function(n) {
          if (n < 0) throw new RangeError("invalid count value");
          if (n == 0) return "";
          return new Array(n + 1).join(this.toString()) 
        };
    

提交回复
热议问题