-
-
Notifications
You must be signed in to change notification settings - Fork 105
string.Format Compatibility
The main point about string.Format
compatibility is, how curly braces and colons are processed in the format string.
In most cases string.Format
compatibility does not bring any advantages.
TLDR; Custom formatter extensions cannot be parsed and used with this setting.
SmartFormat acts as a drop-in replacement for string.Format
. Thus curly braces are escaped the string.Format
way:
If the desired output shall be "{literal}
", it means escaping (i.e. doubling) the open and closing curly braces:
string.Format("{{literal}}") == Smart.Format("{{literal}}")
Smart.Format("{{0}} {{{0}}} {{}}", "Zero");
// Outputs: "{0} {Zero} {}"
Determines how to format items, such as numbers and dates:
Smart.Format("{0:N3} | {1:MMMM, yyyy}", 5.5, new DateTime(2010,3,4));
// Outputs: "5.500 | March, 2010"
Inserts spaces before or after the item, aligning it with other items:
Smart.Format("|{0,-10}|{1,10}|", "Left", "Right");
// Outputs: "|Left | Right|"
Same as string.Format
, Smart.Format
uses the current UI culture info for formatting, or you can supply your own IFormatProvider
:
Smart.Format(CultureInfo.GetCultureInfo("en-US"), "{0:C}", 1234)
// Outputs: "$1,234.00"
Smart.Format(CultureInfo.GetCultureInfo("ja-JP"), "{0:C}", 1234)
// Outputs: "¥1234"
// Works only with StringFormatCompatibility = true
Smart.Format("{0:yyyy-MM-dd HH:mm:ss}", new DateTime())`
// same as with
string.Format("{0:yyyy-MM-dd HH:mm:ss}", new DateTime())`
Numbers correspond to the argument index:
Smart.Format("{0} {1}", "Hello", "World");
// Outputs: "Hello World"
While string.Format
is limited to indexed placeholders like in the examples above, SmartFormat will also resolve named placeholders with dot notation:
var date = new { Today = DateTime.Now };
// formats the Date property of a DateTime structure
Smart.Format("It is now {Today.Date:yyyy/MM/dd HH:mm:ss}", date);
TLDR; All features of SmartFormat can be used
- Curly braces are escaped with a backslash instead using double curly braces.
- Colons must always be escaped when used in the option or format part of a placeholder.
-
Alignment
Smart.Format("|{0,-10}|{1,10}|", "Left", "Right"); // Outputs: "|Left | Right|"
-
Localization
Smart.Format(CultureInfo.GetCultureInfo("en-US"), "{0:C}", 1234) // Outputs: "$1,234.00" Smart.Format(CultureInfo.GetCultureInfo("ja-JP"), "{0:C}", 1234) // Outputs: "¥1234"
-
null
Valuesnull
will be output asstring.Empty
Colons must always be escaped when used in the option or format part of a placeholder.
// string.Format allows for colons in the format part...
string.Format("{0:yyyy-MM-dd HH:mm:ss}", new DateTime())`
// ... but it's also allowed to escape colons
string.Format("{0:yyyy-MM-dd HH\:mm\:ss}", new DateTime())
// Does not work with StringFormatCompatibility = false
Smart.Format("{0:yyyy-MM-dd HH:mm:ss}", new DateTime())`
Note: In the last example the Parser
would use the literal substring "yyyy-MM-dd HH" as a custom formatter name.
// Works with StringFormatCompatibility = true | false
Smart.Format(@"{0:yyyy-MM-dd HH\:mm\:ss}", new DateTime())
All SmartFormat extensions can be used, e.g. the ListFormatter
:
var data = new [] {1, 2, 3};
_ = Smart.Format(@"{0:list:*{:N2}*| \: }", data);
// Result: "*1,00* : *2,00* : *3,00*"
Take
SmartSettings.StringFormatCompatibility = false
(the default setting) to use all features of SmartFormat, ortrue
if you're heading for full compatibility withstring.Format
.
- 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