zoukankan      html  css  js  c++  java
  • es6 新增字符串方法

    es6新增了4个字符串处理的方法:startsWith,endsWith,includes,repeat。

    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个参数[update20170606]

    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

    本文作者starof,因知识本身在变化,作者也在不断学习成长,文章内容也不定时更新,为避免误导读者,方便追根溯源,请诸位转载注明出处:http://www.cnblogs.com/starof/p/6919733.html有问题欢迎与我讨论,共同进步。 

  • 相关阅读:
    微信小程序UI组件、开发框架、实用库...
    关于Web中的图标使用问题
    通过jQuery Ajax使用FormData对象上传文件
    css3 动画demo
    css知识汇总
    git初体验(三)git分支
    js数组去重的三种常用方法总结
    JavaScript继承方式详解
    面试题连接收集
    城市列表的实例
  • 原文地址:https://www.cnblogs.com/starof/p/6919733.html
Copyright © 2011-2022 走看看