ES6 字符串

模板字符串

第一个用途,基本的字符串格式化。将表达式嵌入字符串中进行拼接。用${}来界定。

	//ES5 
    	var name = 'hjx'
    	console.log('hello' + name)
    	//es6
    	const name = 'hjx'
    	console.log(`hello ${name}`) //hello hjx

第二个用途,在ES5时我们通过反斜杠()来做多行字符串或者字符串一行行拼接。ES6反引号(``)直接搞定。

	// ES5
    	var msg = "Hi \
    	man!
    	"
    	// ES6
    	const template = `<div>
        	<span>hello world</span>
    	</div>`

字符串 ES6+

 	// 1.includes:判断是否包含然后直接返回布尔值
    	const str = 'hahay'
    	console.log(str.includes('y')) // true

    	// 2.repeat: 获取字符串重复n次
    	const str = 'he'
    	console.log(str.repeat(3)) // 'hehehe'
    	//如果你带入小数, Math.floor(num) 来处理
    	// s.repeat(3.1) 或者 s.repeat(3.9) 都当做成 s.repeat(3) 来处理

    	// 3. startsWith 和 endsWith 判断是否以 给定文本 开始或者结束
    	const str =  'hello world!'
    	console.log(str.startsWith('hello')) // true
    	console.log(str.endsWith('!')) // true
    
    	// 4. padStart 和 padEnd 填充字符串,应用场景:时分秒
    	setInterval(() => {
        	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)}`)
    	}, 1000)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值