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

indexOf question

1 view
Skip to first unread message

raulavi

unread,
May 8, 2008, 1:50:01 PM5/8/08
to
vs2005 c#
string var = string.Empty;
why "76090,76091,76092,G0202,G0204,G0206".IndexOf(var)
returns 0 ?

should be -1 for not found...empty is not part of my string being searched
any ideas
thanks

Chris Shepherd

unread,
May 8, 2008, 1:55:01 PM5/8/08
to

Morten Wennevik [C# MVP]

unread,
May 9, 2008, 1:05:00 AM5/9/08
to
Hi,

In case you want to test for an empty string use

if(string.IsNullOrEmpty("Hello World"))
{
// string is either null or ""
}

--
Happy Coding!
Morten Wennevik [C# MVP]

Mythran

unread,
May 9, 2008, 1:49:17 PM5/9/08
to

"Morten Wennevik [C# MVP]" <MortenW...@hotmail.com> wrote in message
news:E6732861-719B-4B22...@microsoft.com...


> Hi,
>
> In case you want to test for an empty string use
>
> if(string.IsNullOrEmpty("Hello World"))
> {
> // string is either null or ""
> }
>
> --
> Happy Coding!
> Morten Wennevik [C# MVP]

I would be hard pressed to use IsNullOrEmpty to check for an empty string.
This is due to the fact that Null means the absence of a value while an
empty string is a string whose value contains no characters (hence empty
string). IsNullOrEmpty is the same as:

("Hello World" == null || "Hello World" == string.Empty)

while checking for an empty string is:

("Hello World" == string.Empty)

Therefore, they are not functionally equivalent. Although, I would usually
use IsNullOrEmpty in place of just an empty string check in *most* cases.
Some cases, you may not want to check for null, maybe you need to do
something different for null strings (raise an exception?) compared to empty
strings (allowed so no exception?).

HTH,
Mythran

0 new messages