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".Co ntains("")
or
"AnyOldText".Co ntains(String.E mpty) 8 4648
On May 8, 9:29*am, SMJT <shanemjtowns.. .@hotmail.comwr ote:
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".Co ntains("")
or
"AnyOldText".Co ntains(String.E mpty)
why?
you would expect that (both whole and part are string)
(whole+part).Co ntains( 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".In dexOf("") == 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
On May 8, 9:29 am, SMJT <shanemjtowns.. .@hotmail.comwr ote:
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".Co ntains("")
or
"AnyOldText".Co ntains(String.E mpty)
"abc" + "" = "abc"
it means that "abc" contains ""
Seems like ("").Contains(" ") would be true.
"SMJT" <sh************ *@hotmail.comwr ote in message
news:3d******** *************** ***********@d45 g2000hsc.google groups.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".Co ntains("")
or
"AnyOldText".Co ntains(String.E mpty)
Thanks for all the replies and explanations.
>(whole+part).C ontains( 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".In dexOf("A") so which is it position 0 an
empty string or a text stream?
And "AnyOldText".In dexOf("",1) == 1; "AnyOldText".In dexOf("",2) ==
2; "AnyOldText".In dexOf("",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.
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".In dexOf("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".In dexOf("A") to return 0 is no more a problem than that you
can also get "AnyOldText".In dexOf("An"), "AnyOldText".In dexOf("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".In dexOf("",1) == 1; "AnyOldText".In dexOf("",2) ==
2; "AnyOldText".In dexOf("",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
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".In dexOf("A") so which is it position 0 an
empty string or a text stream?
And "AnyOldText".In dexOf("",1) == 1; "AnyOldText".In dexOf("",2) ==
2; "AnyOldText".In dexOf("",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.
SMJT <sh************ *@hotmail.comwr ote:
Thanks for all the replies and explanations.
(whole+part).Co ntains( 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".I ndexOf("") == 0;
yes but so does "AnyOldText".In dexOf("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".In dexOf("",1) == 1; "AnyOldText".In dexOf("",2) ==
2; "AnyOldText".In dexOf("",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.co m>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 there?
What about Drag/Drop; is there more than one way for the source to make data
available
Is it always OLE?
|
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 getting a StatusCode
of "OK" instead of 200. What should I change here? Thanks
in advance for any help anyone may be able to give.
Code Snippet #1 (The function that does the web request.)
(from a C# program I wrote.)
|
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 DRow.Item(strCol)
End If
End Function
|
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 characters")
Else
Debug.WriteLine("Did NOT find characters!")
End If
|
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 between the following 2
groups of "statements":
function myFunc (x)
{
a = 1;
| |
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.... ie
newstring=format(oldstring,"????")
What goes in as the format conversion to get the affect I want?
|
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 item 7 for why this is virtual
...
|
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 understand why it is doing this.
Below is my code, I would be greatfull if someone can guide me through
the right path or even help me solve this issue.
Problem: The old tool which was written in VB6 works perfect. But I
needed to convert this to C#...
|
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 (Java).
It stores character strings, and resizes itself to accommodate
new strings when needed.
Interface:
----------
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |