JavaScript

String.prototype.startsWith()メソッド

 編集:2022.08.16 by T.A.

その String で、指定文字列が指定位置から始まるか否か返します。

構文
boolean= String.startsWith(searchString[, position] )
String String
searchString 文字列の先頭で検索される文字の集合です。
position 省略可searchString を検索し始めるこの文字列の中の位置です。既定値は 0 です。
戻り値

Boolean。文字列が指定された文字列で始まる場合は true、それ以外の場合は false です。

解説

その文字列が、指定の文字列で始まるかどうかを判断できます。(英文字の)大文字・小文字は区別されます。

let str='To be, or not to be, that is the question.';
D=str.startsWith('To be');         //=true
D=str.startsWith('not to be');     //=false
D=str.startsWith('not to be', 10); //=true

テスト