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
Arguments Code SamplesExample 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:
Example 2: Removing Newlines from a Code SnippetOutput:
Outliers and Special Cases¶
- Empty Strings: If the input string is empty, the
strip_newlinesfilter 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_newlinesfilter might attempt to convert it to a string or return an error, depending on how Experience Builder handles type conversions.
Key Points¶
- The
strip_newlinesfilter 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) orreplace(to replace specific characters) for more comprehensive text transformations.