「JavaScript」endsWithで文字の終わりを調べる方法

書式
str.endsWith(searchString[, length])
searchString
str の末尾で検索される文字の集合です。
length
指定された場合、 str の長さとして使用されます。既定値は str.length です。
endsWith() メソッドは文字列が引数で指定された文字列で終わるかを判定して true か false を返します。

使用例

const strA = "テスト 太郎";
const target="太郎";
if (strA.endsWith(target)) {
    console.log("文字列「太郎」の終わり true"); 
} else {
    console.log("文字列「太郎」の終わり false");
}

const strB = "study skill arkgame";
const target2="arkgame";
if (strB.endsWith(target2)) {
    console.log("文字列「arkgame」の終わり true");
} else {
    console.log("文字列「arkgame」の終わり false"); 
}

実行結果

"文字列「太郎」の終わり true"
"文字列「arkgame」の終わり true"

 

JavaScript

Posted by arkgame