JavaScript

String.endsWith()メソッド

 編集:2022.08.16 by T.A.

その String の末尾に、指定文字列が含まれているか否かを返します。

boolean= String.endsWith(searchString[, length])
String String、文字列。
searchString String の末尾で検索される文字の集合です。
length 省略可。指定された場合、String の長さとして使用されます。既定値は str.length です。
戻り値

boolean。文字列が指定された文字列で終わる場合は true、それ以外の場合は false です。

解説

文字列が特定の文字列で終わるかどうかを判断できます。このメソッドでは(英文字の)大文字・小文字は区別されます。

let str = 'To be, or not to be, that is the question.';

D=str.endsWith('question.');  //=true
D=str.endsWith('to be');      //=false
D=str.endsWith('to be', 19);  //=true

テスト