String tests

Discover the different string tests available in CQL.

String tests are natural language conditions that you can use to check whether a string meets certain criteria. These tests include, for example, whether a string has a start, an end, or contains another string.

The following table summarizes the available string tests for quick reference:

TestDescription
Starts withChecks whether a string starts with another string.
Ends withChecks whether a string ends with another string.
MatchesChecks whether a string matches a regular expression.

Starts with

This test checks if a string begins with another string.

A string starts with another string if the latter is a prefix of the former. For example, "Hello world" starts with "Hello".

The test is case-sensitive, meaning that the strings must match exactly, including the case of the letters.

Croct's mascot neutral
Empty strings are everywhere!

An empty string is a prefix of every string. It follows a math concept called vacuous truth, which says that an empty set is a part of every set.

Syntax

This test has the following syntax:

/*<value>*/ starts with /*<prefix>*/
Try in Playground

To negate the test, you can write:

/*<value>*/ does not start with /*<prefix>*/
Try in Playground

Parameters

These are the supported parameters:

value

The string to validate.

prefix

The prefix that the string may start with.

Examples

Here is a basic example that checks if a string starts with another string:

"Hello world" starts with "Hello" // true
Try in Playground

You can negate the test to verify the contrary:

"Hello world" does not start with "Hello" // false
Try in Playground

Ends with

This test checks if a string ends with another string.

A string ends with another string if the latter is a suffix of the former. For example, "Hello world" ends with "world".

The test is case-sensitive, meaning that the strings must match exactly, including the case of the letters.

Croct's mascot neutral
Empty strings are everywhere!

An empty string is a suffix of every string. It follows a math concept called vacuous truth, which says that an empty set is a part of every set.

Syntax

This test has the following syntax:

/*<value>*/ ends with /*<suffix>*/
Try in Playground

To negate the test, you can write:

/*<value>*/ does not end with /*<suffix>*/
Try in Playground

Parameters

These are the supported parameters:

value

The string to validate.

suffix

The suffix that the string may end with.

Examples

Here is a basic example that checks if a string ends with another string:

"Hello world" ends with "world" // true
Try in Playground

You can negate the test to verify the contrary:

"Hello world" does not end with "world" // false
Try in Playground

Matches

This test checks if a string includes another string or matches a regular expression.

When matching a string, the test checks if the string includes the other string. For example, "Hello world" matches "lo wo". The test is case-sensitive, meaning that the strings must match exactly, including the case of the letters.

Syntax

This test has the following syntax:

/*<value>*/ matches /*<pattern>*/
Try in Playground

To negate the test, you can write:

/*<value>*/ does not match /*<pattern>*/
Try in Playground

Parameters

These are the supported parameters:

value

The string to validate.

pattern

A string or regular expression to check if the string matches.

Examples

Here is a basic example that checks if a string matches another string:

"Hello world" matches "lo wo" // true
Try in Playground

You can negate the test to verify the contrary:

"Hello world" does not match "lo wo" // false
Try in Playground

You can also use a regular expression to match a string:

"CQL is powerful" matches ~/^[a-z]{3}/i // true
Try in Playground

In this example, the test checks whether the string starts with three letters. The i flag makes the test case-insensitive.