Search for word in the first array column

Asked

Viewed 468 times

3

How do I search for a word in all lines, but only in the first column of a 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 way, but it didn’t work.

  • I am closing because by the answer posted only the author could answer the question. There were requirements not clearly set out in the question.

  • I’m feeling here on this forum that I’ve reached a few days, a slightly competitive air, man I don’t want reputation points or anything like that, if you can even delete the question, I just had a question, and I managed to solve my problem with a go, And it was only after seeing the example of your answer that I came up with the idea of using a for in a simpler way. Thank you all for your help.

  • The question is not this, is that the question is not clear, only you could have answered the way you wanted, the answer portando is not clear, and answers that are not clear need to be closed, even because the person who will read it later and try to learn from it will get confused because what is written is different than what had to be answered. If you want to talk more property about the operation of the site you can do the [tour] and see the details on [help]. Reinforce the warning above about [Ask]. Thus avoids confusion and difficulties p/q people give adequate answers

  • The Expensive managed to give an adequate response, and yours can also be said to be adequate, for what may be suitable for my situation is different to what might be suitable for the situation of another.

  • As for competitiveness you are right that some people take it there extreme. I received a negative in the reply without knowing why. It is complete and as correct as possible. I even updated it to agree with your response. I did not deny yours, because it is not wrong, at most it does not work in all situations, but this is something you should decide whether you want it like this or not, my answer serves other people who want the most correct. I only denied the wrong answer, after all it does not do what desired, gives error,and showed in comment because mine works correctly

  • The answer of the expensive is quite wrong, for any case, his answer gives easy error, has performance problems, does not answer what you really wanted, among other small things that are more subjective. Yours is correct, as long as you make sure array was created within the norm, if it was created with track out of normal its gives error too, but okay, as this is rare I will not consider that yours is wrong. Mine does everything that is in the question and even more. If it was not exactly what you wanted it is because the question is not clear on that. Requirements are missing.

  • So that’s what I’m saying, in my situation the best would be what I did, but maybe for someone else it’s what you did. As for -1 it wasn’t me, I didn’t even know it existed until I read your comment ali kkkkk

  • Don’t worry that I know it wasn’t you, you don’t even have the privilege of negative yet. Just to confirm what I said about the reply from dear: https://dotnetfiddle.net/ueBlhi

Show 3 more comments

3 answers

4

Since 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 use more natural. But if you don’t want to just extract the internal logic. But creating abstractions is part of learning.

To get the size of the dimension the correct is to take the beginning of the range and the end of it through the methods GetLowerBound() and GetUpperBound(). Can work with GetLength() in most cases, but not at all. I prefer to do right always. Even if it is an exercise. It’s actually even more important, when you’re learning it’s better to learn right. It’s not enough to just work. Even if I didn’t want to wear it, just remember to just wear it Length does not take the size of the dimension.

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

Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.

Note that a array cannot increase in size. The AP response was made in such a way that the code cannot be used at other points where another array is declared with another size.

  • 1

    Complicated :/

  • 1

    @Ícarodantas I didn’t understand. I even had a mistake because I did the opposite of what the question asked. But at least I made one Contains as requested, I made generic, as extension to be natural, and I did only in the first column as specified (without wasting time in looking at the array whole), gives no error outside the track, and works with array with bands out of pattern. Maybe I took a -1 for having made take the first row and not the first column as the request, now is correct and who gave the negative could remove it, or explain what has wrong.

3

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

I got it this way.

  • You can take that Contains po, just like that: if (array [i, 0] == "Texto")

  • It actually depends on what you want, because contains for example if you search for Anan, it will find banana, now == "banana" if you search Anan, it will return null.

  • I understand now/! I even edited my answer ;)

-1

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

Here the function you seek:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.