Skip to content

remove_last

Documentation for the 'remove_last' Filter in StringFilters

The remove_last filter is designed to remove the last occurrence of a specified substring from an input string.

Functionality

  • Strings: Takes a string as input.
  • Removal Target: Requires a string argument specifying the substring to remove.
  • Last Occurrence: Only the final instance of the substring is removed from the input string.
  • Output: Returns a new string with the last occurrence of the substring removed.

Syntax

    {{ input_string | remove_last: string_to_remove }}

Arguments

  • string_to_remove: The substring that you want to remove from the end of the input_string.

Code Samples

Example 1: Removing the Last Word

    {% assign sentence = "This is a repeated word word" %}
    {{ sentence | remove_last: " word" }}

Output:

This is a repeated word
Example 2: Removing the Last Character

    {% assign message = "Hello!!!" %}
    {{ message | remove_last: "!" }}
Output:
Hello!!
Example 3: Substring Not Found
    {% assign text = "This string does not contain the word 'xyz'" %}
    {{ text | remove_last: "xyz" }}
Output:
This string does not contain the word 'xyz' (No change)

Outliers and Special Cases

  • Empty Strings: If the input string is empty, the remove_last filter returns an empty string.
  • Substring Not Found: If the specified substring is not found in the input string, the original input string is returned unchanged.
  • Empty Substring: If you try to remove an empty string, no change occurs.
  • Non-String Input: If the input is not a string, the remove_last filter might attempt to convert it to a string or return an error, depending on how Experience Builder handles type conversions.

Key Points

  • The remove_last filter is a precise tool for removing the final instance of a substring from a string.
  • It is case-sensitive, meaning "Word" and "word" are treated as different substrings.
  • Consider using the remove filter if you need to remove all occurrences of a substring.
  • You can combine this filter with other string manipulation filters to achieve more complex transformations.