Which is more efficient for the compiler and the best practice for checking whether a string is blank?
- Checking whether the length of the string == 0
- Checking whether the string is empty (strVar == "")
Also, does the answer depend on language?
Which is more efficient for the compiler and the best practice for checking whether a string is blank?
Also, does the answer depend on language? |
||||
|
Yes, it depends on language, since string storage differs between languages.
Etc. |
|||||
|
In languages that use C-style (null-terminated) strings, comparing to 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. |
|||||
|
In .Net:
strings can be null, so .Length sometimes throws a NullReferenceException |
|||
|
Actually, it may be better to check if the first char in the string is '\0':
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. |
||||
|
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. |
||||
|
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. I event tested it (I also tested with C#, same result):
Result:
Which means comparison takes way more than string length check. |
|||||||||||||
|
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) |
|||||||||||||
|
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. |
|||
|
@DerekPark: That's not always true. "" is a string literal so, in Java, it will almost certainly already be interned. |
|||
|
For C strings,
will be faster than either
or
because you will avoid the overhead of a function call. |
|||
|
@Nathan
I almost mentioned that, but ended up leaving it out, since calling Honestly, I always use |
|||
|
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
or comparable. Now there's no doubt at is you're checking. |
|||
|
After I read this thread, I conducted a little experiment, which yielded two distinct, and interesting, findings. Consider the following.
The above is copied from the locals window of the Visual Studio debugger. The same value is used in all three of the following examples. if ( strInstallString == "" ) === if ( strInstallString == string.Empty ) Following is the code displayed in the disassembly window of the Visual Studio 2013 debugger for these two fundamentally identical cases.
if ( strInstallString == string.Empty ) Isn't Significantly Different
From the above machine code listings, generated by the NGEN module of the .NET Framework, version 4.5, I draw the following conclusions.
Conclusion As a matter of principle, I avoid comparing against the empty string as a literal, because the empty string literal can appear ambiguous in source code. To that end, my .NET helper classes have long defined the empty string as a constant. Though I use string.Empty for direct, inline comparisons, the constant earns its keep for defining other constants whose value is the empty string, because a constant cannot be assigned string.Empty as its value. This exercise settles, once and for all, any concern I might have about the cost, if any, of comparing against either string.Empty or the constant defined by my helper classes. However, it also raises a puzzling question to replace it; why is comparing against string.Empty more efficient than testing the length of the string? Or is the test used by Shinny invalidated because by the way the loop is implemented? (I find that hard to believe, but, then again, I've been fooled before, as I'm sure you have, too!) I have long assumed that system.string objects were counted strings, fundamentally similar to the long established Basic String (BSTR) that we have long known from COM. |
|||
|