0

I have an array of strings like so:

[0]Board1
[1]Messages Transmitted75877814
[2]ISR Count682900312
[3]Bus Errors0
[4]Data Errors0
[5]Receive Timeouts0
[6]TX Q Overflows0
[7]No Handler Failures0
[8]Driver Failures0
[9]Spurious ISRs0

just to clarify the numbers in the square brackets indicate the strings position in the array

I want to convert the array of strings to a dictionary with the string to the left of each number acting as the key, for example (ISR Count, 682900312)

I then want to output specific entries in the dictionary to a text box/table in visual studio (which ever is better) it would be preferable for the numbers to be left aligned.

excuse my naivety, I'm a newbie!

1
  • 1
    Which framework are you targeting? Commented Sep 11, 2012 at 11:30

4 Answers 4

4

Pretty Simple. Tried and Tested

string[] arr = new string[] { "Board1", "ISR Count682900312", ... };

var numAlpha = new Regex("(?<Alpha>[a-zA-Z ]*)(?<Numeric>[0-9]*)");

var res = arr.ToDictionary(x => numAlpha.Match(x).Groups["Alpha"], 
                           x => numAlpha.Match(x).Groups["Numeric"]);

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! But... 'System.Array' does not contain a definition for 'ToDictionary' am I missing an assembly reference?
@JimBarton: Yes. Include System.Linq assembly
1

string[] strings = { "Board1", "Messages232" };

        Dictionary<string, int> dictionary = new Dictionary<string, int>();

        foreach (var s in strings)
        {
            int index = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (Char.IsDigit(s[i]))
                {
                    index = i;
                    break;
                }
            }
            dictionary.Add(s.Substring(0, index), int.Parse(s.Substring(index)));
        }

4 Comments

You need to short circuit your loop after you find the first digit.
Thanks for the reply, I get 'Input string was not in a correct format.'
+1 Of course, this is assuming that there is no number embedded in the middle of the "key".
@MarkBertenshaw Good point. If so, the OP should clarify that. Nice catch :)
1
    var stringArray = new[]
                          {
                              "[0]Board1",
                              "[1]Messages Transmitted75877814",
                              "[2]ISR Count682900312",
                              "[3]Bus Errors0",
                              "[4]Data Errors0",
                              "[5]Receive Timeouts0",
                              "[6]TX Q Overflows0",
                              "[7]No Handler Failures0",
                              "[8]Driver Failures0",
                              "[9]Spurious ISRs0"
                          };


    var resultDict = stringArray.Select(s => s.Substring(3))
        .ToDictionary(s =>
                      {
                          int i = s.IndexOfAny("0123456789".ToCharArray());
                          return s.Substring(0, i);
                      },
                      s =>
                      {
                          int i = s.IndexOfAny("0123456789".ToCharArray());
                          return int.Parse(s.Substring(i));
                      });

EDIT: If the numbers in brackets are not included in the strings, remove .Select(s => s.Substring(3)).

Comments

0

Here you go:

string[] strA = new string[10]
{
    "Board1",
    "Messages Transmitted75877814",
    "ISR Count682900312",
    "Bus Errors0",
    "Data Errors0",
    "Receive Timeouts0",
    "TX Q Overflows0",
    "No Handler Failures0",
    "Driver Failures0",
    "Spurious ISRs0"
};
Dictionary<string, int> list = new Dictionary<string, int>();

foreach (var item in strA)
{
    // this Regex matches any digit one or more times so it picks
    // up all of the digits on the end of the string
    var match = Regex.Match(item, @"\d+");

    // this code will substring out the first part and parse the second as an int
    list.Add(item.Substring(0, match.Index), int.Parse(match.Value));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.