String padding

简介: String padding

String.prototype.padStart

MDN 文档

简单解释: padStart() 方法用另一个字符串填充当前字符串(如果需要的话,会重复多次),以便产生的字符串达到给定的长度。从当前字符串的左侧开始填充。


语法:

str.padStart(targetLength[, padString])


参数

  • targetLength

当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。


  • padString | 可选

填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分,其他部分会被截断。此参数的缺省值为 " "(U+0020)。


console.log('hello'.padStart(10)) // '     hello'
console.log('hello'.padStart(10, '1234')) // '12341hello'
console.log('hello'.padStart()) //'hello'
console.log('hello'.padStart(6, '123')) //'1hello'
console.log('hello'.padStart(3)) // 'hello'
console.log('hello'.padStart(3, '123')) // 'hello'


String.prototype.padEnd

MDN 文档

简单解释: padEnd() 方法会用一个字符串填充当前字符串(如果需要的话则重复填充),返回填充后达到指定长度的字符串。从当前字符串的末尾(右侧)开始填充。


语法:

str.padEnd(targetLength[, padString])


参数

  • targetLength

当前字符串需要填充到的目标长度。如果这个数值小于当前字符串的长度,则返回当前字符串本身。

  • padString | 可选

填充字符串。如果字符串太长,使填充后的字符串长度超过了目标长度,则只保留最左侧的部分,其他部分会被截断。此参数的缺省值为 " "(U+0020)。


console.log('hello'.padEnd(10)) // 'hello     '
console.log('hello'.padEnd(10, '1234')) //'hello12341'
console.log('hello'.padEnd()) // 'hello'
console.log('hello'.padEnd(6, '123')) // 'hello1'
console.log('hello'.padEnd(3)) // 'hello'
console.log('hello'.padEnd(3, '123')) // 'hello'
目录
相关文章
For input string: "null"
java.lang.NumberFormatException: For input string: "null"   在开发中你是否遇到过这样的问题,不管请求到的值是什么都能进入不为null或”“的判断中,如下例:      Stringtemp=req.
3914 0
|
4月前
|
Java
String.format()
String.format()
|
5月前
|
存储 编译器 C++
【C++】String -- 详解(下)
【C++】String -- 详解(下)
|
5月前
|
存储 JavaScript C语言
【C++】String -- 详解(上)
【C++】String -- 详解(上)
|
JavaScript
String.match()
String.match()
112 0
string.match
string.match
107 0
|
vr&ar
String转Color
原文:String转Color 很硬性的转换,谁知道更好的忘不吝赐教啊。 /// /// String To Color /// /// 例如#FFFF0000,红色 /// public Color ReturnColorFromString(string color) { //#FFFF0000,A,R,G,B。
886 0