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

Ampix0

Power Member
Joined
Jan 10, 2012
Messages
527
Reaction score
60
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
 
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
 
Last edited:
Like others said, list are awesome for that.

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

public void removeEntry(string[] array, string entry)
{
// Use a temporary array
string[] tmp = new string[array.Length];
for (int i = 0; i < array.Length; i++)
{
// Only add the ones that are NOT the unwanted entry to tmp
if (array != entry)
{
tmp = array;
}
}
// Replace filter by the temporary array
array = tmp;
}


and use like this

string[] filter ={"a", "b", "c", "d", "e"};

removeEntry(filter, "d");
 
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:
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?
 
as far as I know you cannot do this in array but its a good idea to check stackoverflow.
 
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?
dhyibGl
 
Jesus that was so much more simple than I was making it. I over thought that by a mile. Thank you.
 
arraylist mate not arrays ;)

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>.
 
Back
Top