SYNCERのロゴ
アイキャッチ画像

String.prototype.repeat() - 文字を繰り返す

String.prototype.repeat()は、元の文字列を引数の回数だけ繰り返した新しい文字列を返すメソッドです。

概要

名前
repeat
所属
String.prototype
仕様書
https://tc39.github.io/ecma262/#sec-string.prototype.repeat

説明

String.prototype.repeat ( count )

引数(count)には繰り返す回数を指定します。0を指定した場合、空文字が返ります。

デモ

String.prototype.repeat()のデモです。

<!-- このコードは編集できます。 -->

<!DOCTYPE html>
<html>
<head>
	<style>
body {
	white-space: pre-wrap ;
}
	</style>
</head>
<body>
<script>
/** try it! **/
var string = "SYNCER" ;

var a = string.repeat( 3 ) ;
var b = string.repeat( 1 ) ;
var c = string.repeat( 0 ) ;
/** try it! **/

var results = { a:a, b:b, c:c, } ;

for( var name in results ) {
	console.log( name, results[name] ) ;
	document.body.appendChild( new Text( name + " = " + JSON.stringify( results[name] ) + "\n" ) ) ;
}
</script>
</body>
</html>

サポート状況

ChromeFirefoxSafariEdgeIEOperaiOS SafariAndroid
41+ 24+ 9.1+× 28+ 9.1+×
  • Twitterでシェア
  • Facebookでシェア
  • Google+でシェア
  • はてなブックマークでシェア
  • pocketに保存
  • LINEでシェア
更新履歴
2017年9月29日 (金)
コンテンツを公開しました。