471,336 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

indexOf question

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
Jun 27 '08 #1
3 1137
raulavi wrote:
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
This was literally *just* discussed a couple threads back:
http://groups.google.com/group/micro...3be990c83f93f#

Chris.
Jun 27 '08 #2
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]
"raulavi" wrote:
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
Jun 27 '08 #3


"Morten Wennevik [C# MVP]" <Mo************@hotmail.comwrote in message
news:E6**********************************@microsof t.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
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by kd | last post: by
11 posts views Thread by Al | last post: by
4 posts views Thread by sh | last post: by
5 posts views Thread by Chizl | last post: by

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.