标签: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"
标签:aaa 头部 推出 字符 prot script 计时 dstar str
原文地址:http://www.cnblogs.com/happypayne/p/7742532.html