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 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.
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.