ES6语法快速入门五

字符串新增方法:

1.includes;

str.includes(valueToFind[,fromIndex])判断字符串是否包含一个指定的指。

<code>  let str="abc";
console.log(str.includes('a'));
/<code>

2.startsWith;

str.startsWith(searchString[,position])判断当前字符串是否是以另外一个给定的子字符串“开头”;参数:searchString 要搜索的子字符串可选参数:position:在str中搜索searchString的开始位置,默认值为0,也就算真正的字符串开头处。返回值:如果传入的子字符串在搜索字符串的开始则返回true,否则将返回false。

<code>let str='语文数学两门课';
console.log(str.startsWith("数学",2));
/<code>

3.endsWith;

str.endsWith(searchString[,length]判断当前字符串是否以另外一个给定的子字符串结尾。参数:searchString要搜索的子字符串。可选参数length作为str的长度,默认值为str.length;

<code>let str='语文数学两门课';
console.log(str.endsWith("课"));
/<code>

4.repeat;

str.repeat(count)构造并返回一个新字符串,该字符串包含被连接在一起的指定数量的字符串的副本参数:count 介于0与正无穷大之间的整数。表示在新构造的字符串中重复了多少遍原字符串

<code>let str="a";
console.log(str.repeat(10));/<code>


分享到:


相關文章: