// Collection of code snippets by Arne Vajhøj // (likely posted to comp.os.vms/INFO-VAX, INFO-TPU, MACRO32, eksperten.dk, newz.dk, LinkedIn or other place sometime between 1990 and now) using System; using System.Collections.Generic; using System.Text; namespace E { public class Program { public static string XReplace(string s, Dictionary rpl) { StringBuilder res = new StringBuilder(); StringBuilder tmp = null; bool inangles = false; for(int i = 0; i < s.Length; i++) { switch(s[i]) { case '{': inangles = true; tmp = new StringBuilder(); break; case '}': inangles = false; res.Append(rpl[tmp.ToString()]); break; default: if(inangles) { tmp.Append(s[i]); } else { res.Append(s[i]); } break; } } return res.ToString(); } public static void Main(string[] args) { string text = "Lorem ipsum dolor {currency} sit amet, consectetuer adipiscing elit. Quisque et sem {language} ullamcorper est consequat porta."; Dictionary rpl = new Dictionary(); rpl.Add("currency", "DKK"); rpl.Add("language", "Danish"); rpl.Add("foo", "bar"); Console.WriteLine(text); Console.WriteLine(XReplace(text, rpl)); } } }