pFad - Phone/Frame/Anonymizer/Declutterfier! Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

URL: http://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith

cend-cdn.com/cm/d556c3a1-e57c-4bdf-a490-390a1aebf6dd/airgap.js" >

此页面由社区从英文翻译而来。了解更多并加入 MDN Web Docs 社区。

View in English Always switch to English

String.prototype.endsWith()

基线 广泛可用

自 2015年9月 起,此特性已在主流浏览器中得到支持,可在大多数设备和浏览器版本中正常使用。

endsWith() 方法用于判断一个字符串是否以指定字符串结尾,如果是则返回 true,否则返回 false

尝试一下

const str1 = "Cats are the best!";

console.log(str1.endsWith("best!"));
// Expected output: true

console.log(str1.endsWith("best", 17));
// Expected output: true

const str2 = "Is this a question?";

console.log(str2.endsWith("question"));
// Expected output: false

语法

js
endsWith(searchString)
endsWith(searchString, endPosition)

参数

searchString

要搜索的作为结尾的字符串,不能是正则表达式。所有非正则表达式的值都会被强制转换为字符串,因此如果该参数被省略或传入 undefinedendsWith() 方法会在字符串中搜索 "undefined",这通常不是你想要的。

endPosition 可选

预期找到 searchString 的末尾位置(即 searchString 最后一个字符的索引加 1)。默认为 str.length

返回值

如果被检索字符串的末尾出现了指定的字符串(包括 searchString 为空字符串的情况),则返回 true;否则返回 false

异常

TypeError

如果 searchString 是一个正则表达式,则会抛出。

描述

这个方法可以让你确定一个字符串是否以另一个字符串结尾。该方法区分大小写。

示例

使用 endsWith()

js
const str = "生存还是毁灭,这是一个问题。";

console.log(str.endsWith("问题。")); // true
console.log(str.endsWith("毁灭")); // false
console.log(str.endsWith("毁灭", 6)); // true

规范

规范
ECMAScript® 2027 Language Specification
# sec-string.prototype.endswith

浏览器兼容性

参见

pFad - Phonifier reborn

Pfad - The Proxy pFad © 2024 Your Company Name. All rights reserved.





Check this box to remove all script contents from the fetched content.



Check this box to remove all images from the fetched content.


Check this box to remove all CSS styles from the fetched content.


Check this box to keep images inefficiently compressed and original size.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy