Checking for string contents? string Length Vs Empty String

前端 未结 13 894
野趣味
野趣味 2020-12-17 10:14

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
相关标签:
13条回答
  • 2020-12-17 10:26

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

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

    Etc.

    0 讨论(0)
  • 2020-12-17 10:29

    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.

    0 讨论(0)
  • 2020-12-17 10:29

    @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.

    0 讨论(0)
  • 2020-12-17 10:32

    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.

    0 讨论(0)
  • 2020-12-17 10:35

    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.

    0 讨论(0)
  • 2020-12-17 10:38

    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.

    0 讨论(0)
提交回复
热议问题