码迷,mamicode.com
首页 > 编程语言 > 详细

JavaScript开发中使用频率较高的一些方法

时间:2017-10-28 12:47:26      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:aaa   头部   推出   字符   prot   script   计时   dstar   str   

1、填充字符串

ES7推出了字符串补全长度的功能。如果某个字符串不够指定长度,会在头部或尾部补全。

String.prototype.padStart(maxLength, fillString=’ ‘)

该方法是在字符串之前使用 fillString 填充,直到字符串长度为 maxLength 。

String.prototype.padEnd(maxLength, fillString=’ ‘)

该方法是在字符串之后使用 fillString填充,直到字符串长度为 maxLength 。

以在字符串之前填充指定字符为例:

1)字符串的长度小于maxLength,在字符串之前填充指定的字符。

‘hello‘.padStart(10,‘a‘);// 返回"aaaaahello"

做时间倒计时的功能,如果数字是一位数,可以把它转换成字符串类型,然后在前面填充0。

String(num).padStart(2,0)

String(1).padStart(2,0) //输出‘01’
String(11).padStart(2,0)//输出‘11‘

2)如果省略了 fillString ,则使用一个单独空格字符串(”)代替。

‘hello‘.padStart(10); //返回"     hello"

3)如果字符串的长度大于等于 maxLength ,则返回原始字符串。

‘hello‘.padStart(5,‘a‘);//返回"hello"

 

JavaScript开发中使用频率较高的一些方法

标签:aaa   头部   推出   字符   prot   script   计时   dstar   str   

原文地址:http://www.cnblogs.com/happypayne/p/7742532.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!