ES6——字符串新方法(部分)

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');
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值