Language: EN

cpp-como-usar-std-string

How to Use std::string in C++

In C++, the std::string class is a utility from the standard library that represents and manages character strings.

Unlike traditional C character arrays, std::string offers a series of methods and operators that facilitate text string manipulation (such as concatenation, searching, and substring modification).

Additionally, std::string automatically manages memory, avoiding many of the problems associated with manual memory management.

Whenever possible, we should prefer std::string over a null-terminated string.

Declaration

The basic declaration of a text string is:

#include <string>

std::string string_name;

For example, something like this:

#include <string>

std::string greeting;  // Empty string
std::string name = "Juan";  // Initialization with a literal string

Initialization

We can initialize the value of 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 = "Juan";
std::string last_name = "Pérez";
std::string full_name = name + " " + last_name;  // "Juan 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();  // 10

To compare strings, we can use relational operators.

if (name == "Juan") {
    std::cout << "The name is Juan" << std::endl;
}

Or the compare method.

if (name.compare("Juan") == 0) {
    std::cout << "The name is Juan" << std::endl;
}

We can search for text within a string using find.

std::size_t position = greeting.find("world");  // Returns 6

We can obtain a substring using the substr method.

std::string world = greeting.substr(6, 5);  // "world"

Methods and functions

Practical example

Let’s look at a slightly more complete example to illustrate the use of std::string.

#include <iostream>
#include <string>

int main() {
    std::string name = "Juan";
    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.