Repeat Character N Times

后端 未结 23 2797
栀梦
栀梦 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条回答
  •  旧时难觅i
    2020-11-22 12:12

    Convenient if you repeat yourself a lot:

    String.prototype.repeat = String.prototype.repeat || function(n){
      n= n || 1;
      return Array(n+1).join(this);
    }
    
    alert(  'Are we there yet?\nNo.\n'.repeat(10)  )

提交回复
热议问题