String functions
String functions create, combine, search, and transform text values. Use them to clean up text properties, such as page names, or to match text patterns. Before you start, review the NetScript language reference for formula syntax.
'STRING'
Use a string literal to enter a fixed text value, such as a ship mode to compare against. Enclose the text in single quotes.
Note
To include a single quote in a string, add a backslash before it, such as 'Bob\'s order'.
Function
'STRING'Data type – string
Example
'hello world'Result
hello world'STRING_1' + 'STRING_2'
Use the + operator to join two strings, such as a first name and a last name. If either string is null, Analytics treats it as an empty string.
Function
'STRING_1' + 'STRING_2'Data type – string
Example
'hello' + ' world'Result
hello world'STRING_1' in 'STRING_2'
Use the in operator to check whether the first string appears in the second string. For example, find page titles that contain a keyword.
Function
'STRING_1' in 'STRING_2'Data type – boolean
Example
'fruit' in 'Bowl of fruit'
'fruit' in 'Bowl of vegetables'Result
true
false'STRING_1' contains 'STRING_2'
Use the contains operator to check whether the first string includes the second string. For example, find URLs that contain a campaign code.
Function
'STRING_1' contains 'STRING_2'Data type – boolean
Example
'fruit' contains 'it'
'fruit' contains 'Bowl of fruit'Result
true
falsesubstring(INPUT, POSITION, LENGTH)
Use the substring function to extract part of a string, such as a product code from a longer identifier. The function takes the following arguments:
- INPUT – The string to extract characters from.
- POSITION – The position of the first character to return. The first character of the string is at position 1.
- LENGTH – (Optional) The number of characters to return. If you omit it, the function returns the rest of the string.
Function
substring(INPUT, POSITION)
substring(INPUT, POSITION, LENGTH)Data type – string
Example
substring('abcdef', 2, 3)Result
bcdto_string(INPUT)
Use the to_string function to convert a value to a string, such as a number to join with text. The input cannot be a time range.
Function
to_string(INPUT)Data type – string
Example
to_string(5)Result
5lowercase(INPUT)
Use the lowercase function to convert a string to lowercase, such as to compare text without case differences. The lower_case alias works the same way.
Function
lowercase(INPUT)Data type – string
Example
lowercase('HELLO')Result
helloregexp_instr(INPUT, PATTERN)
Use the regexp_instr function to find where a regular expression (regex) first matches in a string. For example, find the position of the first digit in an order code. The function takes the following arguments:
- INPUT – The string to search.
- PATTERN – The regex pattern to match.
The position of the first character is 1. When nothing matches, the function returns 0. If either argument is null, the function returns null. If the regex pattern is invalid, the function returns an error.
Function
regexp_instr(INPUT, PATTERN)Data type – number
Example
regexp_instr('abc1', '[0-9]')Result
4regexp_substr(INPUT, PATTERN)
Use the regexp_substr function to return the first part of a string that matches a regex pattern. For example, extract the first number from an order code. The function takes the following arguments:
- INPUT – The string to search.
- PATTERN – The regex pattern to match.
The function returns null when either argument is null or nothing matches. If the regex pattern is invalid, the function returns an error.
Function
regexp_substr(INPUT, PATTERN)Data type – string
Example
regexp_substr('abc1', '[0-9]')Result
1regexp_count(INPUT, PATTERN)
Use the regexp_count function to count how many times a regex pattern matches in a string. For example, count the repeated segments in a product code. The function takes the following arguments:
- INPUT – The string to search.
- PATTERN – The regex pattern to count.
Function
regexp_count(INPUT, PATTERN)Data type – number
Example
regexp_count('abracadabra', 'abra')Result
2regexp_replace(INPUT, PATTERN, REPLACEMENT)
Use the regexp_replace function to replace every match of a regex pattern in a string. For example, replace the digits in a product code with a label. The function takes the following arguments:
- INPUT – The original string.
- PATTERN – The regex pattern to replace.
- REPLACEMENT – The string to insert for each match.
When nothing matches, the function returns the original string.
Function
regexp_replace(INPUT, PATTERN, REPLACEMENT)Data type – string
Example
regexp_replace('abc123', '[0-9]+', '_number')Result
abc_numberstrlen(INPUT)
Use the strlen function to count the characters in a string, such as to check the length of an ID.
Function
strlen(INPUT)Data type – number
Example
strlen('hello')Result
5trim(INPUT)
Use the trim function to remove leading and trailing spaces from a string, such as values that users type.
Function
trim(INPUT)Data type – string
Example
trim(' a ')Result
a