Repeat Character N Times

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

    I'm going to expand on @bonbon's answer. His method is an easy way to "append N chars to an existing string", just in case anyone needs to do that. For example since "a google" is a 1 followed by 100 zeros.

    for(var google = '1'; google.length < 1 + 100; google += '0'){}
    document.getElementById('el').innerText = google;
    This is "a google":

    NOTE: You do have to add the length of the original string to the conditional.

提交回复
热议问题