473,241 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

Confused about the String Contains function

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)

Jun 27 '08 #1
8 4615
On May 8, 9:29*am, SMJT <shanemjtowns...@hotmail.comwrote:
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;
Jun 27 '08 #2
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
Jun 27 '08 #3
On May 8, 9:29 am, SMJT <shanemjtowns...@hotmail.comwrote:
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 ""
Jun 27 '08 #4
Seems like ("").Contains("") would be true.

"SMJT" <sh*************@hotmail.comwrote in message
news:3d**********************************@d45g2000 hsc.googlegroups.com...
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)

Jun 27 '08 #5
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.
Jun 27 '08 #6
On Fri, 09 May 2008 01:14:18 -0700, SMJT <sh*************@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
Jun 27 '08 #7
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.
>
>>"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.

Jun 27 '08 #8
SMJT <sh*************@hotmail.comwrote:
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
Jun 27 '08 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: SamSpade | last post by:
There seems to be two ways to put things on the clipboard ( I don't mean different formats): SetClipboardData and OleSetClipboard If I want to get data off the clipboard do I care how it was put...
1
by: Shane | last post by:
I want the function in Code Snippet #1 below to return a string of "200" by making a web request for the vbprogram.aspx that contains the PageLoad function in Code Snippet #2 below. Instead, I am...
7
by: Adrian Parker | last post by:
'function to convert null to nothing Function CheckDate(ByVal DRow As DataRow, ByVal strCol As String) As Date If DRow.Item(strCol) Is System.DBNull.Value Then Return Nothing Else Return...
17
by: Tom | last post by:
Is there such a thing as a CONTAINS for a string variable in VB.NET? For instance, I want to do something like the following: If strTest Contains ("A","B", "C") Then Debug.WriteLine("Found...
13
by: Marvin | last post by:
Hi: I have been programming with OOP and C++ for over 10 years but I am new to javascript. I am very confused about one aspect and would appreciate it if someone could explain the differences...
4
by: Simon Verona | last post by:
I'm a little confused I have a string that contains a number eg 11 that I want to format into a 4 character string with leading zeros ie 0011 I guess I need to use the "format" function.... ...
7
by: eric | last post by:
hello i'm confused by an example in the book "Effective C++ Third Edition" and would be grateful for some help. here's the code: class Person { public: Person(); virtual ~Person(); // see...
8
by: manmit.walia | last post by:
Hello Everyone, Long time ago, I posted a small problem I had about converting a VB6 program to C#. Well with the help with everyone I got it converted. But I overlooked something and don't...
3
by: jacob navia | last post by:
Abstract: Continuing the discussion about abstract data types, in this discussion group, a string collection data type is presented, patterned after the collection in C# and similar languages...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.