In C++, the std::string class is a utility of the standard library that represents and manages strings of characters.
Unlike traditional C character arrays, std::string offers a series of methods and operators that facilitate text manipulation (such as concatenation, searching, and modifying substrings).
Additionally, std::string automatically manages memory, avoiding many of the problems associated with manual memory management.
Whenever possible, we should prefer std::string to a null-terminated string.
Declaration
The basic declaration of a string is:
#include <string>
std::string string_name;For example, something like this,
#include <string>
std::string greeting; // Empty string
std::string name = "Luis"; // Initialization with a literal stringInitialization
We can initialize the value of the std::string in different ways.
std::string greeting = "Hello, world";std::string name(5, 'a'); // Initializes with 5 'a' characters, i.e., "aaaaa"Basic operations
We can concatenate strings using the + operator
std::string name = "Luis";
std::string last_name = "Pérez";
std::string full_name = name + " " + last_name; // "Luis Pérez"Or even the += operator
std::string greeting = "Hello";
greeting += ", world"; // "Hello, world"We can access a specific character using the [] operator
char first_character = greeting[0]; // 'H'Or using the at method, which provides safe access (throws an exception if the index is out of bounds).
char first_character = greeting.at(0); // 'H'We can modify strings using both the [] operator and the at method
greeting[0] = 'h'; // "hello, world"
greeting.at(0) = 'H'; // "Hello, world"We can get the length using length or size
std::size_t length = greeting.length(); // 10To compare strings, we can use relational operators
if (name == "Luis") {
std::cout << "The name is Luis" << std::endl;
}Or the compare method
if (name.compare("Luis") == 0) {
std::cout << "The name is Luis" << std::endl;
}We can search for text within a string using find
std::size_t position = greeting.find("world"); // Returns 6We can obtain a substring using the substr method
std::string world = greeting.substr(6, 5); // "world"Methods and functions
append
Adds a string to the end of the current string.
greeting.append("!!"); // "Hello, world!!"insert
Inserts a string at a specific position.
greeting.insert(5, "dear "); // "Hello, dear world"erase
Removes a part of the string.
greeting.erase(5, 7); // "Hello, world"replace
Replaces a part of the string with another.
greeting.replace(5, 5, "dear "); // "Hello, dear world"c_str
Returns a pointer to a C-style string (null-terminated).
const char* c_greeting = greeting.c_str();empty
Checks if the string is empty.
if (greeting.empty()) {
std::cout << "The string is empty" << std::endl;
}clear
Clears the string, making it empty.
greeting.clear();Practical example
Let’s see a somewhat more complete example to illustrate the use of std::string
#include <iostream>
#include <string>
int main() {
std::string name = "Luis";
std::string last_name = "Pérez";
std::string full_name = name + " " + last_name;
std::cout << "Full name: " << full_name << std::endl;
// Modify the name
full_name.replace(0, 4, "Carlos");
std::cout << "Modified name: " << full_name << std::endl;
// Search and remove last name
std::size_t position = full_name.find("Pérez");
if (position != std::string::npos) {
full_name.erase(position);
}
std::cout << "Without last name: " << full_name << std::endl;
return 0;
}In this example, we see how to declare, initialize, concatenate, modify, search, and remove parts of a std::string.