Search code examples
stringoptimizationlanguage-agnostic

Checking for string contents? string Length Vs Empty String


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
  2. Checking whether the string is empty (strVar == "")

Also, does the answer depend on language?


Solution

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

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

    Etc.