Skip to content

lstrip

The lstrip filter is designed to remove leading whitespace characters from the beginning of a string.

Functionality

  • Strings: Takes a string as input.
  • Stripping: Removes any whitespace characters (spaces, tabs, newlines) from the start of the string.
  • Output: Returns a new string with the leading whitespace removed.

Syntax

    {{ input_string | lstrip }}

Arguments

The lstrip filter does not require any arguments.

Code Samples

Example 1: Removing Leading Spaces

    {% assign message = "This string has leading spaces." %}
    {{ message | lstrip }}
Output:
This string has leading spaces.
Example 2: Removing Leading Tabs and Newlines

    {% assign code = "\t\n  def function():\n    pass" %}

    {{ code | lstrip }}

Output:

def function():
pass

Outliers and Special Cases

  • Empty Strings: If the input string is empty, the lstrip filter returns an empty string.
  • Strings Without Leading Whitespace: If the input string does not have any leading whitespace, the filter returns the original string unchanged.
  • Non-String Input: If the input is not a string, the lstrip filter might attempt to convert it to a string or return an error, depending on how Experience Builder handles type conversions.

Key Points

  • The lstrip filter is a handy tool for cleaning and normalizing string data.
  • It is particularly useful when dealing with user input or data imported from external sources where leading whitespace might be present.
  • Combine it with rstrip to remove both leading and trailing whitespace, or with strip to remove whitespace from both ends in a single operation.