SmartStrings is a lightweight string templating library for C# with support for named placeholders, format specifiers, culture support, and ASP.NET Core integration.
$ dotnet add package SmartStringsSmartStrings is a lightweight and intuitive C# string templating library. It adds extension methods like .Fill() that let you replace placeholders in strings using objects, dictionaries, or parameter arrays — with optional fallbacks.
💡 Inspired by the flexibility of
$"{name}", but better suited for dynamic or external templates.
{name}, {plan}, etc.{name:Guest} syntaxnull, missing keys, and extra placeholders gracefullyusing SmartStrings;
var template = "Welcome, {user}!";
var result = template.Fill("Alice");
// Result: "Welcome, Alice!"
const string template = "Hello {0}, your plan is {1}";
var result = template.Fill("Joe", "Premium");
// Result: "Hello Joe, your plan is Premium"
// Alternative
var result = TemplateString.Fill(template, "Joe", "Premium");
// Result: "Hello Joe, your plan is Premium"
Or using named placeholders:
var template = "Hello {name}, your {plan} plan is active.";
var result = template.Fill("Jonatas", "Gold");
// Result: "Hello Jonatas, your Gold plan is active."s
var template = "Hi {name}, you're on the {plan} plan.";
var result = template.Fill(new Dictionary<string, string?>
{
["name"] = "Carla",
["plan"] = "Standard"
});
// Result: "Hi Carla, you're on the Standard plan."
var template = "Welcome {name}, your ID is {id}.";
var result = template.Fill(new { name = "Lucas", id = "12345" });
// Result: "Welcome Lucas, your ID is 12345."var template = "Hi {name:Guest}, welcome!";
var result = template.Fill(new { });
// Result: "Hi Guest, welcome!"var card = new Card()
{
User = new User() {
Name = "Brian",
Company = "SmartCo"
}
};
const string template = "Welcome {NAME} from {COMPANY}"
template.Fill(card, map => {
map.Bind("NAME", c => c.User.Name);
map.Bind("COMPANY", c => c.User.Company);
});
// Welcome Brian from SmartCoTemplateString.Fill("Hello {USERNAME}", new { USERNAME = "Joana" });
TemplateString.Fill("User: {NAME}", user, map => {
map.Bind("NAME", u => u.User.Name);
});dotnet add package SmartStrings📚 Full source, license, and documentation available at
github.com/jonatasolmartins/smart-strings