String modifiers

Learn how to manipulate string values in CQL.

String modifiers are natural language functions for manipulating string values. These functions are useful for tasks that involve transforming text, such as changing the case, replacing parts, and other text manipulations.

This is the summary of the available number modifiers for quick reference:

ModifierDescription
Change caseChange the case of a string to a specified style.
ReplaceReplace a target substring or pattern with a specified replacement.

Change case

This modifier changes the case of a string to a specified style.

It can be useful for formatting strings consistently, especially when you don't have control over the input. For example, if you want to display a user's name in a welcome message, you can use the title case to ensure proper capitalization.

Syntax

This modifier has the following syntax:

/*<value>*/ in /*<style>*/ case
Try in Playground

The /*<style>*/ is a literal word that specifies the case style and cannot be specified dynamically using expressions.

Here is a list of the supported styles:

StyleDescription
lowerChange all characters to lowercase. For example, "Hello World" becomes "hello world".
upperChange all characters to uppercase. For example, "Hello World" becomes "HELLO WORLD".
titleChange the first character of each word to uppercase, and all others to lowercase. For example, "Hello world" becomes "Hello World".
sentenceChange the first character of each sentence to uppercase, and all others to lowercase. For example, "Hello World" becomes "Hello world".

Parameters

These are the supported parameters:

valuestring

The string for which to change the case.

Examples

Here is a basic example of how to ensure that a user's name is properly capitalized:

user's name in title case // John Doe
Try in Playground

To understand the difference between the title and sentence case, consider the following example:

"THE QUICK BROWN FOX" in title case // The Quick Brown Fox
Try in Playground

Now, compare it with the sentence case:

"THE QUICK BROWN FOX" in sentence case // The quick brown fox
Try in Playground

Note that the entire sentence is affected, not just the first word.

Replace

This modifier replaces a target substring or pattern with a specified replacement.

Optionally, you can specify the occurrence of the pattern to replace and the offset to start the search for more control over the replacement. And for more advanced replacements, you can even use regular expressions.

Syntax

The basic syntax for this modifier is:

/*<value>*/ replacing /*<pattern>*/ with /*<replacement>*/
Try in Playground

To replace a specific occurrence of the pattern, use the following syntax:

/*<value>*/ replacing /*<occurrence>*/ occurrence of /*<pattern>*/ with /*<replacement>*/
Try in Playground

You can use any ordinal to specify the occurrence, including the words first, second, third, and so on.

You can also prefix the ordinal with a definite article to make the expression more readable:

/*<value>*/ replacing the /*<occurrence>*/ occurrence of /*<pattern>*/ with /*<replacement>*/
Try in Playground

To start the search at a specific offset, use this syntax:

/*<value>*/ replacing /*<pattern>*/ from /*<offset>*/ with /*<replacement>*/
Try in Playground

Parameters

These are the supported parameters:

valuestring

The string in which to replace the pattern.

patternstring|regex

A string or regular expression to match.

replacementstring

A string to replace the matched pattern.

occurrence(optional)ordinal

An ordinal literal indicating which occurrence of the matched pattern to replace.

Default:all occurrences
offset(optional)integer

A zero-based index indicating where to start the search for the pattern.

Default:starts at the beginning of the string

Examples

Here is a basic example of how to replace a substring:

"Hello World" replacing "World" with "CQL" // Hello CQL
Try in Playground

You can replace a specific occurrence of the pattern by specifying an ordinal:

"Hello World" replacing the first occurrence of "l" with "1" // He1lo World
Try in Playground

To replace the last occurrence, use a negative ordinal:

"Hello World" replacing the last occurrence of "l" with "1" // Hello Wor1d
Try in Playground

For more complex replacements, you can use regular expressions:

" Hello World " replacing ~/^\s+|\s+$/ with "" // Hello World
Try in Playground

The above example removes all leading and trailing whitespace from the string by replacing the pattern with an empty string.