String Contains Empty returns true

Asked

Viewed 95 times

4

Why when using the Contains("") in a string not empty returns true?

Example

if ("MinhaString".Contains(""))
{
    Console.WriteLine("String contém vazio");
}

1 answer

5


We can say it’s a convention. The idea is that all string has a string empty.

One string is composed of at least one string empty, and probably other characters in addition into the void.

This should be true or false?

"" == ""

I imagine you all take it as true.

The Contains() asks if the "nothing" is inside a string and it is. We consider that every character is committed to a "nothing". Even if it has no characters the nothing is there.

So:

"abc" == "abc" + ""

Right?

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

I could ask because they did not consider that nothing does not exist and is never anywhere. And I would ask you what gain you hope to have with this?

If you have nothing to compare what result?

Until the null is equal to null. Then it would be inconsistent.

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

"abc" != ""

I put in the Github for future reference.

This question wants to know if all characters are equal, up to the empty. And the empty is equal, but the other 3 visible only have in one string, there is at least one difference between the two strings, This makes everything different.

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

  • I did not understand your question that whether ""="" should be true or not. Could you explain how this fits better in the context of my question? For if I test "Minhastring"=="" var return false.

  • @Pablotondolodevargas improved?

  • @Maniero is looking more like a philosophical discussion ("Just because you don’t see it doesn’t mean it’s not there."), but briefly I understand that: every string contains "nothing" by convention.

  • @Pablotondolodevargas is metaphysical medium :D

Browser other questions tagged

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