Skip to content

strip

The strip filter is designed to remove leading and trailing whitespace characters from a string. It combines the functionality of both lstrip (removing leading whitespace) and rstrip (removing trailing whitespace).

Functionality

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

Syntax

    {{ input_string | strip }}
Arguments

The strip filter does not require any arguments.

Code Samples

Example 1: Removing Leading and Trailing Spaces

    {% assign message = "   This string has spaces on both ends.   " %}

    {{ message | strip }}

Output:

This string has spaces on both ends.
Example 2: Removing Whitespace Around a Code Block
    {% assign code = "\t\n  def function():\n    pass  \t\n" %}

    {{ code | strip }}
Output:
def function():
pass

Outliers and Special Cases

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

Key Points

  • The strip filter is a convenient shortcut for removing whitespace from both ends of a string in a single operation.
  • It is particularly useful when normalizing text data, such as user input or data imported from external sources.
  • Consider using lstrip or rstrip if you need to remove whitespace from only one end of the string.