How to remove a string from a 2d array in C #?

Possible duplicate:
Delete a string of an array of 2D strings in C #

I have a 2d string array, I want to remove the specified string from the array.

+2
source share
5 answers
string[] a = new string[] { "a", "b" }; //dummy string array
int deleteIndex = 1; //we want to "delete" element in position 1 of string
a = a.ToList().Where(i => !a.ElementAt(deleteIndex).Equals(i)).ToArray();

dirty, but gives the expected result ( foreachthrough an array to check it)

EDIT missed the β€œ2d array” part, here is the correct code for the job

    string[][] a = new string[][] { 
        new string[] { "a", "b" } /*1st row*/, 
        new string[] { "c", "d" } /*2nd row*/, 
        new string[] { "e", "f" } /*3rd row*/
    };
    int rowToRemove = 1; //we want to get rid of row {"c","d"}
    //a = a.ToList().Where(i => !i.Equals(a.ElementAt(rowToRemove))).ToArray(); //a now has 2 rows, 1st and 3rd only.
    a = a.Where((el, i) => i != rowToRemove).ToArray(); // even better way to do it maybe

updated code

+4
source

As stated above, you cannot delete from an array.

If you need to delete rows, quite often, perhaps change the use of the 2d array in the list containing the array of strings. That way you can use the delete methods that the list implements.

+1

, , "" . . , , .

, LINQ .

string[][] arr2d =
{
    new[] { "foo" },
    new[] { "bar", "baz" },
    new[] { "qux" },
};

// to remove the second row (index 1)
int rowToRemove = 1;
string[][] newArr2d = arr2d
    .Where((arr, index) => index != rowToRemove)
    .ToArray();

// to remove multiple rows (by index)
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 };
string[][] newArr2d = arr2d
    .Where((arr, index) => !rowsToRemove.Contains(index))
    .ToArray();

LINQ (, Skip(), Take(), TakeWhile() ..).

( ) , LINQ , , . .

string[,] arr2d =
{
    { "foo", null },
    { "bar", "baz" },
    { "qux", null },
};

// to remove the second row (index 1)
int rowToRemove = 1;
int rowsToKeep = arr2d.GetLength(0) - 1;
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)];
int currentRow = 0;
for (int i = 0; i < arr2d.GetLength(0); i++)
{
    if (i != rowToRemove)
    {
        for (int j = 0; j < arr2d.GetLength(1); j++)
        {
            newArr2d[currentRow, j] = arr2d[i, j];
        }
        currentRow++;
    }
}

// to remove multiple rows (by index)
HashSet<int> rowsToRemove = new HashSet<int> { 0, 2 };
int rowsToKeep = arr2d.GetLength(0) - rowsToRemove.Count;
string[,] newArr2d = new string[rowsToKeep, arr2d.GetLength(1)];
int currentRow = 0;
for (int i = 0; i < arr2d.GetLength(0); i++)
{
    if (!rowsToRemove.Contains(i))
    {
        for (int j = 0; j < arr2d.GetLength(1); j++)
        {
            newArr2d[currentRow, j] = arr2d[i, j];
        }
        currentRow++;
    }
}
+1

Instead of an array, you can use the List or ArrayList class. Using it, you can dynamically add an item and delete based on your requirement. The array is fixed in size, which cannot be manipulated dynamically.

0
source

The best way is to work with List<Type>! Elements are ordered as they are added to the list, and each of them can be deleted.

Like this:

var items = new List<string>;
items.Add("One");
items.Add("Two");
items.RemoveAt(1);
0
source

All Articles