ES6字符串方法

  ES6字符串提供三个函数确定一个字符串是否包含在另一个字符串中,分别是includes()、startsWith()、endsWith(),这三种方法都返回一个布尔值。

includes()方法表示是否找到了参数字符串,startsWith()表示是否在开始搜索位置找到了字符串,endsWith()表示是否在结尾的位置搜索到字符串,开启代码验证
  示例代码
    
var a = "ceshi"; 
console.log(a.includes('ce')); //true console.log(a.includes('aaa')); //false console.log(a.startsWith('c')); //true console.log(a.startsWith('e')); //false console.log(a.endsWith('i')); //true console.log(a.endsWith('a')); //false console.log("/*includes()、startsWith()接受两个参数,表示从第几个开始搜索*/"); console.log("/*endsWith()接受两个参数,表示搜索到第几个字符*/"); console.log(a.includes('c',0)); //true console.log(a.includes('c',3)); //false console.log(a.startsWith('c',0)); //true console.log(a.startsWith('c',4)); //false console.log(a.endsWith('i',6)); //true console.log(a.endsWith('i',4)); //false
复制字符串并返回一个新的字符串使用repeat()方法
var a = 'a';
console.log(a.repeat(3)); //aaa
  ES7提供了字符串补全padStart()、padEnd(),前者是在头部补字符串后者是在尾部补全字符串。
    示例代码
      
var a = 'a';
 /*如果在开头或结尾补位,要补的字符串高于补的位数,则取字符串前头 补的字符串。例如:如果你要补的字符串的位数是4位,现在有1位字符, 则在开头应再补三位,如果补充的字符串大于三位,则只取三位补 充到开头,剩余的字符串则丢弃 */ 
console.log(a.padStart(4,'bbb')); //bbba 
console.log(a.padEnd(4,'bbb')); //abbb 
/*如果不指定第二个参数则用空格来补全*/ 
console.log(a.padStart(4)); // a
此外还有字符串模板和标签模板,这里暂且不提,至于为什么我想洗脚睡觉去了  哈哈哈哈哈哈哈哈
原文地址:https://www.cnblogs.com/qiaohong/p/7705037.html