String.prototype.startsWith()
startsWith()
メソッドは文字列が引数で指定された文字列で始まるかを判定して true
か false
を返します。
試してみましょう
構文
str.startsWith(searchString[, position])
引数
searchString
-
文字列の先頭で検索される文字の集合です。
position
省略可-
searchString
を検索し始めるこの文字列の中の位置です。既定値は0
です。
返値
文字列が指定された文字列で始まる場合は true
、それ以外の場合は false
です。
解説
文字列が特定の文字列で始まるかどうかを判断できます。(英文字の)大文字・小文字は区別されます。
例
startsWith() の使用
//startswith
let str = 'To be, or not to be, that is the question.'
console.log(str.startsWith('To be')) // true
console.log(str.startsWith('not to be')) // false
console.log(str.startsWith('not to be', 10)) // true
ポリフィル
このメソッドは ECMAScript 2015 で追加されました。すべての JavaScript の実装でまだ利用可能ではないかもしれません。しかしながら、次のコードでString.prototype.startsWith()
をエミュレートできます。
if (!String.prototype.startsWith) {
Object.defineProperty(String.prototype, 'startsWith', {
value: function(search, rawPos) {
var pos = rawPos > 0 ? rawPos|0 : 0;
return this.substring(pos, pos + search.length) === search;
}
});
}
少々重いですがより強力 (ES2015 に完全準拠) な互換実装を Mathias Bynens が GitHub で公開しています。
仕様書
Specification |
---|
ECMAScript Language Specification # sec-string.prototype.startswith |
ブラウザーの互換性
BCD tables only load in the browser