C# String replace with dictionary

前端 未结 8 2282
谎友^
谎友^ 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 02:57

    Seems reasonable to me, except for one thing: it's order-sensitive. For instance, take an input string of "$x $y" and a replacement dictionary of:

    "$x" => "$y"
    "$y" => "foo"
    

    The results of the replacement are either "foo foo" or "$y foo" depending on which replacement is performed first.

    You could control the ordering using a List> instead. The alternative is to walk through the string making sure you don't consume the replacements in further replace operations. That's likely to be a lot harder though.

提交回复
热议问题