Skip to main content Link Search Menu Expand Document (external link)

padLeft() : Pad String on Left


On this page

Summary

Pad a string on the left side to reach a specified length.

Signature

padLeft(str, totalLength, padChar?)

Parameters

Parameter Type Required Description
str String Yes The string to pad
totalLength Number Yes The desired total length
padChar String No The padding character (default: space)

Returns

Type: String

The padded string.


Examples

Zero-Padded Numbers

<p>Order #</p>

Data:

doc.Params["model"] = new {
    orderNumber = 12345
};

Output:

<p>Order #00012345</p>

Invoice Numbers


  <div>INV-</div>

Data:

doc.Params["model"] = new {
    invoices = new[] {
        new { id = 1 },
        new { id = 42 },
        new { id = 999 }
    }
};

Output:

<div>INV-000001</div>
<div>INV-000042</div>
<div>INV-000999</div>

Align Text

<pre>

 | $

</pre>

Data:

doc.Params["model"] = new {
    items = new[] {
        new { name = "Widget", price = 19.99m },
        new { name = "Gadget", price = 29.99m },
        new { name = "Tool", price = 9.99m }
    }
};

Output:

<pre>
              Widget | $19.99
              Gadget | $29.99
                Tool | $9.99
</pre>

Custom Padding

<p></p>

Data:

doc.Params["model"] = new {
    code = "ABC"
};

Output:

<p>-------ABC</p>

Notes

  • Pads on the left (adds characters before the string)
  • If string is already longer than totalLength, returns unchanged
  • Default padding character is space
  • Pad character must be a single character
  • Useful for formatting numbers and aligning text
  • For right padding, use padRight()

See Also