Hey, I have this code:
foreach (string x in Value.Split('|'))
{
var y = x.Split(',');
if (y.Length == 1)
Options.Add(y[0], y[0]);
else if (y.Length == 2)
Options.Add(y[0], y[1]);
}
It should process strings like:
b|123|1,op|999
1|2|3|4
... and add them to a Dictionary<string, string>
. It splits the string in the character |
. Then it splits it again in the character ,
. If there is one element, it adds to a dictionary the same key and value. If there are two elements in the array, then it adds to the dictionary an element when the key is the first element in the array, and the value is the second element in the array.
For example, in the string:
b|123|1,op|999
The dictionary should look like:
Key | Value
-----------
b | b
123 | 123
1 | op
999 | 999
It's working, but I'm looking for a more clean way to speed it up using regex or something... The program is that I do not know regex... Any ideas?
Thanks.