1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[Help][C#] Remove item from string array?

Discussion in 'C, C++, C#' started by Ampix0, Nov 11, 2013.

  1. Ampix0

    Ampix0 Power Member

    Joined:
    Jan 10, 2012
    Messages:
    525
    Likes Received:
    60
    Home Page:
    Let's say I have the following code:

    Code:
     string[] filter;
    filter = ["a", "b", "c", "d", "e"]
    
    I need to figure out how to remove filter[3], or "d" from the array filter
     
  2. thepornwebmaster

    thepornwebmaster Newbie

    Joined:
    Oct 28, 2013
    Messages:
    37
    Likes Received:
    4
    Occupation:
    Porn Web Master
    Location:
    The Internet
    Home Page:
    arraylist mate not arrays ;)
     
  3. DarkPixel

    DarkPixel Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 4, 2011
    Messages:
    1,328
    Likes Received:
    1,239
    Location:
    ↓↓↓↓
    Home Page:
    You can't remove items from an array, only from an arraylist.
     
    • Thanks Thanks x 1
  4. Macthetrix

    Macthetrix Regular Member

    Joined:
    Feb 21, 2013
    Messages:
    379
    Likes Received:
    390
    Occupation:
    The Boss
    Location:
    Silicon Valley
    use a list for that
    List<String> someName = new List<String>();

    to add elements you use someName.Add("a");
    someName.Add("d");

    to remove elements someName.Remove("d");
    or remove them with the index someName.RemoveItem(4);

    you use the list the same way you use an array
    Console.WriteLine(someName[4].ToString()); // prints d
     
    • Thanks Thanks x 1
    Last edited: Nov 11, 2013
  5. gaabrielz

    gaabrielz Registered Member

    Joined:
    Nov 17, 2008
    Messages:
    60
    Likes Received:
    35
    Occupation:
    Application Developer
    Location:
    Canada
    Like others said, list are awesome for that.

    But if you really wanna use the array here is some code :



    and use like this

     
  6. 0_00_0

    0_00_0 Senior Member

    Joined:
    Oct 7, 2010
    Messages:
    1,024
    Likes Received:
    486
    Location:
    Canada
    Or..
    Code:
    Public string[] RemoveElement(string[] inArray, int index){
    int tIndex = 0;
    string returnArray = new string[inArray.size-1];
    for(int i = 0; i < inArray.size; i++){
        if(i != index){
            returnArray[tIndex] = inArray [i];
            tIndex ++;
        }
    }
    return returnArray;
    }
    
    I just typed that on my tablet which was a real feat but that is more pseudo code than copy pasta material. I'm sure there are errors!
     
    Last edited: Nov 11, 2013
  7. Ampix0

    Ampix0 Power Member

    Joined:
    Jan 10, 2012
    Messages:
    525
    Likes Received:
    60
    Home Page:
    I hate to be this guy and ask another stupid wuestion. The reason I was using an array was because I could not find out how to convert a multiline textbox to an array list. Can someone help me with that?
     
  8. niloydaemons

    niloydaemons Junior Member

    Joined:
    Jun 23, 2012
    Messages:
    186
    Likes Received:
    71
    as far as I know you cannot do this in array but its a good idea to check stackoverflow.
     
  9. DarkPixel

    DarkPixel Jr. VIP Jr. VIP Premium Member

    Joined:
    Oct 4, 2011
    Messages:
    1,328
    Likes Received:
    1,239
    Location:
    ↓↓↓↓
    Home Page:
    [​IMG]
     
  10. Ampix0

    Ampix0 Power Member

    Joined:
    Jan 10, 2012
    Messages:
    525
    Likes Received:
    60
    Home Page:
    Jesus that was so much more simple than I was making it. I over thought that by a mile. Thank you.
     
  11. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    Code:
    
    var list = txtbox.Lines.ToList();
    var arr = list.ToArray();
    
    
    :)
     
  12. rootjazz

    rootjazz Jr. VIP Jr. VIP

    Joined:
    Dec 21, 2012
    Messages:
    614
    Likes Received:
    313
    Occupation:
    Developer
    Location:
    UK
    Home Page:
    Better to use a List than ArrayList, so you do not need to cast to your type.

    Arraylists were introduced to better handle arrays, for adding / removing etc. But then ArrayList was superceded by List<Type>.