C++: Empty String

Bjarne-stroustrup
 

Languages may have features for dealing specifically with empty strings (those containing no characters).

The task is to:

  • Demonstrate how to assign an empty string to a variable.
  • Demonstrate how to check that a string is empty.
  • Demonstrate how to check that a string is not empty.

#include <string>

// ...

std::string str; // a string object for an empty string

if (str.empty()) { ... } // to test if string is empty

// we could also use the following
if (str.length() == 0) { ... }
if (str == "") { ... }

SOURCE

Content is available under GNU Free Documentation License 1.2.

Subscribe to TFE Times

Enter your email address to become a member of TFE Times today!