Demonstrate insert(), erase(), and replace(). : string replace « String « C++






Demonstrate insert(), erase(), and replace().

Demonstrate insert(), erase(), and replace().
  
#include <iostream>
#include <string>
using namespace std;
   
int main()
{
  string str1("A");
  string str2("B");
   
  cout << "Initial strings:\n";
  cout << "str1: " << str1 << endl;
  cout << "str2: " << str2 << "\n\n";
   
  str1.insert(6, str2);
  cout << str1 << "\n\n";
   
  str1.erase(6, 9);
  cout << str1 <<"\n\n";
   
  str1.replace(7, 8, str2);
  cout << str1 << endl;
   
  return 0;
}
  
    
  








Related examples in the same category

1.string.replace()
2.string.replace( position, 2, '12345;;123', 5, 2 )
3.Replace all spaces with period