String Contains Empty returns true

4

Why does using Contains("") in a string non-empty return true?

Example

if ("MinhaString".Contains(""))
{
    Console.WriteLine("String contém vazio");
}
    
asked by anonymous 21.11.2017 / 17:30

1 answer

5

We can say that it is a convention. The idea is that every string has an empty string .

A string is composed of at least one string empty, and probably other characters in addition empty.

Should this be true or false?

"" == ""

I imagine everyone accepts it as true.

Contains() asks if "nothing" is inside a string and is. We consider that every character is empermeado by a "nada". Even if it has no characters, nothing is there.

Then:

"abc" == "abc" + ""

Right?

Both return that they have nothing, because both have. The first one you do not see that has the second you see. Just because you do not see does not mean it's not there.

I could ask why they did not consider that nothing does not exist and is never anywhere. And I would ask what gain do you expect to have?

If you do not have what to compare, what would it be?

Up to null equals null . So it would be inconsistent.

One thing is to ask if a string has an empty, quite another to ask if it is all empty, then

"abc" != ""

This question wants to know if all the characters are equal, until the void. And the void is the same, but the other visible 3 only has in a string , there is at least one difference between the two strings , this makes everything different. >

The == is like a && in all characters, whereas the Contains() is like a || . If it finds an occurrence of the needle in the haystack it is all true, and the emptiness is present throughout string , by definition, then it would always return true.

    
21.11.2017 / 17:40