Link to home
Start Free TrialLog in
Avatar of Allan
AllanFlag for United States of America

asked on

String Value Into List

Hi Experts!

Thanks for reading this.

If you have a string that contains a list of ids with
comma as a delimiter:

  "332, 123, 782, 2, 87, 30, 1, 45, 32"

How do you get it into the var "tids":

var tids = new List<string>();

Open in new window

TIA!
SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allan

ASKER

wow; that was super quick ged325.

So, that's the same as a List<string>?
I will try it now ...
Avatar of Allan

ASKER

So tried this:

string str = "332, 123, 782, 2, 87, 30, 1, 45, 32";
var tids = str.Split(",");

Got these errors:

Error      1      The best overloaded method match for 'string.Split(params char[])' has some invalid arguments      
Error      2      Argument 1: cannot convert from 'string' to 'char[]'      

Tried:

 var tids = str.Split(Char(44));

but don't know what references Char is from ..


Any ideas?
Avatar of Allan

ASKER

Basically want to go from

//containing a list of IDs from a string w/ delimiter
var tids = new List<string>();

to:

var tntable = tids.AsQueryble();
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allan

ASKER

perfect kaufmed. Thanks again.

I will split points.
Just a small note...

Your original request was for a LIST, but Split() returns an ARRAY.

If you want a List, then pass the results of Split() to the Constructor:
            string data = "332, 123, 782, 2, 87, 30, 1, 45, 32";
            var tids = new List<string>(data.Replace(", ", ",").Split(','));

Open in new window

Note that I threw in a Replace() call to get rid of the space after the comma, which would have been present in the split strings.
Avatar of Allan

ASKER

Thank you Idle_Mind; I have sooooo much to learn.