padStart、padEnd:补全字符串长度
{
let str = 'i';
let str1 = str.padStart(5, 'mooc');//往后补
console.log(str1);
let str2 = str.padEnd(5, 'mooc');往前补
console.log(str2);
}
repeat:重复,带负数会报错,带小数取整
{
console.log(‘i’.repeat(10));//对i重复10遍
//写一个repeat方法
function repeat(str, num) {
return new Array(num + 1).join(str);
}
console.log(repeat('s', 3));//对s重复3遍
}
startsWith、endsWith:判断字符串以…开头/结尾,返回布尔值
{
const str = 'A promise is a promsie';
console.log(str.startsWith('B'));
console.log(str.startsWith('A pro'));
console.log(str.endsWith('promsie'));
console.log(str.endsWith('A'));
}
includes:判断是否存在某个字符串
{
const str = 'A promise is a promise';
//用indexof判断
// if (str.indexOf('promise') !== -1) {
if (~str.indexOf('promise')) {
console.log('存在1');
}
// 用includes判断
if (str.includes('a promise')) {
console.log('存在2');
}
}