Use string literals in expressions
A string literal is a quote followed by text and then an ending quote. Escape sequences help the expression author resolve character conflicts within a string literal. This article, written from the developer’s perspective, explains how to use string literals in expressions.
At runtime, the following expression will evaluate to the string Hello World.
Append("Hello", " ", "World")
Each string contained in quotation marks is a string literal:
- “Hello”
- ” ”
- “World”
To complete a string, an expression author typically needs only to enter the desired text between the start quote and the end quote. For example:
"Genesys Cloud"
But what happens if the author wants to make a string literal for the value He said, “Hi”? If you take the approach described above, constructing a string literal with a start quote, text, and an end quote, your expression will be:
"He said, "Hi""
The desired literal is He said, “Hi”, but the string literal is a quote followed by text and then an ending quote. Looking at the above text, the string literal is actually “He said, “ followed by other text. However, we want the quoted text of “Hi” to also be part of the string literal. Unfortunately, the quotation mark that precedes the word Hi actually becomes the ending quote and the string literal ends there. So how can we resolve the text as part of the string literal? Use an escape sequence.
To resolve the conflict in the above example, apply an escape sequence to the quote inside the string literal. This action directs the expression parser in Architect to treat the quote as a literal quote within the string literal, not as the ending quote. An escape sequence is identified by a backslash (\). Apply it to the He said, “Hi” literal as follows::
"He said, \"Hi\""
At runtime, Architect evaluates the text to the He said, “Hi” string.
Now, consider the following example: Male\Female. In this case, the string literal, when used in an expression, looks like this:
"Male\\Female"
This table shows characters for which Architect has an escape sequence:
Character | Escape sequence |
---|---|
Quotation mark (“) | \” |
Backslash (\) | \\ |
Tab | \t |
New line | \n |
Carriage return | \r |
The following examples illustrate how to use escape sequences in string literals.
Example
- Put the following string into one string literal:
-
He said, "I like the 24 hour clock because you don't have to deal with AM \ PM settings"
Answer
-
"He said, \"I like the 24 hour clock because you don't have to deal with AM \\ PM settings\""
Example
- This example contains several characters that require an escape sequence:
-
""\\.#&."<tab character>"
- Tip: Start with a quote and go through the string one character at a time, adding the appropriate escape sequence for the characters that need it, followed by a closing quote.
- The <tab character> should be an actual tab character, not the text <tab character>.
Answer
-
"\"\"\\\\.#&.\"\t\""