Skip to content

pad

Description

Alias for padLeft.

Adds characters to the beginning of a string until it reaches the desired length. If the string is longer than the target length, it is truncated from the end.

Note

The fillWith argument must be a single-character string.

Syntax

Like many functions in DataPrime, pad supports two notations, function and method notation. These interchangeable forms allow flexibility in how you structure expressions.

pad(value: string, charCount: number, fillWith: string): string
(value: string).pad(charCount: number, fillWith: string): string

Arguments

NameTypeRequiredDescription
valuestringtrueThe string to pad
charCountnumbertrueDesired total length of the string; if smaller than current length, the string is truncated
fillWithstringtrueThe single character used for padding

Example

Ensure all strings are the same length

Consider the following documents:

{ "name": "Chris" }
{ "name": "David" }
{ "name": "John" }

Use pad to make all names 10 characters long:

create name_padded from pad(name, 10, 'X')
create name_padded from name.pad(10, 'X')

Output

{
    "name": "Chris",
    "name_padded": "XXXXXChris"
}
{
    "name": "David",
    "name_padded": "XXXXXDavid"
}
{
    "name": "John",
    "name_padded": "XXXXXXJohn"
}