How to scan a string and check content inside it?

3

I need to make a check with a given text within a string . How do I go through this string and look up the text to do this check?

Example:

If(dentro da string contém "Olá")
{
   Mostre o valor determinado para essa string;
}
    
asked by anonymous 03.11.2015 / 13:41

2 answers

6

Use the Contains() method to see if a text is present inside another:

if ("Olá Mundo".Contains("Olá")) {
    //Faça o que quer aqui
}
    
03.11.2015 / 13:51
3
The Contains () method returns a value that indicates whether a specified substring occurs in this string.

string suaString = "Olá mundo";

if (suaString.Contains("Olá") == true)
{
    Console.Write("A string contain 'Olá' ");
}
Since the Contains () method returns a Boolean and was used inside an IF you do not need to be validating with suaString.Contains("Olá") == true , I did so just to make it easier to understand, so we usually do this:

string suaString = "Olá mundo";

if (suaString.Contains("Olá"))
{
    Console.Write("A string contém 'Olá' ");
}

Reference

    
03.11.2015 / 13:51