JavaScript endsWith() 方法使用示例:检查字符串是否以特定子字符串结尾
if(a.endsWith(b)){ // do something }
该代码示例展示了 endsWith() 方法的使用,用于判断字符串 a 是否以字符串 b 结尾。
方法说明:
endsWith()方法用于检查字符串是否以特定子字符串结尾。- 如果字符串以指定子字符串结尾,则返回
true,否则返回false。
示例:
const str = 'Hello world';
console.log(str.endsWith('world')); // true
console.log(str.endsWith('hello')); // false
应用场景:
- 验证用户输入是否以特定格式结尾。
- 提取文件扩展名。
- 检查 URL 是否以特定域名结尾。
原文地址: https://www.cveoy.top/t/topic/pRgC 著作权归作者所有。请勿转载和采集!