A string literal is zero or more characters enclosed between a pair of double quotation marks (
"...") or single quotation marks (
'...'). In CSPro, string literals will appear in the text editor colored in
magenta. Any spaces enclosed within the quotation marks are considered part of the string literal. Uppercase and lowercase letters may be used, and the string literal
"a" is different than
"A".
String literals are compiled differently based on an application's
logic version.
The
original version of string literals does not allow for escape sequences. If you wish to have single quotation marks embedded within your string, you must use double quotation marks to enclose it, and vice versa. For example, this results in a compiler error:
myString = 'That's great!';
This would set myString to 'that' and the trailing 's great!' would be considered outside the string, and would therefore result in a compiler error. Thus, if you wanted to accomplish the above, you must write:
myString = "That's great!";
Similarly, if you wanted to embed double quotation marks within your string, you must write the string as follows:
myString = 'The chair is 23" high';
With
logic version CSPro 8.0+, escape sequences are processed. An escape sequence begins with a backslash character and is followed by a valid character. For example, using this logic version, the string above could be written as:
myString = 'That\'s great!';
The backspace before the single quotation mark indicates that the next character is a special character. In this case, the special character does not end the string literal, but instead places a single quotation mark into the string without terminating the string literal. Note that the text editor colors escape sequences slightly differently than the other characters in a string literal.
The following escape sequences are recognized by CSPro.
Escape Sequence | Description |
\' | single quotation mark |
\" | double quotation mark |
\\ | backslash |
\a | audible bell |
\b | backspace |
\f | form feed |
\n | line feed |
\r | carriage return |
\t | horizontal tab |
\v | vertical tab |
The escape sequences used most frequently are for quotation marks (\' and \"), backslashes (\\), and
newline characters (\n). When using double quotation marks to surround a string literal, it is not required to escape single quotation marks, and vice versa.
Here are some examples of string literals with escape sequences that are valid with logic version CSPro 8.0+:
// all display: "abc"
// 'xyz'
errmsg("\"abc\"\n\'xyz\'");
errmsg("\"abc\"\n'xyz'");
errmsg('\"abc\"\n\'xyz\'');
errmsg('"abc"\n\'xyz\'');
When using string literals that contain many backslash characters, such as a Windows file path, it may be convenient to use a verbatim string literal (when using logic version CSPro 8.0+). To create a verbatim string literal, use an @ character, immediately followed by text surrounded by double quotation marks. For example, these two strings are identical:
"C:\\Program Files (x86)\\CSPro 8.0\\html\\images\\cspro-logo-medium.png" // string literal
@"C:\Program Files (x86)\CSPro 8.0\html\images\cspro-logo-medium.png" // verbatim string literal
Within a verbatim string literal, the only character that must be escaped is a double quotation mark, which is escaped as two subsequent quotation marks: "". For example:
// displays: The woman asked, "What is your favorite letter: a \ b \ c \ x \ y \ z?"
errmsg(@"The woman asked, ""What is your favorite letter: a \ b \ c \ x \ y \ z?""");