ModelWorks logoModelWorks
JSON TransformString

padStart

Pad the start of a string to a target length

padStart

Pads the beginning of a string with pad_char until its length reaches length bytes. If the string is already long enough, it is returned unchanged.

Options

OptionTypeRequiredDefaultDescription
lengthintegerYesTarget length in bytes.
pad_charstringNo" "Padding string. Empty values fall back to a space.

Example

Input

JSON
"42"

Operation

JSON
{ "type": "padStart", "options": { "length": 5, "pad_char": "0" } }

Output

JSON
"00042"

In a Circuit Step

JSON
{
  "name": "zero_pad_id",
  "description": "Left-pad a numeric id with zeros to a fixed width",
  "function": "circuit.core.transform.json",
  "input": {
    "source": "${{ input.id_string }}",
    "operations": [
      { "type": "padStart", "options": { "length": 8, "pad_char": "0" } }
    ]
  },
  "outputs": [
    {
      "name": "padded_id",
      "description": "Id zero-padded to 8 bytes",
      "value": "${{ zero_pad_id.output.transformed_result }}"
    }
  ]
}

Notes & gotchas

  • Errors with padStart: expected string if the input is not a string.
  • length is measured in bytes, not characters. Multi-byte pad_char values can produce unexpectedly long output.
  • pad_char may be longer than one character; it's repeated whole-string until the target length is reached. Each iteration appends the full pad_char, so the output may exceed length if pad_char doesn't divide evenly.
  • An empty pad_char falls back to a single space.
On this page

On this page