C# String replace with dictionary

前端 未结 8 2292
谎友^
谎友^ 2020-11-29 02:07

I have a string on which I need to do some replacements. I have a Dictionary where I have search-replace pairs defined. I have created fol

8条回答
  •  伪装坚强ぢ
    2020-11-29 03:12

    Do this with Linq:

    var newstr = dict.Aggregate(str, (current, value) => 
         current.Replace(value.Key, value.Value));
    

    dict is your search-replace pairs defined Dictionary object.

    str is your string which you need to do some replacements with.

提交回复
热议问题