关于es6:ES6字符串拓展的方法

6次阅读

共计 2705 个字符,预计需要花费 7 分钟才能阅读完成。

2019.05.27 10:14

一、子串的辨认

ES6 之前判断字符串是否蕴含子串,用 indexOf 办法,ES6 新增了子串的识别方法。
1、includes():返回布尔值,判断是否找到参数字符串。
2、startsWith():返回布尔值,判断参数字符串是否在原字符串的头部。
3、endsWith():返回布尔值,判断参数字符串是否在原字符串的尾部。
以上三个办法都能够承受两个参数,须要搜寻的字符串,和可选的搜寻起始地位索引。

留神:
这三个办法只返回布尔值,如果须要晓得子串的地位,还是得用 indexOflastIndexOf
这三个办法如果传入了正则表达式而不是字符串,会抛出谬误。而 indexOflastIndexOf 这两个办法,它们会将正则表达式转换为字符串并搜寻它。

例:

let string = "apple,banana,orange";
string.includes("banana");     // true
string.startsWith("apple");    // true
string.endsWith("apple");      // false
string.startsWith("banana",6)  // true

二、字符串反复

repeat() 将字符串反复指定次数后返回新的字符串。

// 将字符串反复指定次数后返回新的字符串
console.log("Hello,".repeat(2));  // "Hello,Hello,"

// 如果参数是 0 至 -1 之间的小数,会进行取整运算,0 至 -1 之间的小数取整失去 -0,等同于 repeat 零次
console.log("Hello,".repeat(3.2));  // "Hello,Hello,Hello,"

// 如果参数是 0 至 -1 之间的小数,会进行取整运算,0 至 -1 之间的小数取整失去 -0,等同于 repeat 零次
console.log("Hello,".repeat(-0.5));  // ""

// 如果参数是 NaN,等同于 repeat 零次
console.log("Hello,".repeat(NaN));  // ""

// 如果参数是正数或者 Infinity,会报错:
console.log("Hello,".repeat(-1));  // RangeError: Invalid count value
console.log("Hello,".repeat(Infinity));  // RangeError: Invalid count value

// 如果传入的参数是字符串,则会先将字符串转化为数字
console.log("Hello,".repeat("hh")); // ""console.log("Hello,".repeat("2"));  //"Hello,Hello,"

三、字符串补全

1、padStart():返回新的字符串,示意用参数字符串从头部补全原字符串。
2、padEnd():返回新的字符串,示意用参数字符串从尾部补全原字符串。
以上两个办法承受两个参数,第一个参数是指定生成的字符串的最小长度,第二个参数是用来补全的字符串。如果没有指定第二个参数,默认用空格填充。

console.log("h".padStart(5,"o"));  // "ooooh"
console.log("h".padEnd(5,"o"));    // "hoooo"
console.log("h".padStart(5));      // "h"

// 如果指定的长度大于或者等于原字符串的长度,则返回原字符串:
console.log("hello".padStart(5,"A"));  // "hello"

// 如果原字符串加上补全字符串长度大于指定长度,则截去超出位数的补全字符串:
console.log("hello".padEnd(10,",world!"));  // "hello,worl"

// 罕用于补全位数:console.log("123".padStart(10,"0"));  // "0000000123"

四、模板字符串

模板字符串相当于加强版的字符串,用反引号 `,除了作为一般字符串,还能够用来定义多行字符串,还能够在字符串中退出变量和表达式。

留神:模板字符串中的换行和空格都是会被保留的

// 一般字符串
let string = `Hello'\n'world`;
console.log(string); 
// "Hello'
// 'world"

// 多行字符串:
let string1 =  `Hey,
can you stop angry now?`;
console.log(string1);
// Hey,
// can you stop angry now?

// 字符串插入变量和表达式。// 变量名写在 ${} 中,${} 中能够放入 JavaScript 表达式。let name = "Mike";
let age = 27;
let info = `My Name is ${name},I am ${age+1} years old next year.`
console.log(info);
// My Name is Mike,I am 28 years old next year.

function f(){return "have fun!";}
let string2= `Game start,${f()}`;
console.log(string2);  
// Game start,have fun!

innerHtml = `<ul>
  <li>menu</li>
  <li>mine</li>
</ul>
`;
console.log(innerHtml);
// 输入
<ul>
 <li>menu</li>
 <li>mine</li>
</ul>

五、标签模板

标签模板,是一个函数的调用,其中调用的参数是模板字符串。

alert`Hello world!`;
// 等价于
alert('Hello world!');

当模板字符串中带有变量,会将模板字符串参数解决成多个参数。

function f(stringArr,...values){
 let result = "";
 for(let i=0;i<stringArr.length;i++){result += stringArr[i];
  if(values[i]){result += values[i];
        }
    }
 return result;
}

let name = 'Mike';
let age = 27;
f`My Name is ${name},I am ${age+1} years old next year.`;
// 等价于
f(['My Name is',',I am','years old next year.'],'Mike',28);
// "My Name is Mike,I am 28 years old next year."

正文完
 0