位置: IT常识 - 正文

js endswith(js endswith()用法)

编辑:rootadmin

js中endswith是什么呢?不知道的小伙伴来看看小编今天的分享吧!
613/auto1686643668js endswith(js endswith()用法)

1、js中endswit简介:

endsWith()方法用来判断当前字符串是否是以另外一个给定的子字符串“结尾”的,根据判断结果返回 true 或 false。

The source for this interactive example is stored in a GitHub repository. If you’d like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

Syntaxstr.endsWith(searchString[, length])

2、参数:

searchString

要搜索的子字符串。

length

可选。作为 str 的长度。默认值为 str.length。

返回值

如果传入的子字符串在搜索字符串的末尾则返回true;否则将返回 false。

描述

这个方法帮助你确定一个字符串是否在另一个字符串的末尾。这个方法是大小写敏感的。

示例:

使用 endsWith()var str = "To be, or not to be, that is the question.";

alert( str.endsWith("question.") ); // true

alert( str.endsWith("to be") ); // false

alert( str.endsWith("to be", 19) ); // true

Polyfill

这个方法已经加入到 ECMAScript 6 标准当中,但是可能还没有在所有的 JavaScript 实现中可用。然而,你可以通过如下的代码片段扩展 String.prototype.endsWith() 实现兼容:if (!String.prototype.endsWith) {undefined

String.prototype.endsWith = function(search, this_len) {undefined

if (this_len === undefined || this_len > this.length) {undefined

this_len = this.length;

}

return this.substring(this_len - search.length, this_len) === search;

};

}

以上就是小编今天的分享了,希望可以帮助到大家。

本文链接地址:https://www.jiuchutong.com/zhishi/2785.html 转载请保留说明!

上一篇:js assign(jsassign函数的使用方法)

下一篇:js lastindexof

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络