1.对象inclues()的用法
- 基本用法
判断字符串中是否含有某些字符
console.log('abc'.includes('a')); //true
console.log('abc'.includes('b')); //true
console.log('abc'.includes('ab')); //true
console.log('abc'.includes('ac')); //false
- 第二个参数
表示开始搜索的位置,默认是0
console.log('abc'.includes('a')); //true
console.log('abc'.includes('b', 0)); //true
console.log('abc'.includes('b', 2)); //false
- includes的应用
//https://juejin.cn
//https://juejin.cn/?sort=newest
let url = 'https://juejin.cn?';
const num = url.length;
const check = url.substring(num - 1);
const addURLparam = (url, name, value) => {
if (check === '?') {
url = url.substring(0, num - 1);
}
url += url.includes('?') ? '&' : '?';
url += `${
name}=${
value}`;
return url;
}
url = addURLparam(url, 'sort', 'newest');
console.log(url); //https://juejin.cn?sort=newest
// url = addURLparam(url, 'sort', 'newest');
// console.log(url);
2.padStart()和padEnd()
- 基本用法
console.log('x'.padStart(5, 'ab')); //ababx
console.log('x'.padEnd(5, 'ab')); //xabab
- 注意事项
原字符串的长度,等于或大于最大长度,不会消减原字符串,字符串补全不生效,返回原字符串
console.log('xxx'.padStart(2, 's')); //xxx
console.log('xxx'.padEnd(2, 's')); //xxx
用来补全的字符串与原字符串长度之和超过了最大长度,截去超出位数的补全字符串,原字符串不动
console.log('abc'.padStart(10, '0123456789')); //0123456abc
console.log('abc'.padEnd(10, '0123456789')); //abc0123456
如果省略第二个参数,默认使用空格补全长度
console.l