Checking for string contents? string Length Vs Empty String

前端 未结 13 907
野趣味
野趣味 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: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.

提交回复
热议问题