Which is more efficient for the compiler and the best practice for checking whether a string is blank?
Yes, it depends on language, since string storage differs between languages.
Length = 0
.[0] == 0
. .IsNullOrEmpty
.Etc.
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
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.
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.
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.
In Java 1.6, the String class has a new method [isEmpty] 1
There is also the Jakarta commons library, which has the [isBlank] 2 method. Blank is defined as a string that contains only whitespace.