
JavaScript 字符串 endsWith() 方法
在本教程中,您将学习如何使用 JavaScript 字符串 endsWith() 方法来检查字符串是否以子字符串结尾
在本教程中,您将学习如何使用 JavaScript 字符串 endsWith() 方法来检查字符串是否以子字符串结尾。
JavaScript String endsWith 方法简介
如果字符串是以指定的子字符串结尾 endsWith() 方法返回 true ,否则返回 false。 endsWith() 方法的语法如下:
String.startsWith(searchString [,position])
参数
searchString是要在字符串末尾搜索的一个或者多个字符。length是一个可选参数,用于确定要搜索的字符串的长度。它默认是String的长度。
请注意,要检查字符串是否以子字符串开头,请使用 startsWith()方法。
JavaScript String endsWith() 方法示例
假设您有一个字符串变量 title ,如下所示
const title = 'Jack and Jill Went Up the Hill';
以下示例使用 endsWith() 方法检查 title 字符串是否以字符串 'Hill' 结尾:
console.log(title.endsWith('Hill'));
输出:
trueendsWith() 方法使用区分大小写匹配字符,因此,以下示例返回 false:
title.endsWith('hill');以下示例使用 带有第二个参数的 endsWith() 方法来确定要搜索的字符串的长度:
console.log(title.endsWith('Up', 21));输出:
true放在一起:
const title = 'Jack and Jill Went Up the Hill';
console.log(title.endsWith('Hill'));
console.log(title.endsWith('hill'));
console.log(title.endsWith('Up', 21));输出:
true
false
true结论
使用字符串 endsWith()方法检查字符串是否以指定子字符串结尾。


















