Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Confused about the String Contains function

3 views
Skip to first unread message

SMJT

unread,
May 8, 2008, 9:29:48 AM5/8/08
to
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)

Ignacio Machin ( .NET/ C# MVP )

unread,
May 8, 2008, 9:36:43 AM5/8/08
to

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;

Marc Gravell

unread,
May 8, 2008, 9:42:12 AM5/8/08
to
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

parez

unread,
May 8, 2008, 9:51:54 AM5/8/08
to
On May 8, 9:29 am, SMJT <shanemjtowns...@hotmail.com> wrote:

"abc" + "" = "abc"

it means that "abc" contains ""

Chris Bordeman

unread,
May 8, 2008, 3:34:10 PM5/8/08
to
Seems like ("").Contains("") would be true.

"SMJT" <shanemj...@hotmail.com> wrote in message
news:3dd17fb1-44de-4c76...@d45g2000hsc.googlegroups.com...

SMJT

unread,
May 9, 2008, 4:14:18 AM5/9/08
to
Thanks for all the replies and explanations.

>> (whole+part).Contains( part) return true ALWAYS no? ...
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?

>>"AnyOldText".IndexOf("") == 0;

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.

>>"abc" + "" = "abc", it means that "abc" contains ""
No it doesn't, it just means you concatenated nothing to the original
string so I would expect it to remain unchanged.

>>("").Contains("")
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.

Peter Duniho

unread,
May 9, 2008, 1:57:36 PM5/9/08
to
On Fri, 09 May 2008 01:14:18 -0700, SMJT <shanemj...@hotmail.com>
wrote:

> Thanks for all the replies and explanations.
>
>>> (whole+part).Contains( part) return true ALWAYS no? ...
> 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?

But that's just it. Every zero-length substring of your original string
is "empty". A string of length N has N zero-length substrings in it. One
for each character position in the string of length N.

>>> "AnyOldText".IndexOf("") == 0;
> yes but so does "AnyOldText".IndexOf("A") so which is it position 0 an
> empty string or a text stream?

What do you mean by "text stream"?

In any case, for a string of length N, there are N+1 strings you can pass
to IndexOf() that will return 0. The fact that you can get
"AnyOldText".IndexOf("A") to return 0 is no more a problem than that you
can also get "AnyOldText".IndexOf("An"), "AnyOldText".IndexOf("Any"), etc.
to return 0.

There's nothing wrong at all for allowing more than one string to return
the same character index for the IndexOf() method, including the empty
string "".

> 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.

It's not just theoretically true. It's logically true. At every
character position, there are a number of possible substrings that can be
found at that position. Including the zero-length substring "".

>>> "abc" + "" = "abc", it means that "abc" contains ""
> No it doesn't, it just means you concatenated nothing to the original
> string so I would expect it to remain unchanged.

I agree that example was a bit awkward. However, it's a specific example
of making the statement: "string A contains string B if and only if there
exist strings A1 and A2 such that A1 + B + A2 = A". Since "" is a valid
string, then for string "abc", we have as candidates for A1 the strings
"", "a", "ab", and "abc", and as candidates for A2 the strings "abc",
"bc", "c", and "", respectively.

>>> ("").Contains("")
> Yeah, ok that makes sense and I expected this to be the only time
> Contains returned TRUE.

It's clear you expected that. But your expectation wasn't correct, or
even logical. The Contains() and IndexOf() methods would be logically
inconsistent if they treated "" differently from any other string. So
they don't. The empty string "" follows all of the same rules for
Contains() and IndexOf() that other strings follow.

Pete

Ben Voigt [C++ MVP]

unread,
May 9, 2008, 1:59:23 PM5/9/08
to
SMJT wrote:
> Thanks for all the replies and explanations.
>
>>> (whole+part).Contains( part) return true ALWAYS no? ...
> 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.

>
>>> "AnyOldText".IndexOf("") == 0;
> 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.

Jon Skeet [C# MVP]

unread,
May 9, 2008, 5:02:46 PM5/9/08
to
SMJT <shanemj...@hotmail.com> wrote:
> Thanks for all the replies and explanations.
>
> >> (whole+part).Contains( part) return true ALWAYS no? ...
> 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.

> >>"AnyOldText".IndexOf("") == 0;
> 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.

> >>"abc" + "" = "abc", it means that "abc" contains ""
> 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".

> >>("").Contains("")
> Yeah, ok that makes sense and I expected this to be the only time
> Contains returned TRUE.

Why?

--
Jon Skeet - <sk...@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

0 new messages