Searching and replacing text

C++:
Searching and replacing text

How to:

C++ offers several ways to search and replace text. Below is an example using std::string::find and std::string::replace.

#include <iostream>
#include <string>

int main() {
    std::string myText = "The quick brown fox jumps over the lazy dog.";
    std::string wordToSearch = "lazy";
    std::string replacement = "energetic";

    size_t pos = myText.find(wordToSearch);
    
    if (pos != std::string::npos) {
        myText.replace(pos, wordToSearch.length(), replacement);
    }

    std::cout << myText << std::endl; // Output: The quick brown fox jumps over the energetic dog.
    return 0;
}

Deep Dive

The find and replace functions have been part of C++’s std::string class for ages, making them a basic yet powerful means to manipulate text. Before std::string, C programmers used character arrays and functions like strstr and strcpy from the C Standard Library for similar tasks, which was more error-prone and required manual memory management.

As for alternatives, other standard library components like std::regex provide pattern-based text manipulation capabilities for complex searching and replacing scenarios. Third-party libraries like Boost offer even more sophisticated text processing options.

Under the hood, searching and replacing involves algorithms that iterate over a string to find matching sequences of characters and then modify the string’s content accordingly. The efficiency of these operations may vary depending on the implementation and the complexity of the search pattern.

See Also