is this a bug in net 2.0

D

DaveL

string s = "Bob,Carol";

string sTest = "Bob";

Console.WriteLine(s.IndexOf(sTest)); // Returns 0 this is valid
sTest=""; // lets say we get this value from a database or someother data
and it is trimmed and empty
Console.WriteLine(s.IndexOf(sTest)); // Returns 0 should this in fact
be -1, this Returns 0 valid location in string
Console.ReadKey();

Thanks DaveP
 
P

Pavel Minaev

string s = "Bob,Carol";

string sTest = "Bob";

Console.WriteLine(s.IndexOf(sTest));  // Returns 0 this is valid
sTest="";  // lets say we get this value from a database or someotherdata
and it is trimmed and empty
Console.WriteLine(s.IndexOf(sTest));  // Returns 0  should this in fact
be -1, this Returns 0 valid location in string

Why should it be -1? You're looking for the first index at which an
empty string can be found within string "Bob". It is trivial to show
that an empty string can be found between every two letters of "Bob",
as well as before the first, and after the last. To put it in more
certain terms, the postcondition for i=s.IndexOf(sub) is:

s == s.Substring(0, i) + sub + s.Substring(i + sub.Length);

It is obvious that for sub=="", the smallest value for which the above
holds is 0, therefore, this is what you get.

Also, I'm pretty sure this was discussed before, either here, or on MS
Connect. Either way, the conclusion that time was that it's expected
behavior, and not a bug.
 
B

Bjørn Brox

DaveL skrev:
string s = "Bob,Carol";

string sTest = "Bob";

Console.WriteLine(s.IndexOf(sTest)); // Returns 0 this is valid
sTest=""; // lets say we get this value from a database or someother data
and it is trimmed and empty
Console.WriteLine(s.IndexOf(sTest)); // Returns 0 should this in fact
be -1, this Returns 0 valid location in string
Console.ReadKey();
An why should "Bob,Carol".IndexOf("") return -1?

In my opinion every position in the string would get a match for an
empty string.
 
D

DaveL

I understand the math
But I am looking for Bob or Carol
to me i should get a -1 not found
if this is not a bug
No problem , just more string Checking

Thanks for the Responses
DaveL
 
C

Cor Ligthert[MVP]

In my idea a bug in the program from the one who used this.

That one is testing if there is an empty string in a string, however in this
case is in my idea the most valid point the zero position.

Cor
 
M

Michael C

DaveL said:
I understand the math
But I am looking for Bob or Carol
to me i should get a -1 not found
if this is not a bug
No problem , just more string Checking

It's more of a design decision imo. They could have done it either way but
chose to return 0. However if you think about the logical they would have
used:

start at pos 0
look for any chrs that don't match between the 2 strings
if none found then return the current pos
increment pos and loop

Michael
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top