View source code
Display the source code in std/range/package.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Function std.range.padLeft

Extends the length of the input range r by padding out the start of the range with the element e. The element e must be of a common type with the element type of the range r as defined by CommonType. If n is less than the length of of r, then r is returned unmodified.

auto padLeft(R, E) (
  R r,
  E e,
  size_t n
if ((isInputRange!R && hasLength!R || isForwardRange!R) && !is(CommonType!(ElementType!R, E) == void));

If r is a string with Unicode characters in it, padLeft follows D's rules about length for strings, which is not the number of characters, or graphemes, but instead the number of encoding units. If you want to treat each grapheme as only one encoding unit long, then call byGrapheme before calling this function.

If r has a length, then this is Ο(1). Otherwise, it's Ο(r.length).


r an input range with a length, or a forward range
e element to pad the range with
n the length to pad to


A range containing the elements of the original range with the extra padding

See Also: leftJustifier


import std.algorithm.comparison : equal;

assert([1, 2, 3, 4].padLeft(0, 6).equal([0, 0, 1, 2, 3, 4]));
assert([1, 2, 3, 4].padLeft(0, 3).equal([1, 2, 3, 4]));

assert("abc".padLeft('_', 6).equal("___abc"));


Andrei Alexandrescu, David Simcha, Jonathan M Davis, and Jack Stouffer. Credit for some of the ideas in building this module goes to Leonardo Maffi.


Boost License 1.0.