ES6中新增String方法(部分)

startswith

  • 判断字符串是否以指定字符串片段开头

    var hello = 'My name is Ashen';
    hello.startswith('My')//返回true
  • 大小写敏感

    hello.startswith('my')//返回false
  • 可以指定第二个参数,指定查询字符串开始位置的索引

    //从索引为3的地方开始
    hello.startswith('name',3)//返回true

endsWith

  • 判断字符串是否以指定字符串片段结尾

    var id = '20172281';
    id.endsWith('1');//返回true
  • 大小写敏感

  • 可以指定第二个参数,指定截取原查询字符串的长度

    //截取'2017228'进行查询
    id.endsWith('1',7);//返回true

includes

  • 判断字符串是否包含指定字符串片段

    hello.includes('name')//返回true
  • 可以指定第二个参数,从字符串的指定索引向后查询

    hello.includes('name',3)//返回true

repeat

  • 使字符串重复,指定次数

    '哈'.repeat(10)//"哈哈哈哈哈哈哈哈哈哈"
  •  

原文地址:https://www.cnblogs.com/ashen1999/p/12559783.html