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:
Modifier | Description |
---|---|
Change case | Change the case of a string to a specified style. |
Replace | Replace 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
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:
Style | Description |
---|---|
lower | Change all characters to lowercase. For example, "Hello World" becomes "hello world". |
upper | Change all characters to uppercase. For example, "Hello World" becomes "HELLO WORLD". |
title | Change the first character of each word to uppercase, and all others to lowercase. For example, "Hello world" becomes "Hello World". |
sentence | Change 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:
- value
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
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
Now, compare it with the sentence case:
"THE QUICK BROWN FOX" in sentence case // The quick brown fox
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>*/
To replace a specific occurrence of the pattern, use the following syntax:
/*<value>*/ replacing /*<occurrence>*/ occurrence of /*<pattern>*/ with /*<replacement>*/
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>*/
To start the search at a specific offset, use this syntax:
/*<value>*/ replacing /*<pattern>*/ from /*<offset>*/ with /*<replacement>*/
Parameters
These are the supported parameters:
- value
The string in which to replace the pattern.
- pattern
A string or regular expression to match.
- replacement
A string to replace the matched pattern.
- occurrence(optional)
An ordinal literal indicating which occurrence of the matched pattern to replace.
Default:all occurrences- offset(optional)
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
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
To replace the last occurrence, use a negative ordinal:
"Hello World" replacing the last occurrence of "l" with "1" // Hello Wor1d
For more complex replacements, you can use regular expressions:
" Hello World " replacing ~/^\s+|\s+$/ with "" // Hello World
The above example removes all leading and trailing whitespace from the string by replacing the pattern with an empty string.