-
-
Notifications
You must be signed in to change notification settings - Fork 107
Character Literals in Format Strings
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
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.
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.
string.Format("\t")
and Smart.Format(@"\t")
will return a "TAB" character
Character literals are allowed in all formatters.
Character string literals are not converted.
string.Format(@"\t")
and SmartFormat(@"\t")
will return the 2 characters '\'
and 't'
.
- Syntax, Terminology
- Placeholders and Nesting
- string.Format Compatibility
- Character Literals in Format Strings
- HTML With CSS or JavaScript
- Data Source Extensions
- Default _ DefaultFormatter
- Lists _ ListFormatter
- Choose _ ChooseFormatter
- Condition _ ConditionalFormatter
- Null _ NullFormatter
- SubString _ SubStringFormatter
- RegEx _ IsMatchFormatter
- Pluralization _ PluralLocalizationFormatter
- Localization _ LocalizationFormatter
- Templates _ TemplateFormatter
- TimeSpan _ TimeFormatter
- XML _ XElementFormatter
- Extension Methods
- Home
- Common Pitfalls
- HTML with CSS or JavaScript
- Overview
- Main Features
- Formatters
- Extra Features
- Console and StringBuilder
- TemplateFormatter
- SmartSettings to control Smart.Format behavior
- Additional Info
- License