6
string formIdList = "8256, 8258, 8362, 8120, 8270, 8271, 8272, 8273, 8257, 8279, 8212, 8213, 8214, 8215, 8216, 8217, 8218, 8219, 8231, 8232, 8233, 8234, 8235, 8242, 8248, 8251, 8252, 8254, 8255, 8262, 8263, 8264, 8265, 8266, 8290, 8292, 8293, 8294, 8300, 8320, 8230, 8227, 8226, 8225, 8224, 8223, 8222, 8221, 8291, 8261, 8241, 8228, 8220, 8211, 8208, 8207, 8206, 8205, 8204, 8203, 8202, 8201, 8153, 8151, 8150, 8130, 8122, 8000, 8101, 8102, 8103";

var temp = formIdList.Split(',');

List<int> ids = new List<int>();

I need to load the temp into ids. I can use a for-loop but I'm sure there is a better way.

coder
  • 4,121
  • 14
  • 53
  • 88
  • 1
    Since you know the size of the temp array, initialize your list to that size as to cut back on re-sizing the internal array. And by "better", you mean less code? A For Loop is how it'll be implemented in the end. – Bengie Jan 10 '12 at 19:27
  • possible duplicate of [How to create a List from a comma separated string?](http://stackoverflow.com/questions/910119/how-to-create-a-listt-from-a-comma-separated-string) – Igby Largeman Jan 10 '12 at 19:27
  • > Simon Fox: var asIntegers = arr.Select(s => int.Parse(s)); http://stackoverflow.com/questions/1297231/convert-string-to-int-in-one-string-of-code-using-linq – Bengie Jan 10 '12 at 19:30

4 Answers4

24

You could use LINQ:

string formIdList = ...
List<int> ids = formIdList.Split(',').Select(int.Parse).ToList();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2
List<int> ids = formIdList.Split(',').Select(i=>int.Parse(i)).ToList();
ek_ny
  • 10,153
  • 6
  • 47
  • 60
1

Maybe you should use something like ids.AddRange(temp), did you try it?

Aykut Çevik
  • 2,060
  • 3
  • 20
  • 27
1

Linq is great and all, but you do this with a little less heap-thrashing on your own and have more control over what input you find acceptable. The following will yield the integers from any character enumeration separated by comma's and ignoring all white space.

public static IEnumerable<int> ParseInts(IEnumerable<char> idList)
{
    bool valid = false;
    int working = 0;
    foreach (char c in idList)
    {
        if (c >= '0' && c <= '9')
        {
            valid = true;
            working = (working*10) + (c - '0');
        }
        else if (c == ',')
        {
            if(valid)
                yield return working;
            valid = false;
            working = 0;
        }
        else if(!Char.IsWhiteSpace(c))
        {
            throw new ArgumentOutOfRangeException();
        }
    }
    if (valid)
        yield return working;
}

Then you can fill your collection of ints easily enough by using the List<int> constructor:

string formIdList = "8256, 8258, 8362";
List<int> ids = new List<int>(ParseInts(formIdList));

Just depends on what you intend to do with this, how often, and how large the input will be. For small arrays parsed infrequently I would use the Linq method you already accepted. For higher volumes you might try this instead.

csharptest.net
  • 62,602
  • 11
  • 71
  • 89