• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 198
  • Last Modified:

Put string in an ArrayList...

Hi,
I need to put values from string in an ArrayList.
The string is like that : "1,3,5,7,11"  or : "5,8"  or : "2,3,12"........
I need to put only the numbers in ArrayList.
Thanks
0
udir
Asked:
udir
1 Solution
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
the Split() function of C#'s string class does already return an array:
yourtring.Split(',');
0
 
Chandan_GowdaCommented:

 // string seperated by colons ','
    string info = "1,2,3,4";
    string[] arInfo = new string[4]; 
 // define which character is seperating fields
    char[] splitter  = {','};         
    arInfo = info.Split(splitter);
    for(int x = 0; x < arInfo.Length; x++)
    {
        //u r logic
    }

Open in new window

0
 
udirAuthor Commented:
Chandan_Gowda, Thanks a lot  :)
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
evilrixSenior Software Engineer (Avast)Commented:
It might be worth considering using a StringCollection: -

http://msdn2.microsoft.com/EN-US/library/system.collections.specialized.stringcollection.aspx

    class Program
    {
        static void Main(string[] args)
        {
            string s = "1,2,3,4,5,6,7";
            StringCollection sc = new StringCollection();
            sc.AddRange(s.Split(','));

            ArrayList al = new ArrayList();
            al.AddRange(sc);
        }
    }
0
 
evilrixSenior Software Engineer (Avast)Commented:
BTW: The first answer you get isn't always the best solution! You might want to consider leaving a Q open a little longer in future to see if you get a better one.

NB. I am not implying my solution is any better that those already offered, it is just an observation for you to consider.
0
 
udirAuthor Commented:
I will take ur advice,
thanks...
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now