7
JavaScript 字符串 endsWith 方法 | myfreax
source link: https://www.myfreax.com/javascript-string-endswith/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
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'));
true
endsWith()
方法使用区分大小写匹配字符,因此,以下示例返回 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()
方法检查字符串是否以指定子字符串结尾。
微信公众号
支付宝打赏
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK