Skip to content
scottrippey edited this page Nov 6, 2011 · 64 revisions

SmartFormat is a string composition library written in C# (.NET 3.5) that can format data into a string with a minimal, intuitive syntax. It uses extensions to provide named placeholders, pluralisation, gender conjugation, and time and list formatting.

The example below shows named placeholders used for pluralisation and gender conjugation:

    string result = Smart.Format("{Name} is {Age} {Age:year|years} old and {Gender:he|she} knows {Friends.Count} {Friends.Count:person|people}.", user);
    // Result: "Scott is 28 years old and he knows 2 people."
    //      or "Jennie is 1 year old and she knows 1 person."

SmartFormat uses extensions, so it's very easy to add and remove features. It's lightweight, efficient, has amazing performance, and [[fully supports the regular String.Format features|Compatibility]]:

var output = Smart.Format("{0} is {1} years old and he has {2:N0} friends.", user.Name, user.Age, user.Friends.Count);

Features

Named arguments

The ReflectionSource extension lets you use property names as placeholders. This enhances the readability and manageability of the template:

string output = Smart.Format("{Name} is {Age} years old and he has {Friends.Count:N0} friends.", user);

Pluralisation

The PluralLocalizationFormatter extension makes it very easy to internationalize (i18n) your application with proper grammatical numbers. Grammatical numbers are often very difficult to localize due to the wide range of language rules, which results in strange grammar in many applications like "There are 1 item(s) remaining" or "Items left: 4".

However, SmartFormat makes proper grammar very easy:

    var result = Smart.Format("There {0:is|are} {0} {0:item|items} remaining", items.Count);
    // Result: "There is 1 item remaining" or "There are 2 items remaining"

SmartFormat automatically recognizes the grammatical rules for 147 languages, and it's very easy to add a new language at runtime.

List formatting

The ListFormatter extension outputs a collection of items and joins them naturally:

Smart.Format("{Name}'s friends: {Friends:{Name}|, |, and}", user)
// Result: "Scott's friends: Michael, Jim, Pam, and Dwight"

Custom extensions

SmartFormat has an extension model that lets you easily extend it with additional features. All the features described above are actually just built-in extensions! Extensions are simple to create and can add a lot of functionality to your templates. See Creating Extensions.

Performance

Even with all these features, SmartFormat remains lightweight and performs extremely well. Features and extensions can be added or removed at runtime, and parsing results can be cached for ultimate performance. At its core, Smart.Format performs nearly as well as String.Format — creating output in mere microseconds.

How do I use SmartFormat in my code?

Simply import the SmartFormat namespace and call the static function Smart.Format, just as you would with with String.Format.

string result = Smart.Format("{Name} {Age}", user);
string result = "{Name} {Age}".FormatSmart(user); // alternative syntax with extension methods

For more detailed usage instructions, please read How Do I Use SmartFormat?.

See also

Clone this wiki locally