-5
string original="Blue, Light Blue, Dark Blue";
string replace="Blue";
string replaceTo="Green";
string result=original.Replace(replace,replaceTo);
Console.WriteLine(result);

Output: Green, Light Green, Dark Green

But What I want: Green, Light Blue, Dark Blue

  • You are asking to replace "Blue" with "Green" but you want the result to have "Blue" instead of "Green"? Just do `Console.WriteLine(original)` then – Camilo Terevinto Oct 06 '18 at 16:40
  • the op is saying he only wants to change the instance of blue without any modifiers. ie just change 'blue' but not 'light blue' or 'dark blue'. – Technivorous Oct 06 '18 at 16:46
  • 1
    Possible duplicate of [Way to have String.Replace only hit "whole words"](https://stackoverflow.com/questions/6143642/way-to-have-string-replace-only-hit-whole-words) – fourwhey Oct 06 '18 at 17:15

1 Answers1

2

I would split original into a string[], then iterate over it and replace the exact match.

Code:

string[] lister = original.Split(',');

for (int i = 0; i < lister.Length; i++)
{
    if(lister[i] == "Blue")
    {
        lister[i] = "Green";
    }
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Technivorous
  • 1,682
  • 2
  • 16
  • 22