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;
}
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;
}
6
Use the method Contains()
to know if one text is present within another:
if ("Olá Mundo".Contains("Olá")) {
//Faça o que quer aqui
}
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á' ");
}
Got it here @Renan valeus.
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
True, not necessary. Make it easy in the example for those who are starting. I will edit the answer :-)
Browser other questions tagged c# .net string
You are not signed in. Login or sign up in order to post.
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.
– Maniero
What is "Show the determined value for this string"?
– Maniero
Method String.Contains? https://msdn.microsoft.com/pt-br/library/dy85x1sa(v=vs.110). aspx
– Fernando Mondo
With Contais() gives @Fernandomondo ?
– AndreeH