Skip to content

rstrip

The rstrip filter is designed to remove trailing whitespace characters from the end of a string.

Functionality

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

Syntax

    {{ input_string | rstrip }}
Arguments

The rstrip filter does not require any arguments.

Code Samples

Example 1: Removing Trailing Spaces

    {% assign message = "This string has trailing spaces.   " %}

    {{ message | rstrip }}

Output:

This string has trailing spaces.
Example 2: Removing Trailing Tabs and Newlines

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

    {{ code | rstrip }}

Output:

def function():
pass

Outliers and Special Cases

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

Key Points

  • The rstrip 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 trailing whitespace might be present.
  • Combine it with lstrip to remove both leading and trailing whitespace, or with strip to remove whitespace from both ends in a single operation.