tags:

views:

98

answers:

1

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.

+3  A: 

You can do it with a regular expression like this:

foreach (Match m in Regex.Matches("b|123|1,op|999", "([^|,]+),([^|,]+)|([^|,]+)")) {
   string value3 = m.Groups[3].Value;
   if (value3.Length > 0) {
      Options.Add(value3, value3);
   } else {
      Options.Add(m.Groups[1].Value, m.Groups[2].Value);
   }
}

If you measure the performance, I am pretty sure that the regular expression version is slower.

Edit:
Here is a technique that scans the string for separators, and only creates the substrings that are used in the dictionary:

string input = "b|123|1,op|999";
int key = 0;
int value = 0;
for (int i = 0; i < input.Length; i++) {
   switch (input[i]) {
      case ',':
         value = i + 1;
         break;
      case '|':
        Options.Add(input.Substring(key, key == value ? i - key : value - key - 1), input.Substring(value, i - value));
        key = i + 1;
        value = i + 1;
        break;
   }
}
Options.Add(input.Substring(key, key == value ? input.Length - key : value - key - 1), input.Substring(value));
Guffa
Is your edit faster than my code?
TTT
@Alon: Yes. It's about twice as fast. (I measured the regular expression version also, and it's about ten times slower.)
Guffa