String functions

String functions are used to evaluate, manipulate, and extract information from text.

Basic string functions

Table 1. Basic string functions
Syntax Description Example
<text> contains <text> Tests that a string contains another string. if the name of customer contains "Smith"
<text> does not contain <text> Tests that a string does not contain another string. if the name of customer does not contain "Smith"
<text> does not end with <text> Tests that a string does not end with another string. if the rental agreement code of customer does not end with "XG5"
<text> does not start with <text> Tests that a string does not start with another string. if the rental agreement code of customer does not start with "XG5"
<text> ends with <text> Tests that a string ends with another string. if the rental agreement code of customer ends with "XG5"
<text> is empty Tests that a string is empty. An empty string contains no characters and is equivalent to "". A string is not empty if it contains a space (" "). if the rental agreement code of customer is empty
<text> is not empty Tests that a string is not empty. A string is not empty if it contains a space (" "). if the rental agreement code of the customer is not empty
print <text> Prints a string to standard output. print the message "Hello World"
<text> starts with <text> Tests that a string starts with another string. if the rental agreement code of the customer starts with "XG5"
the length of <text> Returns the number of characters in a string. if the length of 'customer name' is more than 3
<text> + <text> Concatenates two strings. set decision to 'first name' + ' ' + 'last name'
<number> + <text> Concatenates a number and a string. The result is treated as a string. print "Spend just $" + ('discount threshold' - order total) + " more to receive a 10% discount!"
<text> + <number> Concatenates a string and a number. The result is treated as a string. print "You received a 10% discount because your order was over $" + 'discount threshold'
<text> to uppercase [ using the casing rules of <locale> ] Converts all characters of the text to uppercase. The default locale is en-US. set decision to "hello" to uppercase

Expected output: "HELLO"

<text> to lowercase [ using the casing rules of <locale> ] Converts all characters of the text to lowercase. The default locale is en-US. set decision to "HELLO" to lowercase

Expected output: "hello"

<text> removing leading and trailing spaces Removes leading and trailing whitespace from the string. set decision to " Hello " removing leading and trailing spaces

Expected output: "Hello"

<text> divided into parts [ by <string> ] Splits a string into an ordered list of substrings based on Unicode whitespace and line breaks, or by using a specified regular expression. set decision to "Name: John, Surname: Smith" divided into parts by ",\\s?"

Expected output: { "Name: John", "Surname: Smith" }

<strings> joined together with <string> Joins elements of an array into a single string with the specified separator. set decision to { "An", "example" } joined together with " "

Expected output: "An example"

Structured text functions

The following table outlines functions for extracting and converting data in structured formats such as JSON, YAML, and XML.

Table 2. Structured text functions
Syntax Description Example
<text> extracting { the JSON fragment | all JSON fragments } at <string> Extracts one or more values from a JSON string by using a JSON Pointer path. set decision to "{ \"a\": { \"b\": 123 } }" extracting the JSON fragment at "/a/b"

Expected output: "123"

<text> extracting { the YAML fragment | all YAML fragments } at <string> Extracts one or more values from a YAML string by using a JSON Pointer path. set decision to "a:\n b: 123" extracting the YAML fragment at "/a/b"

Expected output: "123"

<text> extracting { the XML fragment | all XML fragments } at <string> Extracts one or more fragments from an XML string using an XPath expression. set decision to "<root><elt1/><elt2/></root>" extracting all XML fragments at "/root/*"

Expected output: { "<elt1/>\n", "<elt2/>\n" }

<text> as YAML from JSON Converts a JSON string into a YAML formatted string. set decision to "{ \"a\": 123 }" as YAML from JSON

Expected output: "---\na: 123\n"

<text> as JSON from YAML Converts a YAML string into a JSON-formatted string. set decision to "---\na: 123" as JSON from YAML

Expected output: "{ \"a\": 123 }"

Pattern matching functions

The following table outlines functions for pattern matching and text transformation by using regular expressions.

Table 3. Pattern matching functions
Syntax Description Example
<text> matches <regex> Checks if the entire string matches a regular expression. if "Name: John, Surname: Smith" matches ".*Surname: ([A-Z][a-z]*).*" then...

Expected output: true

<text> extracting the first string matching <regex> Finds the first substring matching a regular expression. set decision to "1.2, 3, 5.7" extracting the first string matching the pattern for a number

Expected output: "1.2"

<text> extracting all strings matching <regex> Finds all substrings matching a regular expression. set l to "1.2, 3, 5.7" extracting all strings matching the pattern for a number, mapping elements to each string as number

Expected output: { 1.2, 3, 5.7 }

<text> replacing { the first string | all strings } matching <regex> by <replacement>

Returns a string whose value is the given text, with each substring that matches the given regular expression replaced by the specified replacement.

The second string (<regex>) is a regular expression.

The third string (<replacement>) is the replacement value, where:

  • `&` refers to the entire match of the regular expression
  • `\1` refers to the first captured group in the match, `\2` refers to the second, and so on up to `\9`.
  • `\\` inserts a literal backslash
  • `\&` inserts a literal ampersand

Search and replace:

set decision to "https://www.ibm.fr" replacing the first string matching "ibm\\.fr" by "ibm.com"

Expected output: "https://www.ibm.com"

Find:

set decision to "Name: John, Surname: Smith" replacing the first string matching ".*Surname: ([A-Z][a-z]*).*" by "\\1"

Expected output: "Smith"

Find and transform:

set decision to "Name: John, Surname: Smith" replacing the first string matching "Name: ([A-Z][a-z]*), Surname: ([A-Z][a-z]*)" by "\\2 \\1"

Expected output: "Smith John"

Delete:

set decision to "Name: John, Surname: Smith" replacing the first string matching "Name:[^,]+, " by ""

Expected output: "Surname: Smith"

Transform and decorate:

set decision to "$10, $100" replacing all strings matching "\\$([0-9]+)" by "\\1 dollars"

Expected output: "10 dollars, 100 dollars"

Prefix/Suffix logic:

set decision to "12,John,Doe,,23," replacing the first string matching "(.*)(?=Doe).*" by "\\1"

Expected output: "12,John,"

set decision to "12,John,Doe,,23," replacing the first string matching ".*Doe(.*)" by "\\1"

Expected output: ",,23,"