Pad a string to a certain length with .pad()

.pad()


Description: add a pad of characters to a string up to a certain length .


Example:

<!doctype html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>pad demo</title>
      <script src="http://code.bitsycode.com/bitsycode-latest.min.js"></script>
    </head>
    <body>
     
    <p class="padded"></p>
     
    <script>
      bitsyCode(".padded").html(bitsyCode.pad("1", { direction:"left", padWith:"0", size:6 }));
    </script>
     
    </body>
    </html>

Preview:

000001
  • Usage

    • How to use:

      Specify a value with the needed options.

    • Specify a value you want to add a pad to.

      bitsyCode.pad("string", { direction:"left", padWith:"0", size:6 })
    • Example Usage:
      bitsyCode.pad("2"); // Result: 02
      bitsyCode.pad("2", { direction:"left", padWith:"0", size:6 }); // Result: 000002
  • Options

    • What options are there:
      direction: 'left' (default value) // Specify if the string pad should occur on the left or right of string.
      padWith: '0' (default value) // Specify what character should be used as a pad.
      size: 2 (default value) // Specify the size of the pad, this size will exclude the string length, so when specifying "size" add the pad length plus the string length.
  • Returns

    • Returns:

      Padded string.

Share