ES6 - 字符串扩展

  • includes(), startsWith(), endsWith()
    传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中。
    ES6又提供了三种新方法。
    includes():返回布尔值,表示是否找到了参数字符串。
["aaa", "bbb"].includes("aaa")
true
["aaa", "bbb"].includes("ccc")
false
"abc".includes("b")
true
"abc".includes("d")
false

startsWith():返回布尔值,表示参数字符串是否在源字符串的头部。

"abc".startsWith("a")
true

endsWith():返回布尔值,表示参数字符串是否在源字符串的尾部。

"abc".endsWith("c")
true

猜你喜欢

转载自blog.csdn.net/seaalan/article/details/89216998