Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C#

Idiom #254 Replace value in list

Replace all exact occurrences of "foo" with "bar" in the string list x

int i;
while ((i = x.IndexOf("foo")) != -1)
    x[i] = "bar";

While "foo" is found in the list
it is replaced by "bar"

This takes quadratic time instead of linear
(replace {"foo" "bar"} x)

New implementation...