String literal

Learn how to represent text values.

Strings are sequences of characters that represent text. They can include letters, numbers, punctuation marks, and even special symbols, such as emoji or Unicode characters.

In CQL, you express strings by enclosing the text in either single or double quotes.

Single-quoted strings

Single-quoted strings are a simpler form of strings where all characters are interpreted as they are written, without any processing or interpretation.

You can create a single-quoted string by enclosing the text in single quotes:

'This is a single-quoted string'

If you need to include a single quote character in your string, you can escape it with a backslash:

'It\'s a beautiful day πŸ–οΈ' // It's a beautiful day πŸ–οΈ

Double-quoted strings

Double-quoted strings are more powerful and versatile than single-quoted strings, as they allow the use of escape sequences.

You can create a double-quoted string by enclosing the text in double quotes:

"This is a double-quoted string"

In some cases, you may need to include a double quote in your string. This is where escape sequences come in handy.

Escape sequences

Escape sequences are special character combinations that begin with a backslash (\) and are followed by one or more characters. They are used to represent characters that cannot be typed directly, control the formatting of the string, or include special symbols.

Here are the supported escape sequences and their meanings:

  • \\ represents a single backslash (\)
  • \" represents a double quote (")
  • \/ represents a forward slash (/)
  • \n represents a newline character (a line break)
  • \r represents a carriage return character
  • \t represents a tab character
  • \b represents a backspace character
  • \f represents a form feed character

Single-quoted strings do not interpret escape sequences, with one exception: a backslash before a single quote (\') lets you include a single quote inside the string, and a backslash before another backslash (\\) lets you include a literal backslash. Every other character, including the backslash itself, is taken verbatim.

For example, to represent a string with a quote and a newline, you could write:

"Say \"Hello, CQL!\"\nWelcome to the world of strings."

This string would be rendered as:

Say "Hello, CQL!"Welcome to the world of strings.

Unicode characters

Strings in CQL are encoded in UTF-8, you can use any Unicode character, including emoji.

For example, to include a big smiley face in a string, you could write:

"Who doesn't like emoji? πŸ˜ƒ"

However, some characters are difficult to type or are not displayed correctly in text editors. In such situations, you can use the \u escape sequence followed by exactly four hexadecimal digits to represent the character’s Unicode code point.

Croct's mascot neutral
What is a code point?

A code point is a number that uniquely identifies a character in the Unicode standard. For example, the code point for the letter A is U+0041.

For example, you can represent the summation symbol βˆ‘ (U+2211) as follows:

"Summation: \u2211" // Summation: βˆ‘

Because the \u form only accepts four hex digits, it can only represent code points up to U+FFFF. To represent characters above this range, such as most emoji, use the UTF-16 surrogate pair that encodes the character.

For example, the grinning face emoji πŸ˜€ (U+1F600) is encoded as the surrogate pair \uD83D\uDE00:

"Grinning face: \uD83D\uDE00" // Grinning face: πŸ˜€

For a complete list of Unicode characters and their code points, check out this page.