Manipulating C++ Strings with find and erase Methods

Locating substrings within a std::string object is efficiently handled by the find method. This function searches for the first occurrence of a specified character sequence and returns its starting index. If the target cannot be found, the method returns std::string::npos, which typically represents the maximum value for size_t and effectively acts as an error flag.

The following code demonstrates how to locate a substring within a text source:

#include <iostream>
#include <string>

int main() {
    std::string text = "framework_example";
    std::string target = "example";
    
    // Search for the substring
    size_t location = text.find(target);
    
    if (location != std::string::npos) {
        std::cout << "Found at index: " << location << std::endl;
    } else {
        std::cout << "Substring not found" << std::endl;
    }
    
    return 0;
}

In this example, location will hold the value 10, as "example" begins at that zero-based index. The find method is strictly limited to returning the position of the very first match.

Removing Characters with erase

The erase member function provides the ability to remove a sequence of characters from a string. It requires the starting index for deletion and the total count of characters to remove. This mechanism is useful for truncating strings or removing specific segments.

Consider the following implementation which removes a specific portion of a string:

#include <iostream>
#include <string>

int main() {
    std::string buffer = "data_processing";
    
    // Remove 10 characters starting from index 4
    buffer.erase(4, 10);
    
    std::cout << buffer << std::endl;
    return 0;
}

The output will be "data". The function operates by shifting the remaining characters to fill the gap left by the erased segment. Remember that string indices start at 0; therefore, calling buffer.erase(0, 1) on the string "buffer" would remove the first character, resulting in "uffer".

Dynamic String Modification

Combining these two methods allows for dynamic string manipulation, such as removing a specific substring regardless of its position. By first locating the target and then calculating the length of the segment to delete, a string can be modified precisely.

The example below illustrates removing a specific suffix from a source string:

#include <iostream>
#include <string>

int main() {
    std::string source = "user@domain.com";
    std::string to_remove = "@domain.com";
    
    // Find the starting position of the suffix
    size_t start_index = source.find(to_remove);
    
    // Check if the suffix exists before erasing
    if (start_index != std::string::npos) {
        source.erase(start_index, to_remove.length());
        std::cout << "Modified string: " << source << std::endl;
    }
    
    return 0;
}

This logic identifies the index where "@domain.com" begins and removes that specific segment, leaving the output as "user".

Tags: C++ String Manipulation STD programming

Posted on Thu, 07 May 2026 14:44:07 +0000 by fizix