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

Asked

Viewed 3,416 times

8

I’m doing a little program that searches inside a vector. The problem I’m having is that if he finds the name he gives a message saying it was found but then gives another message saying that it was not found, the code I’m 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");
    }    
}
  • If any of the answers helped you mark it as correct so that future visitors have a reference.

  • And I’m new here, I don’t know how you do it

  • Just click on the symbol of checked http://imgur.com/a/sJi2a

4 answers

12


The problem is that in the second for the message is displayed as soon as it finds a different name.

No need to go through the array twice.
If it is traversed to the end without finding, 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 that. However, since you are learning, it is important to know what is wrong with your code and to know how to solve it in the "traditional" way, before using more "advanced" language features.

12

If you can use LINQ (Contains()) can resolve on if who decides whether or not to meet.

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

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

If you want you can simplify even more and until the if, then 5 lines (or 7 depending on the style) can turn into only one.

Obviously I changed the variable and the method of writing on screen to be able to test without dependence on its application, which alias reminds me that screen and business rule should be separate. If it’s just a drill, fine, but keep this in mind.

Your code would look like this:

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

6

Uses the Contains:

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

if(nomes.contains(txtbusca.Text))
    MessageBox.Show("nome encontrado");
else
    MessageBox.Show("nome não encontrado");
  • 1

    thank you so much for your help

  • 1

    Actually this only works if you are using LINQ. And the method is Contains(). And there’s one parenthesis left, but those two mistakes are just typing. And the lack of keys will not cause problems there, but it is a style that helps cause maintenance problems.

  • @bigown, if I’m not mistaken, the Contains() method is for searching inside a string, while the contains() method is for searching inside an array. From what I understood of the question he wanted to look for whether an array contained a string and not whether a string contained a string.

  • 4

    No, there is no contains(), no . NET method is lowercase. It searches on any enumerable object. The type string has a Contains() specialized, but if not, it would work with LINQ. No LINQ only if the type has and few have, List is one of the ones you have too. In my answer I even showed that it works as expected, yours neither compiles, can test.

4

You can use Linq to find as well

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 the Count(x => x == txtbusca.Text) I’m assigning a variable x which will have the value of each item in the array after I compare the value of x with that of hisTextBox, at the end of this process you will have the amount of names that were found.

Then I check if the system found a name for the > 0

  • 6

    Tip: no need to do Where and then Count. Count also receives a predicate.

  • 3

    Complementing: To use Any(x => x == txtbusca.Text) seems an even better option.

Browser other questions tagged

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