Skip to content

split

The split filter is designed to break down a string into an array of substrings based on a specified separator. This is a fundamental operation for working with delimited text, such as comma-separated values (CSV) or sentences separated by spaces.

Functionality

  • Strings: Takes a string as input.
  • Separator: Requires a string argument specifying the character or sequence of characters to use as the delimiter.
  • Splitting: Divides the input string into substrings wherever the separator is found.
  • Output: Returns an array containing the resulting substrings.

Syntax

    {{ input_string | split: separator }}
Arguments

  • separator: A string representing the character or characters to use as the delimiter for splitting the string. If not provided, defaults to an empty string which will split the string by each character

Code Samples

Example 1: Splitting a Comma-Separated List

    {% assign items = "apple,banana,orange" %}
    {{ items | split: "," }}

Output:

["apple", "banana", "orange"]
Example 2: Splitting a Sentence into Words

    {% assign sentence = "This is a sample sentence." %}

    {{ sentence | split: " " }}

Output:

["This", "is", "a", "sample", "sentence."]
Example 3: Splitting by Character

    {% assign string = "hello" %}
    {{ string | split: "" }}

Output:

["h", "e", "l", "l", "o"]

Outliers and Special Cases

  • Empty Separator: If you provide an empty string as the separator (""), the string will be split into an array of individual characters.
  • No Separator Found: If the separator does not occur within the input string, the output will be an array containing a single element (the original input string).
  • Empty Input String: If the input string is empty, the filter returns an empty array.
  • Non-String Input: If the input is not a string, the split filter might attempt to convert it to a string or return an error, depending on how Experience Builder handles type conversions.

Key Points

  • The split filter is a fundamental tool for parsing and processing structured text data.
  • It is particularly useful when working with CSV files, log data, or other formats where data is separated by specific delimiters.
  • Be aware that the behavior of the split filter can be adjusted by providing different separator values.