Checking for string contents? string Length Vs Empty String

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.


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

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

Etc.