脚本宝典收集整理的这篇文章主要介绍了【ES6入门03】:字符串扩展,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
字符串扩展
1.str.includes() str.startsWITh() str.endsWith()
{
let str = 'string';
// 判断字符串中是否包含某一字符
console.LOG('includes', str.includes('r')); // true
// 判断字符串是否以某一字符开始
console.log('start', str.startsWith('str')); // true
// 判断字符串是否以某一字符结束
console.log('end', str.endsWith('ng')); // true
}
2.str.rePEat()
{
// repeat方法用于字符串复制
let str = 'abc';
console.log(str.repeat(3)); // abcabcabc
}
3.str.padStart() 和 str.padEnd()
{
// 字符串补白,日期、时间、金额经常用到
console.log('1'.padStart(2, '0')); // 01
console.log('12'.padStart(2, '0')); // 12
console.log('1'.padEnd(2, '0')); // 10
}
4.模板字符串
{
let name = 'can';
let info = 'hello beautiful girl';
let temp = `i am ${name}, ${info}`;
console.log(temp); // i am can, hello beautiful girl
}
{
let user = {
name: 'can',
info: 'hello beautiful girl'
};
abc`i am ${user.name}, ${user.info}`;
function abc(s, v1, v2) {
console.log(s, v1, v2); // ["i am ", ", ", ""] "can" "hello beautiful girl"
return s + v1 + v2;
}
}
6.String.raw
{
// raw方法对做了转义
console.log(String.raw`Hin${1 + 2}`); // Hin3
console.log(`Hin${1 + 2}`);
// Hi
// 3
}
以上是脚本宝典为你收集整理的【ES6入门03】:字符串扩展全部内容,希望文章能够帮你解决【ES6入门03】:字符串扩展所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。