Gaduks
Gaduks

Reputation: 681

Cropping lines and columns in String[n,m] C#

I have a 'matrix' of strings that I saved as a String[m,n] object (an array of arrays of strings, I believe, whith m lines and n columns).

I would like to know the best way to crop a whole line or a whole column of that array of arrays.

For example, what is the best way to remove all m=1 indexed entries (the second line of the matrix), such that the third line automatically becomes the second, end the fourth becomes the third and so forward...).

Any help is much appreciated. Thanks.

Upvotes: 2

Views: 209

Answers (3)

ASh
ASh

Reputation: 35721

keywords rows, columns, excel table gave me a hint

there is DataTable class in System.Data namespace, which can do almost all what you are asking about (except that it contains values of object type, not string).

example

int cols = 100;
int rows = 500;
System.Data.DataTable dt = new DataTable();

for (int c = 0; c < cols; c++)
    dt.Columns.Add("col_" + c);

for(int r=0; r<rows; r++)
{
    dt.Rows.Add();
    for (int c = 0; c < cols; c++)
        dt.Rows[r][c] = String.Format("({0}:{1})", r, c);
}

how to get values?

string value_row2_colum_5 = dt.Rows[2].Field<string>(5);
string value_row20_colum_50 = dt.Rows[20][50].ToString();

how to set values?

dt.Rows[2][5] = "set";

how to remove column?

DataTable has Columns property. you can remove any of them, e.g.

dt.Columns.RemoveAt(0);

how to remove row?

DataTable has Rows property. you can remove any of them, e.g.

dt.Rows.RemoveAt(0);

also DataTable has Select and Compute methods which are very helpful in some cases

Upvotes: 2

Robert Langdon
Robert Langdon

Reputation: 33

I think RemoveAt method should do the work for you. Just simply go through your Array in a loop.

Take a look at this

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460238

From my comment: If you need to modify it use a List<List<string>> instead since arrays have a fixed size. Then removing a "line" becomes trivial. But removing a "column" is a bit more work. You need to loop the list and use List.RemoveAt(index) on every nested list.

Here's an example.

int rows = 10; int columns = 5;
List<List<string>> lines = new List<List<string>>(rows);
for (int row = 1; row <= rows; row++)
{
    List<string> fields = new List<string>(columns);
    for (int col = 1; col <= columns; col++)
        fields.Add(string.Format("row{0},col{1}", row, col));
    lines.Add(fields);
}

This list contains 10 lists and each inner list contains 5 strings. Now it's easy to remove the second "line":

lines.RemoveAt(1);

or to remove the second "column":

foreach (List<string> line in lines)
    line.RemoveAt(1);

The resulting list without the seond line and second column:

foreach (List<string> line in lines)
    Console.WriteLine(string.Join(" | ", line));

row1,col1 | row1,col3 | row1,col4 | row1,col5
row3,col1 | row3,col3 | row3,col4 | row3,col5
row4,col1 | row4,col3 | row4,col4 | row4,col5
row5,col1 | row5,col3 | row5,col4 | row5,col5
row6,col1 | row6,col3 | row6,col4 | row6,col5
row7,col1 | row7,col3 | row7,col4 | row7,col5
row8,col1 | row8,col3 | row8,col4 | row8,col5
row9,col1 | row9,col3 | row9,col4 | row9,col5
row10,col1 | row10,col3 | row10,col4 | row10,col5

Upvotes: 2

Related Questions