none
remove element from an array of string

    Question

  • hi everybody,

    I have an array of string

    myArray[0] = "Hello World";

    myArray[1] = "This is a sample code";

    myArray[2] = "Delete me";

    how do I delete element 2? or I need to copy everything but element 2? I don't know what would be best in this case and if I have a lot of informations.

    thanks

    Tuesday, August 08, 2006 9:42 PM

Answers

  • Best solution to remove an index of the array is to use Linq.

    Check this out:

                string[] myArray = new string[3];
                myArray[0] = "Hello World";
                myArray[1] = "This is a sample code";
                myArray[2] = "Delete me";
    
                myArray = myArray.Where(w => w != myArray[2]).ToArray(); 
    


    On the end of the code, "myArray" has only lenght of 2 (so index at 2 was removed).

    Hope it helps.


    Mitja
    • Proposed as answer by Ehsan Mohammadi Monday, November 21, 2011 10:49 AM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Sunday, October 23, 2011 7:46 AM
  • string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7" };
    
    List<string> list = new List<string>(array);
    
    //you can sort List<string>!
    list.Sort();
    
    list.Remove("4");//remove specieifed item.
    list.RemoveAt(2);//remove item from index.
    list.RemoveRange(1, 2);//remove a range of items.
    

    Any fool can know. The point is to understand.(Albert Einstein)
    • Proposed as answer by Ehsan Mohammadi Monday, November 21, 2011 10:49 AM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Friday, October 21, 2011 2:22 PM
  • Arrays do not have built-in deletion capabilities.  Use an ArrayList of strings (or List<string> in .NET 2.0), and then call the RemoveAt method to remove the elements you have to.

    If you are going to be doing a lot of deleting in a large list, though, consider using a linked list.
    • Proposed as answer by Prasanna_SSN_India Monday, November 21, 2011 12:09 PM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Tuesday, August 08, 2006 9:58 PM
  • Here is a complete code. Pay attention to the fact that we use System.Collections:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList myArr = new ArrayList();
    
                myArr.Add("Hello World");
                myArr.Add("This is a sample code");
                myArr.Add("Delete me");
    
                Console.WriteLine("Before deletion:");
                ShowArray(myArr);
    
                myArr.Remove("Delete me");
    
                Console.WriteLine("\nAfter deletion:");
                ShowArray(myArr);
    
                Console.ReadLine();
            }
    
            private static void ShowArray(ArrayList myArr)
            {
                foreach (string item in myArr)
                {
                    Console.WriteLine(item);
                }
            }
        }
    }
    
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Sunday, October 23, 2011 7:22 AM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Sunday, October 23, 2011 7:22 AM

