example2_string_modification.cpp

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>

int main() {
    std::string str = "Hello World";
    
    // Append
    str.append("!");
    
    // Insert
    str.insert(5, " Beautiful");
    
    // Replace
    str.replace(0, 5, "Greetings");
    
    // Erase
    str.erase(str.find("World"), 5);
    
    std::cout << str << std::endl;
    
    return 0;
}
Back to string