repeat method

String repeat(
  1. int n,
  2. {String separator = ''}
)

Repeats the string n times

You can set an optional separator that is put in between each repetition

Example:

'hello'.repeat(2);                // 'hellohello'
'cat'.repeat(3, separator: ':');  // 'cat:cat:cat'

Implementation

String repeat(int n, {String separator = ''}) {
  throwIfNot(n > 0,
      () => ArgumentError('n must be a positive value greater then 0'));

  var repeatedString = '';

  for (var i = 0; i < n; i++) {
    if (i > 0) {
      repeatedString += separator;
    }
    repeatedString += this;
  }

  return repeatedString;
}