<分区>
Possible Duplicate:
Is there a JavaScript function that can pad a string to get to a determined length?
在 javascript 中左填充字符串的最简单方法是什么?
我正在寻找等效于 mystr.lpad("0", 4)
的内联表达式:对于 mystr='45'
将返回 0045
。
标签 javascript string
<分区>
Possible Duplicate:
Is there a JavaScript function that can pad a string to get to a determined length?
在 javascript 中左填充字符串的最简单方法是什么?
我正在寻找等效于 mystr.lpad("0", 4)
的内联表达式:对于 mystr='45'
将返回 0045
。
最佳答案
找到一个简单的一行解决方案:
("0000" + n).slice(-4)
如果字符串和填充在变量中,您将:
mystr = '45'
pad = '0000'
(pad + mystr).slice(-pad.length)
已找到答案 here ,感谢@dani-p。 感谢@profitehlolz。
关于javascript - 左填充字符串的最简单内联方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13859538/