String empty test

  • Thread starter Thread starter Julie
  • Start date Start date
J

Julie

I've been using the following to test if a string contains something:

if ((str!=null) && (str!=string.empty))
{
// use string
}

Is this the preferred method? or is there another way to perform the null and
empty test at the same time?

Thanks
 
Nah, what you have is about as good as it gets. As a suggestion to further
bulletproof the test, use the .Trim() method.

if(s != null && s.Trim() != string.Empty)
{
// hello world!
}
 
It looks fine. As alternative to (str != string.empty), you can use
(str.Length > 0), but I doubt that you will see a noticeable performance
difference. Depending on what you consider empty (e.g. string consisting of
blank spaces may be treated as empty by an application), you may also need
to trim white spaces from beginning and end, before checking the string
length, such as str.Trim(); make sure it is not null before. Don't know if
Trim handles tabs, new lines, etc. MSDN doc says it trims white spaces, but
from my experience with the corresponding functions in VBScript,
Transact-SQL, etc, I would double check. If it does not, try something like
str.Trim(" \t\r\n".ToCharArray()), but maybe in C# it works as it is
supposed to.

Alek
 
Julie:

It does not means the same. Look that code.

string s = Initializer();
....
if (s=string.Empty){
//means s=="". Reference to an empty string.
}
....
if (s=null){
//means s has no object reference.
}

LC
 
or is there another way to perform the null and
empty test at the same time?

In Whidbey they're adding a new static method on the String class for
this, called IsNullOrEmpty. So you'll be able to write

if ( !String.IsNullOrEmpty( str ) )



Mattias
 
As an FYI, Empty is actually a public static string var set to Empty = "" in
the string classes static constructor. This should probably be faster then
testing Length == 0, but as you said, probably could not tell the difference
until you ran millions of times in a loop.
 
Mattias,

Other then helping to shorten and simply code, do you know if there
are any (i guess not) performance advantages (i.e will it compile into
shorter IL), or other nice features with it ?

//Andreas
 
It would probably look something like this:

public bool IsNullOrEmpty(string str)
{
if ( str == null || str == string.Empty )
return true;
return false;
}

--
William Stacey, MVP

Andreas Håkansson said:
Mattias,

Other then helping to shorten and simply code, do you know if there
are any (i guess not) performance advantages (i.e will it compile into
shorter IL), or other nice features with it ?

//Andreas
 
William,

Or even shorter

public bool IsNullOrEmpty(string str)
{
return ( str == null || str == string.Empty )
}

//Andreas

William Stacey said:
It would probably look something like this:

public bool IsNullOrEmpty(string str)
{
if ( str == null || str == string.Empty )
return true;
return false;
}
 
yes. I think the IL would turn out to be the same. You could also create a
static method in your helper library and kill all the birds like:

public static bool IsNullOrEmptyTrim(string str)
{
return ( str == null || str.Trim() == string.Empty );
}

Cheers!
 
Or the shortest?

public static bool MyNullOrEmpty(string value)
{
return ((value == null) || (value.length== 0));
}

Willy.
 
Willy,

I've never actually went down the IL road (I really should), do you mean
shortest as in "you wont have to create an class instance" or shorter as in
"the shortest possible IL" ?

//Andreas
 
Or shorter:

public bool IsNullOrEmpty(string str)
{
return ( ("" + str).Length > 0)
}

Erik
 
Sure, if you don't count the ...58 IL bytes in the Concat method

call string [mscorlib]System.String::Concat(string, string)

Willy.
 
The (IMO) shortest IL codepath generated by the C# compiler (with optimize
option on).

Here it is:

..method public hidebysig static bool MyNullOrEmpty(string 'value') cil
managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_000d
IL_0003: ldarg.0
IL_0004: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0009: ldc.i4.0
IL_000a: ceq
IL_000c: ret
IL_000d: ldc.i4.1
IL_000e: ret
} // end of method Tester::MyNullOrEmpty

Willy.
 
Willy,

Is there any nice way to add the ilasm.exe as a tool to vs.net so
I wont have to command line it each time? Maybe then I'd go the IL
way more often to check things out =)

//Andreas

Willy Denoyette said:
The (IMO) shortest IL codepath generated by the C# compiler (with optimize
option on).

Here it is:

.method public hidebysig static bool MyNullOrEmpty(string 'value') cil
managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_000d
IL_0003: ldarg.0
IL_0004: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0009: ldc.i4.0
IL_000a: ceq
IL_000c: ret
IL_000d: ldc.i4.1
IL_000e: ret
} // end of method Tester::MyNullOrEmpty

Willy.

Andreas Håkansson said:
Willy,

I've never actually went down the IL road (I really should), do you
mean
shortest as in "you wont have to create an class instance" or shorter as
in
"the shortest possible IL" ?

//Andreas

class
for
 
Opps, make that Ildasm.exe =)

Andreas Håkansson said:
Willy,

Is there any nice way to add the ilasm.exe as a tool to vs.net so
I wont have to command line it each time? Maybe then I'd go the IL
way more often to check things out =)

//Andreas

Willy Denoyette said:
The (IMO) shortest IL codepath generated by the C# compiler (with optimize
option on).

Here it is:

.method public hidebysig static bool MyNullOrEmpty(string 'value') cil
managed
{
// Code size 15 (0xf)
.maxstack 8
IL_0000: ldarg.0
IL_0001: brfalse.s IL_000d
IL_0003: ldarg.0
IL_0004: callvirt instance int32 [mscorlib]System.String::get_Length()
IL_0009: ldc.i4.0
IL_000a: ceq
IL_000c: ret
IL_000d: ldc.i4.1
IL_000e: ret
} // end of method Tester::MyNullOrEmpty

Willy.

Andreas Håkansson said:
Willy,

I've never actually went down the IL road (I really should), do you
mean
shortest as in "you wont have to create an class instance" or shorter as
in
"the shortest possible IL" ?

//Andreas

"Willy Denoyette [MVP]" <[email protected]> skrev i meddelandet
Or the shortest?

public static bool MyNullOrEmpty(string value)
{
return ((value == null) || (value.length== 0));
}

Willy.

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
William,

Or even shorter

public bool IsNullOrEmpty(string str)
{
return ( str == null || str == string.Empty )
}

//Andreas

"William Stacey [MVP]" <[email protected]> skrev i meddelandet
It would probably look something like this:

public bool IsNullOrEmpty(string str)
{
if ( str == null || str == string.Empty )
return true;
return false;
}

--
William Stacey, MVP

"Andreas Håkansson" <andy.h (at) telia.com> wrote in message
Mattias,

Other then helping to shorten and simply code, do you know if
there
are any (i guess not) performance advantages (i.e will it compile
into
shorter IL), or other nice features with it ?

//Andreas

"Mattias Sjögren" <[email protected]> skrev i
meddelandet

or is there another way to perform the null and
empty test at the same time?

In Whidbey they're adding a new static method on the String class
for
this, called IsNullOrEmpty. So you'll be able to write

if ( !String.IsNullOrEmpty( str ) )



Mattias
 
Back
Top