erase off end string c++ code example

Example 1: string erase character c++

#include <iostream>
#include <algorithm>
#include <string>
 
int main()
{
    std::string s = "This is an example";
    std::cout << s << '\n';
 
    s.erase(0, 5); // Erase "This "
    std::cout << s << '\n';
 
    s.erase(std::find(s.begin(), s.end(), ' ')); // Erase ' '
    std::cout << s << '\n';
 
    s.erase(s.find(' ')); // Trim from ' ' to the end of the string
    std::cout << s << '\n';
}

Example 2: pop off end of string c++

#include <string>

int main() {
 std::string str = "Hello, World!";
 str.pop_back(); // str is now "Hello, World"
}

Tags:

Cpp Example