padStart()
method pads the current string with another string (multiple times, if needed) until the resulting string reaches the given length.
The padding is applied from the start of the current string.
str.padStart(targetLength [, padString])
targetLength
str
has been padded. If the value is less than
str.
length
, then
str
is returned as-is.
padString
可选
str
with. If
padString
is too long to stay within the
targetLength
, it will be truncated from the end. The default value is
" "
(
U+0020 'SPACE'
).
A
String
of the specified
targetLength
with
padString
applied from the start.
'abc'.padStart(10); // " abc" 'abc'.padStart(10, "foo"); // "foofoofabc" 'abc'.padStart(6,"123465"); // "123abc" 'abc'.padStart(8, "0"); // "00000abc" 'abc'.padStart(1); // "abc"
// Javascript version of: (unsigned)
// printf "%0*d" width num
function leftFillNum(num, targetLength) {
return num.toString().padStart(targetLength, 0);
}
const num = 123;
console.log(leftFillNum(num, 5));
// expected output: "00123"
| 规范 |
|---|
|
ECMAScript (ECMA-262)
The definition of 'String.prototype.padStart' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
padStart
|
Chrome 57 | Edge 15 | Firefox 48 | IE No | Opera 44 | Safari 10 | WebView Android 57 | Chrome Android 57 | Firefox Android 48 | Opera Android 43 | Safari iOS 10 | Samsung Internet Android 7.0 |
nodejs
8.0.0
|
完整支持
不支持
用户必须明确启用此特征。
String
String.fromCharCode()
String.fromCodePoint()
String.prototype.anchor()
String.prototype.big()
String.prototype.blink()
String.prototype.bold()
String.prototype.charAt()
String.prototype.charCodeAt()
String.prototype.codePointAt()
String.prototype.concat()
String.prototype.endsWith()
String.prototype.fixed()
String.prototype.fontcolor()
String.prototype.fontsize()
String.prototype.includes()
String.prototype.indexOf()
String.prototype.italics()
String.prototype.lastIndexOf()
String.prototype.link()
String.prototype.localeCompare()
String.prototype.match()
String.prototype.matchAll()
String.prototype.normalize()
String.prototype.padEnd()
String.prototype.padStart()
String.prototype.repeat()
String.prototype.replace()
String.prototype.replaceAll()
String.prototype.search()
String.prototype.slice()
String.prototype.small()
String.prototype.split()
String.prototype.startsWith()
String.prototype.strike()
String.prototype.sub()
String.prototype.substr()
String.prototype.substring()
String.prototype.sup()
String.prototype.toLocaleLowerCase()
String.prototype.toLocaleUpperCase()
String.prototype.toLowerCase()
String.prototype.toSource()
String.prototype.toString()
String.prototype.toUpperCase()
String.prototype.trim()
String.prototype.trimEnd()
String.prototype.trimStart()
String.prototype.valueOf()
String.prototype[@@iterator]()
String.raw()
Function
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()