8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[JavaScript][ES2017]0埋め(ゼロパディング)をするシンプルな記法(padStart, padEnd)

Last updated at Posted at 2019-12-26

概要

ググるとまずslice()を使う方法が出てくるが、ES2017が使える環境ならばpadStart()の方が良い。

Node.jsならば8.0.0から使用可能。

使い方

一つ目の引数にパディング後の桁数、二つ目にパディングに使う文字(デフォルトは半角スペース)を指定する。

> '123'.padStart(5)
'  123'

> '123'.padStart(5, '0')
'00123'

> '123'.padStart(10, '*')
'*******123'

右側を埋めるpadEnd()もある。

> '123'.padEnd(5, '0')
'12300'

ドキュメント

8
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?