Replacing C# String with use of dictionary
Posted by jineesh uvantavida on Thursday, August 9, 2018
Tags: c# string replace replace using dictionary string replace in c# string template replacing in c#
static readonly Regex re = new Regex(@"\$(\w+)\$", RegexOptions.Compiled);
static void Main() {
string input = @"Dear $name$, as of $date$ your balance is $amount$";
var args = new Dictionary<string, string>(
StringComparer.OrdinalIgnoreCase) {
{"name", "Mr Smith"},
{"date", "05 Aug 2009"},
{"amount", "GBP200"}
};
string output = re.Replace(input, match => args[match.Groups[1].Value]);
}Note: Answer is copied from stackoverflow answer
Translate This Page
Make a free website with Yola