TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C++ String erase() function

C++ String erase() function tutorial for beginners and professionals with examples on constructor, if-else, switch, break, continue, comments, arrays, object and class, exception, static, structs, inheritance, aggregation etc.

<< Back to CPP

C++ String erase()

This function removes the characters as specified, reducing its length by one.

Syntax

Consider a string str. Syntax would be:

str.erase(pos,len);
str.erase(itr);
str.erase(first,last);

Parameter

  • pos :It defines the position of the character which is to be removed.
  • len :It defines the number of characters to be erased.
  • Itr : It is an iterator to the character to be removed.
  • Range(first,last): It defines the range within the string to be removed.

Return value

It returns *this.

Example 1

Let's see a simple example when pos and len are given:

#include<iostream>
using namespace std;
int main()
{
	string str="This is a java tutorial";
	str.erase(8,1);
	cout<<str;
	return 0;
}

Output:

This is java tutorial

Example 2

Let's see a simple example when iterator is passed in a parameter:

#include<iostream>
using namespace std;
int main()
{
	string str="java programming";
	str.erase(str.begin()+11);
	cout<<str;
	return 0;
}

Output:

java programing

Example 3

Let's see a simple example when the range is mentioned in a parameter:

#include<iostream>
using namespace std;
int main()
{
	string str="This is an example of C and C++";
	str.erase(str.begin()+24,str.end());
	cout<<str;
	return 0;
}

Output:

This is an example of C

Next TopicC++ Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf