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.{date:yyyy-MM-dd}, {price:C2}appsettings.json binding{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!"
// DateTime formatting
var template = "Event: {date:yyyy-MM-dd}";
var result = template.Fill(new { date = new DateTime(2025, 12, 17) });
// Result: "Event: 2025-12-17"
// Currency formatting with culture
var template = "Price: {amount:C2}";
var result = template.Fill(new { amount = 1299.99m }, new CultureInfo("en-US"));
// Result: "Price: $1,299.99"
// Configuration URLs
var urlTemplate = "https://api.com/v{version}/orders?date={date:yyyy-MM-dd}";
var url = urlTemplate.Fill(new { version = 2, date = DateTime.Now });
// Result: "https://api.com/v2/orders?date=2025-12-17"
// Program.cs
builder.Services.AddSmartStrings("en-US");
// Or from appsettings.json
builder.Services.AddSmartStrings(builder.Configuration.GetSection("SmartStrings"));
// appsettings.json
{
"SmartStrings": {
"DefaultCulture": "pt-BR",
"InheritThreadCulture": true
}
}
// Usage - automatically uses configured culture
"{price:C2}".Fill(new { price = 29.99m }); // "R$ 29,99" with pt-BR
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 SmartCo
TemplateString.Fill("Hello {USERNAME}", new { USERNAME = "Joana" });
TemplateString.Fill("User: {NAME}", user, map => {
map.Bind("NAME", u => u.User.Name);
});
// With culture
TemplateString.Fill("Price: {amount:C2}", new { amount = 29.99m }, new CultureInfo("en-US"));
dotnet add package SmartStrings
📚 Full source, license, and documentation available at
github.com/jonatasolmartins/smart-strings