Repeat String - Javascript

前端 未结 30 1841
长情又很酷
长情又很酷 2020-11-22 08:46

What is the best or most concise method for returning a string repeated an arbitrary amount of times?

The following is my best shot so far:

function          


        
相关标签:
30条回答
  • 2020-11-22 09:09

    With ES8 you could also use padStart or padEnd for this. eg.

    var str = 'cat';
    var num = 23;
    var size = str.length * num;
    "".padStart(size, str) // outputs: 'catcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcatcat'
    
    0 讨论(0)
  • 2020-11-22 09:10

    String.prototype.repeat is now ES6 Standard.

    'abc'.repeat(3); //abcabcabc
    
    0 讨论(0)
  • 2020-11-22 09:10

    ES2015 has been realized this repeat() method!

    http://www.ecma-international.org/ecma-262/6.0/#sec-string.prototype.repeat
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/repeat
    http://www.w3schools.com/jsref/jsref_repeat.asp

    /** 
     * str: String
     * count: Number
     */
    const str = `hello repeat!\n`, count = 3;
    
    let resultString = str.repeat(count);
    
    console.log(`resultString = \n${resultString}`);
    /*
    resultString = 
    hello repeat!
    hello repeat!
    hello repeat!
    */
    
    ({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2);
    // 'abcabc' (repeat() is a generic method)
    
    // Examples
    
    'abc'.repeat(0);    // ''
    'abc'.repeat(1);    // 'abc'
    'abc'.repeat(2);    // 'abcabc'
    'abc'.repeat(3.5);  // 'abcabcabc' (count will be converted to integer)
    // 'abc'.repeat(1/0);  // RangeError
    // 'abc'.repeat(-1);   // RangeError

    0 讨论(0)
  • 2020-11-22 09:11

    For all browsers

    This is about as concise as it gets :

    function repeat(s, n) { return new Array(n+1).join(s); }
    

    If you also care about performance, this is a much better approach :

    function repeat(s, n) { var a=[],i=0;for(;i<n;)a[i++]=s;return a.join(''); }
    

    If you want to compare the performance of both options, see this Fiddle and this Fiddle for benchmark tests. During my own tests, the second option was about 2 times faster in Firefox and about 4 times faster in Chrome!

    For moderns browsers only :

    In modern browsers, you can now also do this :

    function repeat(s,n) { return s.repeat(n) };
    

    This option is not only shorter than both other options, but it's even faster than the second option.

    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 09:11

    Fiddle: http://jsfiddle.net/3Y9v2/

    function repeat(s, n){
        return ((new Array(n+1)).join(s));
    }
    alert(repeat('R', 10));
    
    0 讨论(0)
  • 2020-11-22 09:12

    Here's the JSLint safe version

    String.prototype.repeat = function (num) {
      var a = [];
      a.length = num << 0 + 1;
      return a.join(this);
    };
    
    0 讨论(0)
提交回复
热议问题