How to split strings in AmpScript
AMPscript provides the ability to split strings into arrays (or lists), making it easier to process individual components of a string. The primary function used to split strings in AMPscript is BuildRowsetFromString(). This function splits a string based on a specified delimiter and stores the resulting substrings in a rowset (which is essentially a collection of values).
- Using BuildRowsetFromString()
The BuildRowsetFromString() function is used to split a string into individual components based on a delimiter and returns a rowset. You can then loop through this rowset to extract each individual value.
Syntax:
BuildRowsetFromString(string, delimiter)
- string: The string to be split.
- delimiter: The character or characters used to split the string.
Example: Split a Comma-Separated List
%%[
SET @string = “apple,banana,orange,grape”
SET @rowset = BuildRowsetFromString(@string, “,”)
SET @rowCount = RowCount(@rowset)
FOR @i = 1 TO @rowCount DO
SET @item = Field(Row(@rowset, @i), 1)
OUTPUTLINE(@item)
NEXT @i
]%%
Explanation:
- The string “apple,banana,orange,grape” is split into individual items using a comma , as the delimiter.
- The Rowset contains each item, and the FOR loop iterates through the rowset to output the individual items (apple, banana, orange, grape).
- Using Field() to Access Elements
Once you have split the string into a rowset, you can access individual elements using the Field() function by specifying the row and column. Since the rowset is 1-based, the first element is accessed using Row(@rowset, 1).
Example: Access a Specific Element from the Split String
%%[
SET @string = “apple,banana,orange,grape”
SET @rowset = BuildRowsetFromString(@string, “,”)
SET @secondItem = Field(Row(@rowset, 2), 1)
OUTPUTLINE(“The second item is: ” & @secondItem)
]%%
Explanation:
- The string is split, and the second item (banana) is extracted using Row(@rowset, 2) and Field(Row(), 1).
- Handling Edge Cases
If the delimiter is not found or the string is empty, you may need to account for those cases to avoid errors.
Example: Handling Empty or Missing Delimiters
%%[
SET @string = “”
SET @rowset = BuildRowsetFromString(@string, “,”)
SET @rowCount = RowCount(@rowset)
IF @rowCount > 0 THEN
SET @firstItem = Field(Row(@rowset, 1), 1)
OUTPUTLINE(“The first item is: ” & @firstItem)
ELSE
OUTPUTLINE(“No items found.”)
ENDIF
]%%
Explanation:
- The code checks if the rowset contains any items before trying to access the first element.
Use Case Example: Email Personalization with Split Strings
Let’s say you have a Data Extension field containing a list of product interests (e.g., “Shoes,Shirts,Accessories”) and you want to personalize an email with the first product.
Example: Split Product Interests and Personalize Email
%%[
SET @productInterests = AttributeValue(“ProductInterests”)
SET @rowset = BuildRowsetFromString(@productInterests, “,”)
SET @firstProduct = Field(Row(@rowset, 1), 1)
OUTPUTLINE(“Your first product interest is: ” & @firstProduct)
]%%
Explanation:
- The field ProductInterests is split using a comma as the delimiter, and the first product (Shoes) is extracted and displayed in the email.
- Important Considerations
- Rowset Indexing: AMPscript rowsets are 1-based (i.e., the first item is at index 1, not 0).
- Delimiter: The delimiter must be a single character, such as a comma, space, or semicolon. You can use multiple characters if necessary (e.g., “; “).
- Null or Empty Values: Ensure that the string is not null or empty before splitting to avoid runtime errors.
Best Practices:
- Use the RowCount() function to check how many items were created after splitting the string to avoid accessing non-existing elements.
- Optimize string splitting for performance by keeping the string size reasonable and limiting unnecessary splits in emails with large subscriber bases.