Replace multiple words in string

后端 未结 8 1128
独厮守ぢ
独厮守ぢ 2020-12-18 19:02

I have multiple words I want to replace with values, whats the best way to do this?

Example: This is what I have done but it feels and looks so wrong



        
8条回答
  •  别那么骄傲
    2020-12-18 19:26

    If you're planning on having a dynamic number of replacements, which could change at any time, and you want to make it a bit cleaner, you could always do something like this:

    // Define name/value pairs to be replaced.
    var replacements = new Dictionary();
    replacements.Add("", client.FullName);
    replacements.Add("", event.EventDate.ToString());
    
    // Replace
    string s = "Dear , your booking is confirmed for the ";
    foreach (var replacement in replacements)
    {
       s = s.Replace(replacement.Key, replacement.Value);
    }
    

提交回复
热议问题