All replies

  • Arrays do not have built-in deletion capabilities.  Use an ArrayList of strings (or List<string> in .NET 2.0), and then call the RemoveAt method to remove the elements you have to.

    If you are going to be doing a lot of deleting in a large list, though, consider using a linked list.
    • Proposed as answer by Prasanna_SSN_India Monday, November 21, 2011 12:09 PM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Tuesday, August 08, 2006 9:58 PM
  • yeah he's right.

    It depends on what you are doing with this array, how efficient it has to be , etc.  You could do this a million ways probably.

    You could just do something like

    myArray[2] = null;

    And then when you check later you know you do not have anything there (check for null).

    I would suggest using an ArrayList probably.

    Wednesday, August 09, 2006 3:28 AM
  • Hi.,

         Please try this:

    int index = Array.IndexOf( myArray, "Delete me");

    if ( index != -1 )
    {

    string[] copyStrArr = new string[myArray.Length - 1];

    // copy the elements before the found index
    for ( int i = 0; i < index; i++ )
    {
         copyStrArr[ i ] = myArray[ i ];
    }

    // copy the elements after the found index
    for
    ( int i = index ; i < copyStrArr.Length; i++ )
    {
         copyStrArr[ i ] = myArray[i + 1];
    }

    }

     

    I hope it'll help you..
    soemoe

    Wednesday, August 09, 2006 10:21 AM
  • Please don't. This will work but is horribly inefficient. ArrayList (or List<String>) is much better.
    Wednesday, August 09, 2006 11:50 AM
  • hi.,

          I agreed with ShellShock. I develop like that because it is array of string (not ArrayList). If it is ArrayList, there is not much problem.
          You can do like this.: ( I assume it is ArrayList)

           myArrayList.RemoveAt ( myArrayList.IndexOf ( yourSearchString ) );

    And you can also convert ArrayList to Array of string.
    For example:

            string[ ] copyStr = (string[ ])  myArrayList.ToArray ( typeof ( string ) );

    I hope it'll help you.,
    soemoe

    Wednesday, August 09, 2006 12:08 PM
  • string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7" };
    
    List<string> list = new List<string>(array);
    
    //you can sort List<string>!
    list.Sort();
    
    list.Remove("4");//remove specieifed item.
    list.RemoveAt(2);//remove item from index.
    list.RemoveRange(1, 2);//remove a range of items.
    

    Any fool can know. The point is to understand.(Albert Einstein)
    • Proposed as answer by Ehsan Mohammadi Monday, November 21, 2011 10:49 AM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Friday, October 21, 2011 2:22 PM
  • string[] array = new string[] { "1", "2", "3", "4", "5", "6", "7" };
    
    List<string> list = new List<string>(array);
    
    //you can sort List<string>!
    list.Sort();
    
    list.Remove("4");//remove specieifed item.
    list.RemoveAt(2);//remove item from index.
    list.RemoveRange(1, 2);//remove a range of items.
    

    Any fool can know. The point is to understand.(Albert Einstein)
    You don't need to Sort it though, that will just add some extra overhead.
    Mark good replies as helpful and correct ones as answers! - http://blog.filipekberg.se
    Friday, October 21, 2011 2:33 PM
  • Here is a complete code. Pay attention to the fact that we use System.Collections:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                ArrayList myArr = new ArrayList();
    
                myArr.Add("Hello World");
                myArr.Add("This is a sample code");
                myArr.Add("Delete me");
    
                Console.WriteLine("Before deletion:");
                ShowArray(myArr);
    
                myArr.Remove("Delete me");
    
                Console.WriteLine("\nAfter deletion:");
                ShowArray(myArr);
    
                Console.ReadLine();
            }
    
            private static void ShowArray(ArrayList myArr)
            {
                foreach (string item in myArr)
                {
                    Console.WriteLine(item);
                }
            }
        }
    }
    
    

    Noam B.



    Do not Forget to Vote as Answer/Helpful, please. It encourages us to help you...
    • Proposed as answer by Noam B Sunday, October 23, 2011 7:22 AM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Sunday, October 23, 2011 7:22 AM
  • Best solution to remove an index of the array is to use Linq.

    Check this out:

                string[] myArray = new string[3];
                myArray[0] = "Hello World";
                myArray[1] = "This is a sample code";
                myArray[2] = "Delete me";
    
                myArray = myArray.Where(w => w != myArray[2]).ToArray(); 
    


    On the end of the code, "myArray" has only lenght of 2 (so index at 2 was removed).

    Hope it helps.


    Mitja
    • Proposed as answer by Ehsan Mohammadi Monday, November 21, 2011 10:49 AM
    • Marked as answer by Fahd Friday, May 10, 2013 2:25 AM
    Sunday, October 23, 2011 7:46 AM
  • foreach(string obj in myarray){

    if(obj=="Delete me")

    {

    //just provide value as you want or give the value as null

    obj="Accept me";

    or

    obj=null;//use either of one

    }

    }

    if this help you  then mark this as answer becuase this will help others also.

    Sunday, October 23, 2011 12:36 PM
  • foreach(string obj in myarray){

    if(obj=="Delete me")

    {

    //just provide value as you want or give the value as null

    obj="Accept me";

    or

    obj=null;//use either of one

    }

    }

    if this help you  then mark this as answer becuase this will help others also.

     

    1st of all: Please START using CodeBlock to insert a code decently. 

    2nd of all: Your code provided is not removing an element (index) from an array. It only changes it.

     


    Mitja
    Sunday, October 23, 2011 12:42 PM
  • On the end of the code, "myArray" has only lenght of 2 (so index at 2 was removed).

    Exactly, all strings equal to the one at index 2 were removed. If the goal is to use Where to remove the item at index 2, there is an overload for that:
    myArray = myArray.Where((w, i) => i != 2).ToArray();
    

    Monday, October 24, 2011 9:52 AM
  • Exactly, all strings equal to the one at index 2 were removed. If the goal is to use Where to remove the item at index 2, there is an overload for that:
    myArray = myArray.Where((w, i) => i != 2).ToArray();
    

    Why you use 2 variables "w, i" ?

    w is in this case of your not used at all.


    Mitja
    Monday, October 24, 2011 12:57 PM
  • Why you use 2 variables "w, i" ?

    w is in this case of your not used at all.


    Because Where(Func<T, bool>) has the item as parameter, while Where(Func<T, int, bool>) has the index as second parameter. If you want an index-based Where, you must use the overload taking a Func<T, int, bool>.
    Monday, October 24, 2011 2:37 PM
  • It's been 5 years after I posted the original question and I am surprised to see people giving answers. I think I went with ArrayList...

    Thanks guys for the support

    Sunday, November 20, 2011 8:56 PM
  • Hi Fahd,

    I think people are still answering this because it has not yet been marked as answered.


    Mark good replies as helpful and correct ones as answers! - http://blog.filipekberg.se
    Monday, November 21, 2011 10:45 AM
  • Hey thanks...This was helpful!
    Saturday, March 17, 2012 3:43 AM
  • Best solution to remove an index of the array is to use Linq.

    Check this out:

                string[] myArray = new string[3];
                myArray[0] = "Hello World";
                myArray[1] = "This is a sample code";
                myArray[2] = "Delete me";
    
                myArray = myArray.Where(w => w != myArray[2]).ToArray(); 
    


    On the end of the code, "myArray" has only lenght of 2 (so index at 2 was removed).

    Hope it helps.


    Mitja

    Thanks a lot for the LinQ expression. I was wondering how to remove a section from an array.

    For example:

    int[] arrayOfInt= {1,2,3,4,5,6,7,8,9,0};

    After the operation I want the same "arrayOfInt" to look like that: {5,6,7,8,9,0}

    Is that possible with the same LinQ expression?

    Thanks in advance,

    Bozhidar.


    Monday, September 02, 2013 8:39 AM
  • myArray = myArray.Where(w => w != myArray[2]).ToArray()


    Be careful using this approach.  It will remove ALL elements with the text of "Delete me".  If you only want to remove the last element, use Ehsan's suggestion.

    Saturday, March 08, 2014 6:34 PM
  • I found a simple solution when I didn't find the Remove and RemoveAt option in int array

    myArray[RemoveIndex] = myArray[myArray.getUpperBound(0)];	
    
    Array.Resize(ref myArray, myArray.Lenght -1);

    Hope this helps


    • Edited by Mumin Asaad Sunday, September 20, 2015 9:16 AM Typo
    Sunday, September 20, 2015 9:15 AM