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.
You can alternate string delimiters to avoid having to escape quotes. For example, you can use single quotes to create a string that contains double quotes, or vice versa.
Here are some common escape sequences and their meanings:
- \\ represents a single backslash (\)
- \" represents a double quote (")
- \' represents a single quote (')
- \n represents a newline character (a line break)
- \t represents a tab character
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 the character's Unicode code point to represent it in your string.
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: ∑
For a complete list of Unicode characters and their code points, check out this page.