There are a couple of different string types inside X#. For normal strings, the notation can be different for different dialects:
Literal |
VO, FoxPro & Harbour |
Vulcan & Core |
'<char>' |
String literal |
Char literal* |
'<char>...<char>' |
String literal |
Not Supported |
"<char>...<char>" |
String literal |
String literal |
e"<char>...<char>" |
Extended string literal* |
Extended string literal* |
i"<char>.. <char>{expression}" |
Interpolated string literal |
Interpolated string literal |
ei"<char>.. <char>{expression}" or ie"<char>.. <char>{expression}" |
Extended interpolated string literal* |
Extended interpolated string literal* |
The <char> literals in the table that are marked with an asterisk (*) may contain a special escape code:
Character |
Description |
Character that does NOT start with a backslash |
Normal character |
\\ |
Backslash |
\" |
Double quote |
\' |
Single quote |
\0 |
0 character |
\a |
Alert |
\b |
Backspace |
\f |
Form feed |
\n |
Line feed |
\r |
New Line |
\t |
Tab |
\v |
Vertical tab |
\x HEXDIGIT(1-4) |
Hex number of character. 1-4 hex digits. |
\u HEXGDIGIT (4 or 8) |
Unicode number of character. 4 or 8 hex digits. |
Interpolated strings is a feature that allows you to embed local variables, instance variables or other expressions inside literal strings. X# supports two kinds of interpolated strings:
This works like a normal X# string but with an embedded expression:
FUNCTION Start AS VOID
LOCAL Who AS STRING
Who := "World"
Console.Writeline( i"Hello {Who}")
Console.Read()
RETURN
This is a combination of an interpolated string and an extended string. In the example below the \t will be replaced with a tab character:
FUNCTION Start AS VOID
LOCAL Who AS STRING
Who := "World"
Console.Writeline( ie"Hello\t{Who}")
Console.Read()
RETURN
Expression inside Interpolated strings inside X# have the form of
{<interpolationExpression>[,<alignment>][::<formatString>]}
In C# the formatString is prefixed with a single colon. Please note that because in X# the colon (:) operator is the default member access operator for instance fields, properties and methods, it is not always possible to use that operator to separate expressions and format string. We have therefore changed that operator to the double colon (::). I you compile with /allowdot+ then you can also use the single colon for the format separator.
To represent a literal '{' or '}' character you will have to duplicate them inside the string. The expressions inside the string are in italics in this example.
An example of this could be
FUNCTION Start() AS VOID
local X := 2 as INT
local Y := 3 as INT
var pointMessage := i"The point {{{X}, {Y}}} is {Math.Sqrt(X * X + Y * Y)::F3} from the origin"
Console.WriteLine(pointMessage)
// Output is:
// The point {2, 3} is 3.606 from the origin
The expression elements inside the string can use formatting notation just like the String.Format() notation. For example:
FUNCTION Start AS VOID
LOCAL i AS INT
i := 42
Console.Writeline( i"Hello {i:x}") // i is printed in hex notation, so Hello 2a
Console.Read()
RETURN