1

I have one string that contains integers and strings, separated by a comma. For example:

0, Link Alive,1, Link Dead,2, Link Weak,3, Wiznet 0 Dead,4, Wiznet 1 Dead,5, Wiznets Dead

I want to make an enum out of this string like this:

public enum myEnums {
    Link Alive = 0,
    Link Dead = 2,
    Link Weak = 1,
    Wiznet 0 Dead = 3,
    Wiznet 1 Dead = 4,
    Wiznets Dead = 5
}

I was thinking about changing the string into a char array. After that I loop through the char array. If I detect an integer, I assign its value to a temporary integer value. If I detect a string, I assign its value to a temporary string. After this I'll assign the temporary integer and string to an enumerator.

Only thing is, I don't know how to deal with the comma and the equal sign.

Can someone show me how it's supposed to be done?

2
  • 4
    Enum values can't contain spaces. So you need to rethink that for a start. Commented Oct 23, 2015 at 14:52
  • 1
    Are you writing a code generator? If not, the obvious problem here is that enum values are fixed at compile time, but you won't have a string to process until runtime. Commented Oct 23, 2015 at 14:54

2 Answers 2

1

It sounds to me like what you really ought to be doing is creating a Dictionary<string,int> since unless you are going to generate code, you can't change an enum at runtime, it's constant.

Now looking at your string:

0, Link Alive,1, Link Dead,2, Link Weak,3, Wiznet 0 Dead,4, Wiznet 1 Dead,5, Wiznets Dead

It looks like you have a set of comma delimited values. So split on , and then each pair of values is an int and a string. Make that you dictionary.

So a simple way to do that might look like this (assuming your data is good, i.e. it has a even number of items and every odd item actually can be parsed as an int):

var dict = new Dictionary<int,string>();    
var cells = source.Split(',');
for (var i=0; i < cells.Length; i+=2)
{
    dict[int.Parse(cells[i])] = cells[i+1].Trim();      // Note: you might want to check boundaries first!
}

Or using Linq, you could do something like this:

    string source = "0, Link Alive,1, Link Dead,2, Link Weak,3, Wiznet 0 Dead,4, Wiznet 1 Dead,5, Wiznets Dead";
    var dict = source.Split(',')
        .Select((v,i) => new { v, i })
        .GroupBy(x => x.i/2)
        .ToDictionary(x => int.Parse(x.First().v), x => x.Skip(1).First().v.Trim());

Here's a fiddle.

To explain what we are doing here:

  • First with Split your string on ,. This give us a string array with ["0","Link Alive","1","Link Dead",...]
  • Next we use Select to select each item and it's index in a pair. So now we have a collection of objects that looks something like [{v="0",i=0},{v="Link Alive",i=1},...]
  • Now we group this by dividing the index by 2. Because this is integer division, it will truncate. So 0/2 == 0 and 1/2 == 0 and 2/2 == 1 and 3/2 == 1. So we are sorting into pairs of values.
  • Finally we convert these groups (which we know are pairs of values) into a dictionary. To do that we use the first item in each group and parse it into an int and use that as the key for our dictionary. Then we use the second value as the value. This finally gives us our dictionary

Now with you dictionary, if you want to look up a value, it's easy:

var myValue = dict[2];   // myValue is now "Link Weak"
Sign up to request clarification or add additional context in comments.

Comments

1

By enumerator I assume you mean something over which you can iterate. An 'enum' is basically a set of named integers.

So if you have a string of items separated by commas and want to 'iterate' over them, then this may help:

string input = "0, Link Alive,1, Link Dead,2, Link Weak,3, Wiznet 0 Dead,4, Wiznet 1 Dead,5, Wiznets Dead"
string[] parts = input.split(new char[] {','}, StringSplitOptions.RemoveEmptyEntries);
foreach (string part in parts)
{
    // do something
}

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.