利用淘宝做网站卖货到国外,互动营销网站建设,wordpress网格化插件,佛山做网站多少钱子串识别 自从 JS 引入了 indexOf() 方法#xff0c;开发者们就使用它来识别字符串是否存在于其它字符串中。ES6 包含了以下三个方法来满足这类需求#xff1a;includes()、startsWith()、endsWith()
【includes()】 该方法在给定文本存在于字符串中的任意位置时会返回 true…子串识别 自从 JS 引入了 indexOf() 方法开发者们就使用它来识别字符串是否存在于其它字符串中。ES6 包含了以下三个方法来满足这类需求includes()、startsWith()、endsWith()
【includes()】 该方法在给定文本存在于字符串中的任意位置时会返回 true 否则返回false
【startsWith()】 该方法在给定文本出现在字符串起始处时返回 true 否则返回 false
【endsWith()】 该方法在给定文本出现在字符串结尾处时返回 true 否则返回 false 以上每个方法都接受两个参数需要搜索的文本以及可选的搜索起始位置索引 当提供了第二个参数(假设为n)时 includes() 与 startsWith() 方法会从该索引位置(n)开始尝试匹配而endsWith() 方法则从字符串长度减去这个索引值的位置开始尝试匹配 当第二个参数未提供时 includes() 与 startsWith() 方法会从字符串起始处开始查找而 endsWith() 方法则从尾部开始。实际上第二个参数减少了搜索字符串的次数
var msg Hello world!;
console.log(msg.startsWith(Hello)); // true
console.log(msg.endsWith(!)); // true
console.log(msg.includes(o)); // trueconsole.log(msg.startsWith(o)); // false
console.log(msg.endsWith(world!)); // true
console.log(msg.includes(x)); // falseconsole.log(msg.startsWith(o, 4)); // true
console.log(msg.endsWith(o, 5)); // true
console.log(msg.includes(o, 8)); // false 虽然这三个方法使得判断子字符串是否存在变得更容易但它们只返回了一个布尔值。若需要找到它们在字符串中的确切位置则需要使用 indexOf() 和 lastIndexOf() [注意]如果向 startsWith() 、 endsWith() 或 includes() 方法传入了正则表达式而不是字符串会抛出错误。而对于indexOf()和lastIndexOf()这两个方法它们会将正则表达式转换为字符串并搜索它 字符串重复
【repeat()】 ES6为字符串添加了一个 repeat() 方法它接受一个参数作为字符串的重复次数返回一个将初始字符串重复指定次数的新字符串
console.log(x.repeat(3)); // xxx
console.log(hello.repeat(2)); // hellohello
console.log(abc.repeat(4)); // abcabcabcabc 参数如果是小数会被取整
console.log(na.repeat(2.9)); // nana 如果repeat的参数是负数或者Infinity会报错
//Uncaught RangeError: Invalid count value
console.log(na.repeat(Infinity));
//Uncaught RangeError: Invalid count value
console.log(na.repeat(-1)); 如果参数是0到-1之间的小数则等同于0这是因为会先进行取整运算。0到-1之间的小数取整以后等于-0repeat视同为0
console.log(na.repeat(-0.9)); // 参数NaN等同于0
console.log(na.repeat(NaN)); // 如果repeat的参数是字符串则会先转换成数字
console.log(na.repeat(na)); //
console.log(na.repeat(3)); // nanana
【创建缩进级别】 此方法比相同目的的其余方法更加方便在操纵文本时特别有用尤其是在需要产生缩进的代码格式化工具中
// 缩进指定数量的空格
var indent .repeat(4),
indentLevel 0;
// 需要增加缩进时
var newIndent indent.repeat(indentLevel); 调用第一个repeat()方法创建了一个包含四个空格的字符串indentLevel变量用来持续追踪缩进的级别。此后可以通过增加indentLevel的值来调用repeat() 方法从而改变空格数量 字符串补全 ES2017 引入了字符串补全长度的功能。如果某个字符串不够指定长度会在头部或尾部补全
【padStart()】 头部补全
【padEnd()】 尾部补全 padStart()和padEnd()一共接受两个参数第一个参数用来指定字符串的最小长度第二个参数是用来补全的字符串
x.padStart(5, ab) // ababx
x.padStart(4, ab) // abaxx.padEnd(5, ab) // xabab
x.padEnd(4, ab) // xaba 如果省略第二个参数默认使用空格补全长度
x.padStart(4) // x
x.padEnd(4) // x 如果原字符串的长度等于或大于指定的最小长度则返回原字符串
xxx.padStart(2, ab) // xxx
xxx.padEnd(2, ab) // xxx 如果用来补全的字符串与原字符串两者的长度之和超过了指定的最小长度则会截去超出位数的补全字符串
abc.padStart(10, 0123456789)// 0123456abc
【应用】 padStart的常见用途是为数值补全指定位数。下面代码生成10位的数值字符串
1.padStart(10, 0) // 0000000001
12.padStart(10, 0) // 0000000012
123456.padStart(10, 0) // 0000123456 另一个用途是提示字符串格式
12.padStart(10, YYYY-MM-DD) // YYYY-MM-12
09-12.padStart(10, YYYY-MM-DD) // YYYY-09-12