Confused about the String Contains function

  • Thread starter Thread starter SMJT
  • Start date Start date
S

SMJT

Does anyone know why the string contains function always returns true
if the token is an empty string? I expected it to return false.

"AnyOldText".Contains("")
or
"AnyOldText".Contains(String.Empty)
 
Does anyone know why the string contains function always returns true
if the token is an empty string?  I expected it to return false.

"AnyOldText".Contains("")
or
"AnyOldText".Contains(String.Empty)

why?


you would expect that (both whole and part are string)
(whole+part).Contains( part)
return true ALWAYS no?

Why the above would change if part= String.Empty;
 
That sounds reasonable to me, in the same way that I agree that
"AnyOldText".IndexOf("") == 0;

Not a real justification, but consider a string "abcdef"; it contains
"abcdef", and "abcde", and "abcd", and "abc", and "ab", and "a" - why
wouldn't it contain ""? We've simply reduced it to a substring...

Of course, you could just check the length of your strings before
calling Contains?

Marc
 
Does anyone know why the string contains function always returns true
if the token is an empty string? I expected it to return false.

"AnyOldText".Contains("")
or
"AnyOldText".Contains(String.Empty)

"abc" + "" = "abc"

it means that "abc" contains ""
 
Thanks for all the replies and explanations.
Fair enough, IF an empty string was part of the original string, but
if a string has any contents, how can any part of it be empty?
yes but so does "AnyOldText".IndexOf("A") so which is it position 0 an
empty string or a text stream?
And "AnyOldText".IndexOf("",1) == 1; "AnyOldText".IndexOf("",2) ==
2; "AnyOldText".IndexOf("",3) == 3; etc ... So by that logic there is
an empty string at each of these positions and text, which although
theoretically true isn't exactly useful information.
No it doesn't, it just means you concatenated nothing to the original
string so I would expect it to remain unchanged.
Yeah, ok that makes sense and I expected this to be the only time
Contains returned TRUE.

Anyway thank you all for your replies, it is much appreciated.
 
SMJT said:
Thanks for all the replies and explanations.

Fair enough, IF an empty string was part of the original string, but
if a string has any contents, how can any part of it be empty?

Contains is true when any contiguous subset of the original string is the
sought string. Since a zero-length substring is not discontiguous, and is
equal to the sought string, clearly the condition for Contains is met.
yes but so does "AnyOldText".IndexOf("A") so which is it position 0 an
empty string or a text stream?
And "AnyOldText".IndexOf("",1) == 1; "AnyOldText".IndexOf("",2) ==
2; "AnyOldText".IndexOf("",3) == 3; etc ... So by that logic there is
an empty string at each of these positions and text, which although
theoretically true isn't exactly useful information.

If you wanted "useful" information, you would search for a non-empty string.
 
SMJT said:
Thanks for all the replies and explanations.

Fair enough, IF an empty string was part of the original string, but
if a string has any contents, how can any part of it be empty?

There exists an empty string beginning at every place in the string. I
can't think of any definition of containment for which that's not true.
yes but so does "AnyOldText".IndexOf("A") so which is it position 0 an
empty string or a text stream?

Both, just as "An" is also at the start, and so is "Any".
And "AnyOldText".IndexOf("",1) == 1; "AnyOldText".IndexOf("",2) ==
2; "AnyOldText".IndexOf("",3) == 3; etc ... So by that logic there is
an empty string at each of these positions and text, which although
theoretically true isn't exactly useful information.

Asking for the index of an empty string isn't a question which can
yield useful information though. Ask a silly question, get a silly
answer.
No it doesn't, it just means you concatenated nothing to the original
string so I would expect it to remain unchanged.

The logic seems fairly clear to me: if x+y=z, then z contains y, right?
Now apply the same logic with x="abc", y="" and thus z="abc".
Yeah, ok that makes sense and I expected this to be the only time
Contains returned TRUE.

Why?
 
Back
Top