String and Character Literals
String Literals start with a double quote (") or single quote (') and end with the same type of quote that started it. Within the string,
occurrences of the quote character can be escaped by duplicating it.
var hello = "Hello, ";
var world = 'World';
writeLn(hello+world);
Literals enclosed with single quotes (') and consisting only of one individual character are considered to be Character Literals by default, but the compiler will smartly 'upgrade' them to string literals, if it detects that a string is expected in the current context. Literals enclosed with double quotes (") will always be strings.
var o := 'o'; // a Char
var p := "p"; // a String
var qr := 'qr'; // also a String
Character Literals can also be expressed as Character Code Literals – which are made up by a hash symbol (#) followed by a 16-bit value indicating the characters' unicode, without quotation marks. A hexadecimal code can be used if prefixed with $.
var linefeed = #10;
var carriageReturn = #$0d;
String literals enclosed with a double quote (") may contains line breaks, in order to to define multi-line strings. Such literals will include all characters encountered between the opening and closing quote, including all line breaks and all whitespace.
String and character literals can be concatenated with +, or linked/interspersed by
Character Code Literals without the need for an explicit +.
var MyString := 'Hey there!';
var MyString2 := "Don't Stop Believing" // The apostrophe is fine because the string is surrounded by double quotes
var MyString3 := 'Don''t Stop Believing' // The apostrophe escaped
var MyString3 := 'Don'#0039't Stop Believing' // The apostrophe as character code literals
var MySingleChar := 'x'; // A Char, because length is 1
var MySingleCharString: String := 'x'; // Treated as string based on context
Interpolated Strings
String Literals can be interpolated with values at runtime, if they are prefixed by a dollar ($) character. Inside an interpolated string literal, any range of code surrounded by curly braces ({...}) will be interpreted as a code expression, which will be compiled, evaluated at runtime and inserted in the string at this position.
var TimeString := $'It is {DateTime.UtcNow} o'' clock!';
To use a literal curly brace inside an interpolated string, the brace can be duplicated from escape interpolating:
var lValue := 5;
var MyString := $'The {{curly} gets replaced by a {lValue}'; // "The {curly} gets replaced by 5"
Interpolated string literals can be used for Constants, if all interpolated values are constant, themselves.
Note: Interpolated strings are considered obsolete, superseded by Raw Strings as described below.
Raw strings
Raw Strings, new in Oxygene .2345 combine the best of multi-line and interpolated strings, with the ability to control leading whitespace and more flexible escaping of both quotes and curly braces.
Raw strings always start with one or more hash (#) symbol, followed by one or more double quote (") symbols.
The same number of quotes that starts the raw string literal is required to terminate it. Quote symbols can be freely used inside the string without the need to be escaped, as long as not more than one less are used consecutively. For example, if the string literal has the need of a set of three consecutive quotes (""") it its text, it should be started with four (or more) quotes:
var s := #""""This is a string that has three """ quotes."""";
Raw strings are always interpolated. The number of has symbols at the start defines, how many curly braces are used to start an interpolation. Sets of fewer curly braces can be used freely and without escaping. For example, to represent Json code with single curly braces, and still use interpolation, one would use two (or more) hash symbols:
var lJson := ##""{ "Value" : }""; // #"{ "Value" : 8 }";
Raw strings can either be single-line, as in the examples above, or contain line breaks. For a raw string to allow line-breaks, the actual content of the string must start on a new line after the opening sequence, and end on the line above closing quotes:
var lMoreJson := ##""
{
"Value" : 8,
"Value2" : "Boo",
"Value3" : ,
}
"";
For multi-line raw strings, a base indent is calculated by the number of whitespace in front of the closing quote(s). The same amount of leading whitespace will be removed from the start of each line of the final string. If any non-empty line is not indented far enough to remove said whitespace, a compiler error is raised.
No trailing whitespace is preserved for raw strings (just as the Fire code editor does not preserve trailing whitespace, either). Tab characters in the source file will be treated as two spaces.
In the above example, the lines with the curly braces ({ and }) would have no leading whitespace, and the lines defining the three values would have two spaces.
This allows raw strings to be indented to match the rest of the surrounding code, without the indentation affecting the string value.
Note: To start or end a raw string with a quote char, use multi-line syntax:
var lQuote := #""
"War is Peace."
"";