ES6新特性之字符串扩展
生活随笔
收集整理的這篇文章主要介紹了
ES6新特性之字符串扩展
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
字符串擴展
在ES6中,為字符串擴展了幾個新的API:
includes() :返回布爾值,表示是否找到了參數字符串。
startsWith() :返回布爾值,表示參數字符串是否在原字符串的頭部。
endsWith() :返回布爾值,表示參數字符串是否在原字符串的尾部。
實驗一下:
<script>let str = "hello leon";console.log(str, "中是否包含了 leon", str.includes("leon"));console.log(str, "中是否包含了 andy", str.includes("andy"));console.log(str, "中是否以 hello 開頭", str.startsWith("hello"));console.log(str, "中是否以 andy 開頭", str.startsWith("andy"));console.log(str, "中是否以 hello 結束", str.endsWith("hello"));console.log(str, "中是否以 leon 結束", str.endsWith("leon"));let str2 = "abc 123";</script>字符串模板
ES6中提供了`來作為字符串模板標記。我們可以這么玩:
?
?
?
總結
以上是生活随笔為你收集整理的ES6新特性之字符串扩展的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ES6新特性之let和const命令
- 下一篇: ES6新特性之解构表达式