String macros

Learn how to use macros to work with string in CQL.

String macros are specialized functions written in natural language that allow you to perform operations on strings. They handle all the logic involved in the process, from argument conversion to the final result and everything in between.

This is the summary of the available string macros for quick reference:

MacroDescription
LengthCounts the number of characters in a string.

Length

This macro counts the number of characters in a given string, typically used for validation and manipulation purposes.

To find out the length of a string, this macro counts the number of Unicode characters, like we normally do when counting characters in a string. For example, the emoji "🙂" is represented by two bytes but is counted as a single character.

Croct's mascot neutral
Is there another way to count characters?

In the wild world of programming, the length of a string can mean different things, like the number of bytes. As a result, the length of characters with accents or emojis may be greater than one.

Syntax

This macro has the following syntax:

length of /*<value>*/
Try in Playground

Parameters

These are the supported parameters:

value

The string for which you want to determine the length.

Examples

Here is a practical example of how to get the length of a string:

length of "Hello, world! 👋" // 15
Try in Playground

Note that if a value is not a string, CQL converts it to one whenever possible before determining its length:

length of 123 // 3
Try in Playground