ES6-2-字符串

1、includes:判断是否包含然后直接返回布尔值

const str = 'hahahhy';
	console.log(str.includes('y'));//true

2、获取字符串重复n次结果

const str1 = 'he';
	console.log(str1.repeat(3));//'hehehe'

如果你带入小数,Math.floor(num)来处理
s.repeat(3.1) 或者s.repeat(3.9)都当做成s.repeat(3)来处理

3、startsWith 和 endsWith 判断是否以给定的文本 开始或者结束

const str2 = "helloworld!";
	console.log(str2.startsWith('hello'));//true
	console.log(str2.endsWith('!'));//true

4、padStart 和 padEnd 填充字符串,应用场景:时分秒

  • 如果原字符串的长度,等于或大于指定的最小长度,则返回原字符串。

  • 如果用来补全的字符串与原字符串,两者的长度之和超过了指定的最小长度,则会截去超出位数的补全字符串。

  • 如果省略第二个参数,默认使用空格补全长度。

  • 另一个用途是提示字符串格式。

    ‘12’.padStart(10, ‘YYYY-MM-DD’) // “YYYY-MM-12”
    ‘09-12’.padStart(10, ‘YYYY-MM-DD’) // “YYYY-09-12”

     setInterval(function(){
     	const now = new Date();
     	const hours = now.getHours().toString();
     	const minutes = now.getMinutes().toString();
     	const seconds = now.getSeconds().toString();
     	console.log(`${hours.padStart(2,0)}:${minutes.padStart(2,0)}:${seconds.padStart(2,0)}`);
     },5000);
    

作者:陈嘻嘻啊
链接:https://www.jianshu.com/p/287e0bb867ae
來源:简书

猜你喜欢

转载自blog.csdn.net/weixin_43742121/article/details/84569001