C++C++11C++14C++17Introduction to C++Learn C++

How To Erase/Delete Part Of A String In C++ Software

How To EraseDelete Part Of A String In C++ Software

How can I use erase() method of std::string? How can I erase part of a string in my C++ software? Here are the answers with C++ examples.

Generally, as an introduction to C++, in addition to int, float, double there is another data type called string that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types are defined in header <string>.

Strings are the string class for byte characters represented with string and alphanumeric characters are stored and displayed in string forms. In another terms string stores for the alphanumeric text with 1 byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. Simply we can define a string as below,

String has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can erase part of a string by using erase() method. Let’s see this method now.

How to remove part of a string with the C++ erase() method

erase() method of std::string in C++ removes string characters from the string in a given range. We can directly give the start position in a string to erase all behind as given Syntax below

or we can erase inside of a string by using start position to delete and number of characters. Thus, erase() method will delete all characters in this given Syntax below.

Here is an erase() example that uses both erase() methods

and the output will be,

As you see in the first part we delete all after the 4th character and in the second part we delete 13 characters after 7th character,

Why do we get an “out of range” error when trying to use the C++ Erase() method?

As you can see in the example above, we can directly copy, assign, replace or insert any string after its some part being erased. When we use erase() method, we must be sure that the first parameter is lower than the string size (str.size()) and the second parameter is lower than size()-first_parameter, which means both should be inside the string. If the second parameter exceeds the string size, it returns remaining string till the position without throwing an exception. If the first parameter exceed the limits of string (size of string) then this erase method will throw out_of_range, we can catch this exception as given example below,

In this example above, we try to erase 2 characters of this given string starting from 64th character which means that we exceed the limits of this string size, and the output will be,

As you see it is very easy to erase any part of a string, and this erase() method can be used with wstrings as given in the examples above.

close

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17Generic ProgrammingLearn C++Templates

What Is A Template In C++?

C++C++17Code SnippetComponentsIteratorsLearn C++

What Are The Boost C++ Libraries?

C++C++17Introduction to C++Learn C++Syntax

Learn Bits and How To Use std::bitset In C++

C++C++17IteratorsLearn C++

How To Use Insert In std::map In Modern C++