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

padRight() : Pad String on Right


On this page

Summary

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

Signature

padRight(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

Align Left in Fixed Width

<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>

Table-Like Formatting

<pre>
  Price

  $

</pre>

Data:

doc.Params["model"] = new {
    products = new[] {
        new { name = "Product A", status = "Active", price = "99.99" },
        new { name = "Product B", status = "Sold Out", price = "149.99" }
    }
};

Output:

<pre>
Name            Status     Price
Product A       Active     $99.99
Product B       Sold Out   $149.99
</pre>

Custom Padding Character

<p></p>

Data:

doc.Params["model"] = new {
    label = "Total"
};

Output:

<p>Total...............</p>

Create Separators

<div>
  <span></span>
  <span></span>
</div>

Data:

doc.Params["model"] = new {
    title = "Section Header"
};

Output:

<div>
  <span>Section Header</span>
  <span>----------------------------------------</span>
</div>

Notes

  • Pads on the right (adds characters after 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 tables and aligning text
  • For left padding, use padLeft()

See Also