How to do a search to know if a string is inside a vector in C #

8

I'm doing a small program that does search within a vector. The problem I am having is that if it finds the name it gives a message saying it was found but then it gives another message saying that it was not found, the code I am creating is this:

string[] nomes = { "misael", "camila", "fernando" };

for (int i = 0; i < nomes.Length; i++) {
    if (txtbusca.Text == nomes[i])
    {
        MessageBox.Show("nome encontrado");
    }    
}

for (int i = 0; i < nomes.Length; i++) {
    if (txtbusca.Text != nomes[i])
    {
        MessageBox.Show("nome não encontrado");
    }    
}
    
asked by anonymous 30.05.2017 / 10:09

4 answers

12

The problem is that in the second for the message is displayed barely find a different name.

You do not need to go through the array twice. If it is traversed all the way without finding it, it is because the name is not in the array.

string[] nomes = { "misael", "camila", "fernando" };  

string message = "nome não encontrado";
for (int i = 0; i < nomes.Length; i++) {
    if (txtbusca.Text == nomes[i])
    {
        message = "nome encontrado";
        break; //Foi encontrado, não necessita de procurar mais
    }    
}
MessageBox.Show(message);

Of course there are simpler ways to do this. However, since you are learning, it is important to know what is wrong with your code and how to solve it in the "traditional" way, before using more "advanced" language features.

    
30.05.2017 / 10:29
12

If you can use LINQ ( Contains() ) you can

using static System.Console;
using System.Linq;

public class Program {
    public static void Main() {
        string[] nomes = { "misael", "camila", "fernando" };
        var txtBusca = "camila";
        if (nomes.Contains(txtBusca)) {
            WriteLine("Nome encontrado");
        } else {
            WriteLine("Nome não encontrado");
        }
    }
}

See running on .NET Fiddle . And no Coding Groun d. Also I put it in GitHub for future reference .

If you'd like to simplify it further and until you delete the if , then 5 lines (or 7 depending on the style) can become just one.

Obviously I changed the variable and the writing method on screen to be able to test without depending on your application, which alias reminds me that screen and business rule should be separate. If it's just an exercise, fine, but keep this in mind.

In your code it would look like this:

string[] nomes = { "misael", "camila", "fernando" };
MessageBox.Show(nomes.Contains(txtBusca.Text) ? "Nome encontrado" : "Nome não encontrado");
    
30.05.2017 / 14:44
6

Use Contains :

string[] nomes = { "misael", "camila", "fernando" };

if(nomes.contains(txtbusca.Text))
    MessageBox.Show("nome encontrado");
else
    MessageBox.Show("nome não encontrado");
    
30.05.2017 / 10:22
4

You can use Linq to find it too

string[] nomes = { "misael", "camila", "fernando" };

if (nomes.Count(x => x == txtbusca.Text) > 0)
    MessageBox.Show("nome encontrado");
else
    MessageBox.Show("nome não encontrado");

In% with% I'm assigning a variable Count(x => x == txtbusca.Text) that will have the value of each item in the array after I compare the value of x with that of your x , at the end of this process you will have the number of names which were found.

Then I check if the system has at least found a name with TextBox

    
30.05.2017 / 13:28