user10862475
user10862475

Reputation:

Searching for a string in a part of a multidimensional array c#

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].

Upvotes: 1

Views: 2053

Answers (1)

Ryan Wilson
Ryan Wilson

Reputation: 10765

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());
     }
}

Upvotes: 1

Related Questions