Ad space (banner)
Lesson 15 / 20

String Manipulation (string)

This lesson covers basic string manipulation using std::string. It's for anyone searching "C++ string manipulation."

std::string comes with handy features โ€” length() to get the length, substr() to extract a substring, and the + operator to concatenate strings.

The example checks the character count with message.length(), and extracts the first 5 characters with message.substr(0, 5).

A common early mistake is how character counting works with multi-byte characters. std::string's length() returns a byte count, so a single non-ASCII character can be counted as multiple bytes.

In real projects, string manipulation โ€” validating user input, formatting a log line, and more โ€” is essential to nearly every program.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run C++ directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)