padLeft - Pad start of a string with a given character
padLeft
will add additional values to the beginning of a string up to a desired length.
NOTE: If the size of the string is greater than the desired character count, the string will be truncated from the end. For example, Chris
with charCount
of 3
becomes Chr
.
Syntax
Arguments
Name | Type | Required | Description |
---|---|---|---|
value | string | true | The string to pad |
charCount | number | true | The desired character count. If less than value, string will be truncated |
fillWith | string | true | The padding character |
NOTE: fillWith
MUST be a single character string.
Example - Ensuring all strings are the same length
Consider the following documents:
We may wish to make sure all of these strings are padded to the same length. We can do this using padLeft
:
This will result in the following document:
{
"name": "Chris",
"name_padded": "XXXXXChris"
},
{
"name": "David",
"name_padded": "XXXXXDavid"
},
{
"name": "John",
"name_padded": "XXXXXXJohn"
}
Theme
Light