首页 > 其他 > 详细

ES6字符串包含方法

时间:2021-04-18 14:41:45      阅读:19      评论:0      收藏:0      [点我收藏+]

5.3.3 String

5.字符串包含方法

ECMAScript6增加了3个用于判断字符串是否包含另一个字符串的方法:startsWith()、endsWith()和includes()。

let message = "foobarbaz";
console.log(message.startsWith("foo")); // true
console.log(message.startsWith("bar")); // false
console.log(message.endsWith("baz")); // true
console.log(message.endsWith("bar")); // false
console.log(message.includes("bar")); // true
console.log(message.includes("qux")); // false

startsWith() 和 includes() 方法接收可选的第二个参数,表示
开始搜索的位置。如果传入第二个参数,则意味着这两个方法会从指定
位置向着字符串末尾搜索,忽略该位置之前的所有字符。下面是一个例
子:

let message = "foobarbaz";
console.log(message.startsWith("foo")); //true
console.log(message.startsWith("foo", 1)); //false
console.log(message.includes("bar")); //true
console.log(message.includes("bar", 4)); //false
console.log(message.startsWith("bar", 3));// true

endsWith() 方法接收可选的第二个参数,表示应该当作字符串末尾
的位置。如果不提供这个参数,那么默认就是字符串长度。如果提供这
个参数,那么就好像字符串只有那么多字符一样:

let message = "foobarbaz";
console.log(message.endsWith("bar")); //false
console.log(message.endsWith("bar", 6)); // true

 

ES6字符串包含方法

原文:https://www.cnblogs.com/huanghuali/p/14673119.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!