Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the string array of all keys from the dictionary and modify every string in the same way

Tags:

c#

dictionary

Easy and simple (but not very nice) way is just to get the array of the keys and iterate it updating every string.

string[] mapKeys = myDictionary.Keys.ToArray();

    for (int i = 0; i < mapKeys.Length; i++)
        mapKeys [i] = mapKeys [i].Replace("substringToRemove", "");

But is there any way to do it in 1 line of code (e.g. using LINQ)?

like image 800
user1271551 Avatar asked Feb 06 '23 12:02

user1271551


1 Answers

mapKeys = mapKeys.Select(o=>o.Replace("substringToRemove", string.Empty)).ToArray();

or from your myDictionary:

string[] mapKeys = myDictionary.Keys.Select(o=>o.Replace("substringToRemove", string.Empty)).ToArray();
like image 74
kurakura88 Avatar answered Feb 12 '23 10:02

kurakura88