Edit: I don't feel this was duplicate- I did fail to elaborate clearly that I wanted to know an efficient way to pad char's to the left of my dictionary values. The question I marked as answer should clarify as it is a very different answer than the answer in other thread. Sorry, I worded it wrong.
I am trying to verify that all the values within my Dictionary are a certain length, and if they are not, pad them with '0' to the left. I know "what" the issue is, I am reading through the dictionary's KVP and then modifying it which of course throws an exception, but I am not sure how to modify my code to make it "legal"
class prefixMe
{
Dictionary<string, string> d = new Dictionary<string, string>();
public prefixMe()
{
d.Add("one", "123456");
d.Add("two", "678");
d.Add("three", "13");
foreach(KeyValuePair<string,string> k in d)
{
StringBuilder sb = new StringBuilder(k.Value);
while(sb.Length != 10)
{
Console.WriteLine("Key:{0}\tValue:{1}", k.Key, sb);
sb.Insert(0, '0');
}
d[k.Key] = sb.ToString();
//k.Value.Replace(k.Value, sb.ToString());// = sb.ToString();
Console.WriteLine("Key:{0}\tNew Value:{1}", k.Key, d[k.Key]);
break;
}
}
}
The break; stops the code from throwing an error, but also makes it so it does not go through for the other values as it breaks out of the foreach loop.
The result should be:
0000123456
0000000678
0000000013
5 Answers 5
Use
foreach(KeyValuePair<string,string> k in d.ToList())
instead of
foreach(KeyValuePair<string,string> k in d)
This creates a list of the key-value-pairs, through which you then iterate. This means that in the loop you can touch the dictionary.
If you don't mind creating a new dictionary, then you can also replace the dictionary quite easily using Linq.
d = d.ToDictionary(x => x.Key, x => x.Value.PadLeft(10, '0'));
3 Comments
Use a for loop instead of a foreach loop. This will allow you to iterate through the Dictionary and modify it in the same go.
EDIT: Upon further Googling/trying to compile a for loop through a dictionary, it seems you can't actually do this. I found How to modify key in a dictionary in C#, it seems you can't even change a key without removing it. So maybe this question will be helpful to you.
Use PadLeft
method, and do not break your loop on first iteration.
public prefixMe()
{
d.Add("one", "123456");
d.Add("two", "678");
d.Add("three", "13");
foreach(var k in d.ToList())
{
if(k.Value.Length != 10)
d[k.Key] = k.Value.PadLeft(10, '0');
}
}
ToList
will allow you to modify your dictionary.
1 Comment
This should do the trick in a line
Dictionary<string, string> result = d.ToDictionary(p => p.Key,p=> p.Value.PadLeft(10, '0'));
Comments
Use a for loop instead of foreach. In a foreach you cannot modify the collection (in your care d) because you are iterating on it
4 Comments
for
loop?
void add(string key, string val) {d.Add(key, val.PadLeft(10, '0'));}