Check if a word is within a phrase

10

Is there a way to check if a word is within a user-described phrase?

Ex:

palavra = "criativo";

Sentence written by the user:

"Eu sou criativo"

    
asked by anonymous 16.10.2015 / 15:43

2 answers

17

You can use the Contains () .

    string frase = "Eu sou criativo";
    string palavra = "criativo";

    if(frase.Contains(palavra))
    {
        faça algo;
    }
    
16.10.2015 / 15:46
5

Just to complement the response from @ Mathi901.

In addition to using Contains() to validate yourself a string exists within another, you can also use StartsWith() "to verify that a string starts with a given string , or the EndsWith() to check if it ends with given string .

string frase = "eu sou criativo";

//Validar se contém "criativo"
if(frase.Contains("criativo")) { ... }

//Validar se começa com "criativo"
if(frase.StartsWith("criativo")) { ... }

//Validar se termina com "criativo"
if(frase.EndsWith("criativo")) { ... }
    
16.10.2015 / 15:53