Search for word in the first array column [closed]

3

How do I search for a word in all rows, but only in the first column of an array that will always increase in size?

string[,] array = new string[1,6]{{"texto","","","","",""}};

    if(array[array.Length, 0].Contains("texto")){
                //Encontrou palavra
        }

I tried that, but it did not work.

    
asked by anonymous 17.10.2016 / 00:26

3 answers

3
for(int i = 0; i <  array.GetLength (0); i++){
            if (array [i, 0].Contains ("Texto")) {
                Debug.Log ("achou");
}

I did it that way.

    
17.10.2016 / 05:04
3

As it is a somewhat more complex algorithm that requires some lines I find it more interesting to make a method that treats this. An extension method makes the use more natural. But if you do not want to just extract the internal logic. But creating abstractions is part of learning.

To get the size of the dimension the correct way is to get the beginning of the range and the end of it by the methods GetLowerBound() and GetUpperBound() . It can work with GetLength() in most cases, but not at all. I'd rather do it right always. Even if it is an exercise. In fact it is even more important, when you are learning it is better to learn right. It is not enough to function. Even if you did not want to use this, just be aware that using only Length does not take the dimension size.

using static System.Console;

public class Program {
    public static void Main() {
        var array = new string[1, 6] {{"texto", "", "", "", "", ""}};
        if (array.ContainsInFirstCol("texto")) {
            WriteLine("achou");        
        }
    }
}

public static class ArrayExt {
    public static bool ContainsInFirstCol(this string[,] array, string search) {
        for (int row = array.GetLowerBound(0); row <= array.GetUpperBound(0); row++) {
            if (array[row, 0].Contains(search)) {
                return true;
            }
        }
        return false;
    }
}

See running on dotNetFiddle or on CodingGround .

Note that an array can not grow in size. The AP response was made in such a way that the code can not be used at other points where another array is declared in another size.

    
17.10.2016 / 01:27
-1

First this is wrong: string array[,] = new string[1,6]{{"texto","","","","",""}}; It should be: string[,] array = new string[1, 6] { { "texto", "", "", "", "", "" } };

Here's the function you're looking for:

public static int IndexOfStringInArrayOfStrings(string[,] arrayDeStrings, string stringQueVocêProcura, int colunaEmQueAFunçãoDeveProcurar)
{
    for (int i = 0; i < arrayDeStrings.Length; i++) // Aqui eu percorro todas as linhas.
    {
        if (arrayDeStrings[i, colunaEmQueAFunçãoDeveProcurar].Contains(stringQueVocêProcura)) // Aqui eu checo se na célula[i, j] têm a string que você procura.
        {
            return i; // Caso tenha a string, imediatamente a função se encerra e retorna sua posição. 0 = Primeira linha do texto.
        }
    }
    return -1; // Se chegar até o fim, certamente não encontrou nada.
}
    
17.10.2016 / 02:03