Cpp - String String Erasing

Introduction

You can use the erase() method to delete a number of characters from a string.

The starting position is supplied as the first argument and the number of characters to be erased is the second argument.

      
string s("this is a test"); 
s.erase(4,6);

This statement deletes 7 characters from string s starting at position 4.

The erase() method can be called without specifying a length and will then delete all the characters in the string up to the end of the string.

      
string s("this is a test"); 
s.erase(6);   

You can call erase() without any arguments to delete all the characters in a string.

Demo

#include <iostream> 
#include <string> 
using namespace std;

int main()//from   ww w .  jav a2s . c  o m
{
  string s("this is a test");
  s.erase(4, 6);

  cout << s << endl;

  s = "this is a test";
  s.erase(6);

  cout << s << endl;
  return 0;
}

Result

Exercise