How to scan a string and check content within it?

Asked

Viewed 928 times

3

I need to do a check with a certain text within a string. How do I go through this string and search the text to make this check ?

Example:

If(dentro da string contém "Olá")
{
   Mostre o valor determinado para essa string;
}
  • Give more details, put in what you’ve already done, show where you’re having trouble. What is this check? I think this question has been answered a few times here.

  • What is "Show the determined value for this string"?

  • Method String.Contains? https://msdn.microsoft.com/pt-br/library/dy85x1sa(v=vs.110). aspx

  • With Contais() gives @Fernandomondo ?

2 answers

6


3

The method Contains() returns a value indicating whether a specified character substring occurs in this character 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 within an IF you do not need to validate with suaString.Contains("Olá") == true, I did it this way just to facilitate your understanding, so we usually do it this way:

string suaString = "Olá mundo";

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

Reference

  • Got it here @Renan valeus.

  • 3

    Use a == true in if it’s bad huh?

  • The method contains no longer returns a boolean? Why do this one == true ai?

  • @bigown kkkkkk

  • 1

    True, not necessary. Make it easy in the example for those who are starting. I will edit the answer :-)

Browser other questions tagged

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