Operation of the "Contains() method"

Asked

Viewed 54 times

1

I have a array of strings:

string[] warnings =
{
"INQUIRY REQUEST",
"ITEM ALREADY EXISTS",
"ITEM NOT FOUND",
"END OF FILE",
};

and I have a variable with the following value:

status = " 18:46:24:97    INQUIRY REQUEST                                                 ";

It arrives with blank spaces and with the time at the beginning. I confirm if the value assigned to this variable is contained within the array in this way:

if (warnings.Any(warning => warning.Contains(status.Trim())))))
{
    // Code here
}
else
{
    // Code here
}

But the condition falls on else even if it contains "INQUIRY REQUEST" within the variable status. How can I correct or improve this condition of if?

1 answer

1


You’re telling me to look for a haystack on a needle, and you obviously don’t have a whole haystack on a needle. In other words it is telling you to search if the text sitio do pica pau amarelo is inside a text sitio, and clearly he is not. Quite the opposite, sitio would be in sitio do pica pau amarelo. Then the result is correct.

If you do otherwise you should solve your problem, I just don’t guarantee if that’s what you want. Something like this:

using static System.Console;
using System.Linq;

public class Program {
    public static void Main() {
        string[] warnings = {
            "INQUIRY REQUEST",
            "ITEM ALREADY EXISTS",
            "ITEM NOT FOUND",
            "END OF FILE",
            };
        var status = " 18:46:24:97    INQUIRY REQUEST                                                 ";
        if (warnings.Any(warning => status.Contains(warning))) WriteLine("achou");
        else WriteLine("não achou");
    }
}

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

Browser other questions tagged

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