1 Replies - 402 Views - Last Post: 12 September 2012 - 11:30 PM Rate Topic: -----

#1 martyn19  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 12-September 12

need help with ArrayList

Posted 12 September 2012 - 10:49 PM

hey guys, need some help with an arraylist for part of my irc client.
line from the IRC server ":punch.va.us.dal.net 353 botname = #my_cha :botname @person1 @person2"

that's the names list given by the irc server. I'm attempting to read the names in to a list array
but rearrange the list so that the names begging with the "@" symbol come first.

all I have is:
            ArrayList arrUsers = new ArrayList();

            string Names = ":punch.va.us.dal.net 353 botname = #my_cha :botname @person1 @person2";
            int offset = Names.LastIndexOf(":");
            string parseNames = Names.Substring(offset, Names.Length-offset);
            string[] strSplit = parseNames.Split(' ');
            arrUsers.AddRange(strSplit);

            foreach (string name in arrUsers)
            {
                if (name.Contains("@"))
                {
                    arrUsers.Insert(0, name);
                }
                MessageBox.Show(name);
            }



Is This A Good Question/Topic? 0
  • +

Replies To: need help with ArrayList

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3663
  • View blog
  • Posts: 11,482
  • Joined: 05-May 12

Re: need help with ArrayList

Posted 12 September 2012 - 11:30 PM

You should use List<string> instead of ArrayList to keep your code typesafe.

You are modifying the list that you are iterating over in your foreach on line 9. It would be better to do a foreach over array the comes from the Split() call.

On line 11, you should just inspect the first character. Using Contains() will return true if there is any '@' in the string.

So for the if statement you have code that handles inserting into the beginning for names with an '@'. Why aren't you adding the names without an '@' to the end of the list?

This post has been edited by Skydiver: 12 September 2012 - 11:31 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1