C++20 sections · 1024 units
Open in Course

Empty Strings

Strings with no content

Create an empty string with string s; or string s = "";. Both produce zero characters. Empty strings are useful for building text incrementally or as initial values. Check if empty with s.empty().

Returns true for zero-length, false otherwise. Cleaner than s.length() == 0 and communicates intent better. Use it in conditions and loops. Empty doesn't mean null. An empty string is a valid object.

You can call methods on empty strings without crashes. This differs from null pointers in other contexts.