Skip to content

strip_newlines

The strip_newlines filter is designed to remove newline characters (\n and \r) from a string. It is useful for cleaning up text data that may contain unwanted line breaks or formatting.

Functionality

  • Strings: Takes a string as input.
  • Removal: Eliminates all instances of newline characters (\n, which represents a line feed, and \r, which represents a carriage return).
  • Output: Returns a new string with all newline characters removed.

Syntax

    {{ input_string | strip_newlines }}
Arguments
The `strip_newlines` filter does not require any arguments.
Code Samples

Example 1: Removing Newlines from a Paragraph

    {% assign paragraph = "This is the first line.\nThis is the second line.\r\nThis is the third line." %}

    {{ paragraph | strip_newlines }}

Output:

This is the first line.This is the second line.This is the third line.
Example 2: Removing Newlines from a Code Snippet

    {% assign code = "def function():\n    print('Hello, world!')\r\n" %}

    {{ code | strip_newlines }}

Output:

def function():    print('Hello, world!')

Outliers and Special Cases

  • Empty Strings: If the input string is empty, the strip_newlines filter returns an empty string.
  • Strings Without Newlines: If the input string does not contain any newline characters, the filter returns the original string unchanged.
  • Non-String Input: If the input is not a string, the strip_newlines 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_newlines filter is helpful for normalizing text data and removing unwanted line breaks.
  • It is particularly useful when dealing with text copied from different sources or formats.
  • Combine this filter with other string manipulation filters like strip (to remove leading and trailing whitespace) or replace (to replace specific characters) for more comprehensive text transformations.