Repeat Character N Times

后端 未结 23 2811
栀梦
栀梦 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条回答
  •  Happy的楠姐
    2020-11-22 12:33

    In a new ES6 harmony, you will have native way for doing this with repeat. Also ES6 right now only experimental, this feature is already available in Edge, FF, Chrome and Safari

    "abc".repeat(3) // "abcabcabc"
    

    And surely if repeat function is not available you can use old-good Array(n + 1).join("abc")

提交回复
热议问题