Although less common in typical clinical logic, some use cases require string manipulation. As such, CQL supports a core set of string operators.
Like lists, strings are 0-based in CQL. To index into a string, use the indexer operator:
To determine the length of string, use the Length operator:
To determine the position of a given pattern within a string, use the PositionOf operator:
PositionOf('cde', 'abcdefg')
The PositionOf() operator returns the index of the starting character of the first argument in the second argument, if the first argument can be located in the second argument. Otherwise, PositionOf() returns -1 to indicate the pattern was not found in the string. To find the last appearance of a given pattern, use PositionOf(), and to find patterns at the beginning and end of a string, use StartsWith() and EndsWith(). Regular expression matching can be performed with the Matches() and ReplaceMatches() operators.
To return a substring from a given string, use the Substring operator:
Substring('abcdefg', 0, 3)
This example returns the string 'abc'. The second argument is the starting index of the substring to be returned, and the third argument is the length of the substring to be returned. If the length is greater than number of characters present in the string from the starting index on, the result includes only the remaining characters. If the starting index is less than 0, or greater than the length of the string, the result is null. The third argument is optional; if it is not provided, the substring is taken from the starting index to the end of the string.
To concatenate strings, use the + operator:
Note that when using + with string values, if either argument is null, the result will be null. To treat null as the empty string (''), use the & operator:
To combine a list of strings, use the Combine operator:
Combine({ 'ab', 'cd', 'ef' })
The result of this expression is:
To combine a list with a separator, provide the separator argument to the Combine operator:
Combine({ 'completed', 'refused', 'pending' }, ';')
The result of this expression is:
'completed;refused;pending'
To split a string into a list of strings based on a specific separator, use the Split operator:
Split('completed;refused;pending', ';')
The result of this expression is:
{ 'completed', 'refused', 'pending' }
Use the Upper and Lower operators to return strings with upper or lowercase letters for all characters in the argument.