Romonov Romonov - 1 year ago 105
C# Question

Replace a specific item in IEnumerable<string>

I am new to C# and dealing with IEnumerable. I want to replace a specific item in IEnumerable. E.g.

IEnumerable<string> m_oEnum = new string[] {"abc","def","ghi", "abcdef"};

I only want to replace the string "abc" with "abc-test", but not change "abcdef".

Answer Source
m_oEnum  = m_oEnum.Select(s => s == "abc" ? "abc-test" : s).ToArray();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download