JavaScript

String.prototype.repeat メソッド

 編集:2022.08.16 by T.A.

その String を指定数だけコピーして結合した文字列を返します。

構文
string= String.repeat(count)
String String。
count 0 から正の無限大までの間の整数で、文字列を繰り返す数を示します。
戻り値

String。与えられた文字列の指定した回数分のコピーを含む新しい文字列です。

例外
D='abc'.repeat(-1);  // RangeError発生
D='abc'.repeat(0);   //=''
D='abc'.repeat(1);   //='abc'
D='abc'.repeat(2);   //='abcabc'
D='abc'.repeat(3.5); //='abcabcabc'。小数は丸められ、整数の結果が返されます。
D='abc'.repeat(1/0); // RangeError発生

D=({ toString: () => 'abc', repeat: String.prototype.repeat }).repeat(2); //='abcabc'。repeat() は汎用メソッドです。

テスト