首页 > 其他 > 详细

ES6系列--【ES6 新增字符串方法】

时间:2021-06-13 23:49:53      阅读:34      评论:0      收藏:0      [点我收藏+]

1、简单使用

  • includes()返回布尔值,表示是否找到了参数字符串
  • startsWith()返回布尔值,表示参数字符串是否在源字符串的头部
  • endsWith()返回布尔值,表示参数字符串是否在源字符串的尾部
技术分享图片
 let str="lxy";
        //字符串是否以某个字符开头
        console.log(str.startsWith(‘l‘));//true
        console.log(str.startsWith(‘x‘));//false
        //字符串是否以某个字符结尾
        console.log(str.endsWith(‘x‘));//false
        console.log(str.endsWith(‘y‘));//true
        //字符串是否包含某个字符
        console.log(str.includes(‘x‘));//true
        console.log(str.includes(‘z‘));//false
        //repeat()重复某个字符串几次
        console.log(str.repeat(3));//lxylxylxy
        console.log(str.repeat(5));//lxylxylxylxylxy
技术分享图片

2、第2个参数

includes(),startsWith(),endsWith()都支持第2个参数。

使用第2个参数n时,endsWith的行为与其他两个方法有所不同。

  • endsWith针对前n个字符。
  • 其他2个方法:针对从第n个位置直到字符串结束的字符。
var s="hello world!";
s.startsWith(‘world‘,6);//true
s.endsWith(‘hello‘,5);//true
s.includes(‘hello‘,6);//false

技术分享图片

ES6系列--【ES6 新增字符串方法】

原文:https://www.cnblogs.com/chenhaiyun/p/14881378.html

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