Modern C# 6 and Newer Applications
With the advent of C# and String Interpolation the original answers on this question have become outdated. String Interpolation is essentially syntactic sure to make string.Format
. It allows you to place C# and variables inside of a string. The syntax to trigger string interpolation is to put a $
before your string quote and then anything within the curly braces will be interpolated. The following is an example that will insert a variable myName
into a string.
string myName= "Scott";
string text = $"Hello {myName}!";
This is all built into C# 6 and newer applications with no separate download or library/package required.
Old/Outdated Answer
You could use a 3rd party templating framework. My favorite at the moment is Mustache due to it's simplicity and implementations in almost every language. The .NET library for it is Nustache.
The syntax for the templates is like the following:
This is my template I will insert a {{mustache}} right there.
Here's a code example for .NET.
using Nustache.Core;
string template = "This is my template I will insert a {{mustache}} right there.";
Dictionary<string, string> data = new Dictionary<string,string>();
data["mustache"] = "Beard";
string final = Render.StringToString(template, data));