1

I need to find the location of a string in a part of a multidimensional array.

If you have:

string[,] exampleArray = new string[3,y]

Where 3 is the number of columns and y is the number of rows, I need to find the location of a string in the full y row but only in the second or third 'column'. So I want my program to search for the location of the string in [1,y] and [2,y].

1
  • 1
    Welcome to SO! Can you show any work you've done towards this problem? It may help clear up the issue and inform others how to help. Commented Jan 12, 2019 at 18:24

1 Answer 1

1

One possible solution is to iterate your rows and check each column of index 1 and 2 on each row.

//Example array to search
string[,] exampleArray = new string[y,3]
//string to search for
string searchedString = "someValue";

//for-loop to iterate the rows, GetLength() will return the length at position 0 or (y)
for(int x = 0; x < exampleArray.GetLength(0); x++)
{
     if(string.Equals(exampleArray[x, 1], searchedString))
     {
         //Do something
         Console.Writeline("Value found at coordinates: " + x.ToString() + ", " + 1.ToString());
     }

     if(string.Equals(exampleArray[x, 2], searchedString))
     {
         //Do something
         Console.Writeline("Value found at coordinates: " + x.ToString() + ", " + 2.ToString());
     }
}
Sign up to request clarification or add additional context in comments.

4 Comments

thank you!, is it considered bad code if your columns are much bigger than your rows?
@Michiel What do you mean by "bigger", if you mean you have more columns than rows, no, I mean a datatable might have 20 columns and depending on its use could have 0 - 20 rows.
Yes, like the first unedited post where i was wrong, switching the columns and rows
@Michiel A row would be one instance of something, like a person, and a person could have lots of features (columns), for example (Height, Weight, Eye Color, Hair Color, etc..) and if the only people who exist at some given time are you and I, you would only have 2 rows, but lots more columns, so I don't think you can clasify it as bad code, its more situational.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.