views:

2830

answers:

12

Which is more efficient for the compiler and the best practice for checking whether a string is blank?

  1. Checking whether the length of the string == 0
  2. Checking whether the string is empty (strVar == "")

Also, does the answer depend on language?

+12  A: 

Yes, it depends on language, since string storage differs between languages.

  • Pascal-type strings: Length = 0.
  • C-style strings: [0] == 0.
  • .NET: .IsNullOrEmpty.

Etc.

Stu
I think the OP was asking about blank strings validation, not nullity, so when you already know that the string is not null, using IsNullOrEmpty is just another unnecessary check. So the OP's question is what takes more performance, myString.Length > 0 or myString != "". Read http://stackoverflow.com/questions/10230/checking-for-string-contents-string-length-vs-empty-string/2306659#2306659
Shimmy
+2  A: 

In .Net:

string.IsNullOrEmpty( nystr );

strings can be null, so .Length sometimes throws a NullReferenceException

Keith
A: 

Actually, IMO the best way to determine is the IsNullOrEmpty() method of the string class.

http://msdn.microsoft.com/en-us/library/system.string.isnullorempty.

Update: I assumed .Net, in other languages, this might be different.

Vaibhav
+7  A: 

In languages that use C-style (null-terminated) strings, comparing to "" will be faster. That's an O(1) operation, while taking the length of a C-style string is O(n).

In languages that store length as part of the string object (C#, Java, ...) checking the length is also O(1). In this case, directly checking the length is faster, because it avoids the overhead of constructing the new empty string.

Derek Park
+1  A: 

In Java 1.6, the String class has a new method isEmpty

There is also the Jakarta commons library, which has the isBlank method. Blank is defined as a string that contains only whitespace.

bpapa
A: 

In this case, directly checking the length is faster, because it avoids the overhead of constructing the new empty string.

@DerekPark: That's not always true. "" is a string literal so, in Java, it will almost certainly already be interned.

GaryF
A: 

For C strings,

if (s[0] == 0)

will be faster than either

if (strlen(s) == 0)

or

if (strcmp(s, "") == 0)

because you will avoid the overhead of a function call.

Mark Harrison
+1  A: 

In languages that use C-style (null-terminated) strings, comparing to "" will be faster

Actually, it may be better to check if the first char in the string is '\0':

char *mystring;
/* do something with the string */
if ((mystring != NULL) && (mystring[0] == '\0')) {
    /* the string is empty */
}

In Perl there's a third option, that the string is undefined. This is a bit different from a NULL pointer in C, if only because you don't get a segmentation fault for accessing an undefined string.

Nathan Fellman
A: 

@Nathan

Actually, it may be better to check if the first char in the string is '\0':

I almost mentioned that, but ended up leaving it out, since calling strcmp() with the empty string and directly checking the first character in the string are both O(1). You basically just pay for an extra function call, which is pretty cheap. If you really need the absolute best speed, though, definitely go with a direct first-char-to-0 comparison.

Honestly, I always use strlen() == 0, because I have never written a program where this was actually a measurable performance issue, and I think that's the most readable way to express the check.

Derek Park
+1  A: 

String.IsNullOrEmpty() only works on .net 2.0 and above, for .net 1/1.1, I tend to use:

if (inputString == null || inputString == String.Empty)
{
// String is null or empty, do something clever here. Or just expload.
}

I use String.Empty as opposed to "" because "" will create an object, whereas String.Empty wont - I know its something small and trivial, but id still rather not create objects when I dont need them! (Source)

pzycoman
I would be really surprised if "" actually results in an instantiation inside of the C# compiler.
jsight
Use 'inputString.Length == 0', rather than 'inputString == String.Empty' for better performance
Matt Lacey
Id argue that inputString == String.Empty is easier to read than .Length == 0...
pzycoman
A: 

Again, without knowing the language, it's impossible to tell.

However, I recommend that you choose the technique that makes the most sense to the maintenance programmer that follows and will have to maintain your work.

I'd recommend writing a function that explicitly does what you want, such as

#define IS_EMPTY(s) ((s)[0]==0)

or comparable. Now there's no doubt at is you're checking.

Andy Lester
A: 

Assuming your question is .NET:

If you want to validate your string against nullity as well use IsNullOrEmpty, if you know already that your string is not null, for example when checking TextBox.Text etc., do not use IsNullOrEmpty, and then comes in your question.
So for my opinion String.Length is less perfomance than string comparison.

I event tested it (I also tested with C#, same result):

Module Module1
  Sub Main()
    Dim myString = ""


    Dim a, b, c, d As Long

    Console.WriteLine("Way 1...")

    a = Now.Ticks
    For index = 0 To 10000000
      Dim isEmpty = myString = ""
    Next
    b = Now.Ticks

    Console.WriteLine("Way 2...")

    c = Now.Ticks
    For index = 0 To 10000000
      Dim isEmpty = myString.Length = 0
    Next
    d = Now.Ticks

    Dim way1 = b - a, way2 = d - c

    Console.WriteLine("way 1 took {0} ticks", way1)
    Console.WriteLine("way 2 took {0} ticks", way2)
    Console.WriteLine("way 1 took {0} ticks more than way 2", way1 - way2)
    Console.Read()
  End Sub
End Module

Result:

Way 1...
Way 2...
way 1 took 624001 ticks
way 2 took 468001 ticks
way 1 took 156000 ticks more than way 2

Which means comparison takes way more than string length check.

Shimmy