SmartStrings 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.
- ✅ Replace named placeholders like
{name},{plan}, etc. - ✅ Optional fallback values using
{name:Guest}syntax - ✅ Fill from:
- A single value
- Multiple ordered values
- An object
- A dictionary
- Nested object
- ✅ Safe: handles
null, missing keys, and extra placeholders gracefully - ✅ Works with .NET Framework (4.6.1+), .NET 6, 7, 8 and future versions
Install via NuGet:
dotnet add package SmartStringsOr reference it from a local .nupkg:
dotnet add package SmartStrings --source ./packagesusing 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."svar 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);
});// Replace first {placeholder}
string Fill(this string template, string value);
// Replace all {..} in order
string Fill(this string template, params string[] values);
/// Replaces placeholders in the template with values from a model or primitive values..
string Fill<T>(this string template, T values);
// Replace named placeholders with dictionary values
string Fill(this string template, Dictionary<string, string?> values);
// Fills a template using the flat properties of a model, allowing custom overrides for nested or formatted values.
string Fill<T>(this string template, T model, Action<TemplateMap<T>> map)SmartStrings targets:
- ✅ .NET Framework (4.6.1+)
- ✅ .NET 6
- ✅ .NET 7
- ✅ .NET 8 and above
If SmartStrings has saved you time or made your life easier, please consider:
- ⭐ Starring the repo
- 📣 Sharing it with others
- 🧩 Contributing with ideas, feedback, or pull requests
Thank you! 🙌
This project is licensed under the MIT License. © 2025 Jonatas Olziris Martins. All rights reserved.
You may use, modify, and distribute this library in commercial and non-commercial applications. Attribution required.
