Character Literals in Format Strings - axuno/SmartFormat GitHub Wiki
1. Character Literals at Compile Time
A character literal is a value. It is usually assigned to a variable of type char
. Character literals in C# are enclosed in single-quote characters.
Whenever the format string comes from code, the compiler will take care of converting the character literals into their Unicode representation:
Example:
string.Format("\t")
Smart.Format("\t")
// resulting character in both cases: TAB
2. Character Literals Created at Runtime?
Things are different, when the format string is read from a file or resource. Example:
string.Format(@"Read from a file \t")
// result: "Read from a file \t"
In the majority of cases this is not the desired outcome.
3. Convert From String to Character Literal
There is a property ConvertCharacterStringLiterals
in SmartSettings
. This setting is relevant for interpreting character literals. If true
(the default), character string literals will be treated just like the compiler would do. E.g.: string "\t"
becomes char '\t'
, string "\n"
becomes char '\n'
etc. Very easy to read and good to control.
a) ConvertCharacterStringLiterals = true
string.Format("\t")
and Smart.Format(@"\t")
will return a "TAB" character
Character literals are allowed in all formatters.
b) ConvertCharacterStringLiterals = false
Character string literals are not converted.
string.Format(@"\t")
and SmartFormat(@"\t")
will return the 2 characters '\'
and 't'
.