Strings
The Old Way Was Painful
In C, a "string" was just an array of characters ending with a sneaky '\0' null terminator. Want to concatenate two strings? You'd better know their lengths, allocate enough memory, and call strcat() β hoping you didn't overrun the buffer. One mistake and you've got a security vulnerability.
std::string changed everything. It manages its own memory, knows its own length, and lets you do things like a + b to concatenate. It's what strings should have been from the start.
Creating Strings
There are many ways to create a string, and they all feel natural:
Creating & Concatenating Strings
Searching & Manipulating
Strings come loaded with methods for finding and modifying text. The key ones are find(), substr(), and replace(). Think of find() as a search bar β it returns the position of the first match, or string::npos if nothing is found.
Searching, Substring & Replace
Iterating Over Characters
Comparing Strings
c_str() β Talking to C Code
Sometimes you need to pass a std::string to a C library function that expects a const char*. That's what c_str() is for β it gives you a null-terminated C-string pointer to the underlying data.
string_view β The Zero-Copy Reader (C++17)
std::string_view is a lightweight, non-owning view into a string. It doesn't allocate memory or copy characters β it just points at an existing string's data. This makes it perfect for function parameters when you only need to read the string.
string_view β Read Without Copying
std::string_view for read-only string parameters β it avoids copying and works with both std::string and C-strings. Just be careful: a string_view doesn't own the data, so don't store it if the original string might be destroyed.