String.prototype.repeat() - 文字を繰り返す
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>
サポート状況
Chrome | Firefox | Safari | Edge | IE | Opera | iOS Safari | Android |
---|---|---|---|---|---|---|---|
● 41+ | ● 24+ | ● 9.1+ | ● | × | ● 28+ | ● 9.1+ | × |
関連記事
- String.prototype.slice()
- String.prototype.slice()は、元の文字列の指定した部分を切り取って新しい文字列を返すメソッドです。substring()と用途は同じですが、引数の指定方法が違います。
- String.prototype.replace()
- String.prototype.replace()は、文字列を置換するメソッドです。置換前の文字は文字列、または正規表現(RegExp)で指定できます。
- String.prototype.substring()
- String.prototype.substring()は、元の文字列の指定した部分を取り出して新しい文字列を返すメソッドです。slice()と用途は同じですが、引数の指定方法が違います。
- String.prototype.concat()
- String.prototype.concat()は、元の文字と引数に指定した文字を連結して新しい文字列を取得するメソッドです。