Repeat Character N Times

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

    For repeat a value in my projects i use repeat

    For example:

    var n = 6;
    for (i = 0; i < n; i++) {
        console.log("#".repeat(i+1))
    }
    

    but be careful because this method has been added to the ECMAScript 6 specification.

    0 讨论(0)
  • 2020-11-22 12:27

    For all browsers

    The following function will perform a lot faster than the option suggested in the accepted answer:

    var repeat = function(str, count) {
        var array = [];
        for(var i = 0; i < count;)
            array[i++] = str;
        return array.join('');
    }
    

    You'd use it like this :

    var repeatedString = repeat("a", 10);
    

    To compare the performance of this function with that of the option proposed in the accepted answer, see this Fiddle and this Fiddle for benchmarks.

    For moderns browsers only

    In modern browsers, you can now do this using String.prototype.repeat method:

    var repeatedString = "a".repeat(10);
    

    Read more about this method on MDN.

    This option is even faster. Unfortunately, it doesn't work in any version of Internet explorer. The numbers in the table specify the first browser version that fully supports the method:

    0 讨论(0)
  • 2020-11-22 12:27
    /**  
     * Repeat a string `n`-times (recursive)
     * @param {String} s - The string you want to repeat.
     * @param {Number} n - The times to repeat the string.
     * @param {String} d - A delimiter between each string.
     */
    
    var repeat = function (s, n, d) {
        return --n ? s + (d || "") + repeat(s, n, d) : "" + s;
    };
    
    var foo = "foo";
    console.log(
        "%s\n%s\n%s\n%s",
    
        repeat(foo),        // "foo"
        repeat(foo, 2),     // "foofoo"
        repeat(foo, "2"),   // "foofoo"
        repeat(foo, 2, "-") // "foo-foo"
    );
    
    0 讨论(0)
  • 2020-11-22 12:27

    Here is what I use:

    function repeat(str, num) {
            var holder = [];
            for(var i=0; i<num; i++) {
                holder.push(str);
            }
            return holder.join('');
        }
    
    0 讨论(0)
  • 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;
    <div>This is "a google":</div>
    <div id="el"></div>

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

    0 讨论(0)
  • 2020-11-22 12:32

    If you're not opposed to including a library in your project, lodash has a repeat function.

    _.repeat('*', 3);
    // → '***
    

    https://lodash.com/docs#repeat

    0 讨论(0)
提交回复
热议问题