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

You Need To Know How To Correctly Remove Characters From C++ Strings

How can I correctly remove a character from a string? How can I use pop_back() method in strings? Can I use erase() and resize() methods in std::string to remove characters?

Modern C++ uses Strings, Wide Strings and Unicode Strings to support worldwide languages. Strings (std::string) uses char as the character type which means they are ASCII chars and they are an instantiation of the basic_string class template. In C++, there are several typedefs for common character types and one of them is std:string types that 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 other terms, string stores for the alphanumeric text with 1-byte chars, called char. Strings are the instantiation of the basic_string class template that uses char as the character type. In modern C++, simply we can define a string as below,

The string has methods to append, assign, copy, align, replace or operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can add characters with + or += operators ; we can use pop_back(), erase() and resize() methods to add chars to a string. Let’s see how we add and remove characters from strings.

How to Remove a Character from a String in Modern C++

Strings are arrays of characters, and we can add and remove characters. In C++, while string contents are defined between ” and “, characters are defined between ‘ and ‘. We can delete the last character by using the pop_back() method .

Using pop_back() method to Remove a Character from a String

The best and easy way to delete the last character of a string is to use the pop_back() method.

pop_back() method is a String Method that removes the last character of the basic_string which means it is also decreasing its length by one. Here is the general syntax of the pop_back() method to string.

Syntax:

We can remove the last character with pop_back() method without using any parameter. Here is the example below,

This deletes the last character of the string str. See full example below,

and the string outputs will be,

as you see we delete the last character of this string.

Using erase() method to Delete Characters from a String

erase() method is a String Method that deletes the last characters from the given index of the basic_string which means it is also decreasing its length by deleted number of characters. Here is the general syntax of the erase() method to string.

Syntax:

We can remove the last character with erease() as in the example below,

and the outputs will be,

as you see we delete the last character of this string by pointing the index s1.length()-1 . We can also delete more characters after the index given by using this erase() method .

and the string outputs will be,

You can also use this erase method to delete portion of string which means you can delete characters in a given range inside of a string. Here is the Syntax below,

Using resize() method to Delete Characters from a String

resize() method is a String Method that defines the new size of the basic_string which means it can be used to delete extra characters if they are not needed. . Here is the general syntax of the erase() method to string.

Syntax:

We can remove the last characters with resize(). For example we can delete the last 6 characters of a string as in the example below,

and the string outputs will be,

You can use all pop_back(), erase() and resize() methods of a std::string safely in your C++ applications. Note that you can use these methods on std::wstrings in your modern C++ applications

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

33+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux and some other operating systems. Dr. Yilmaz Yoru was born in 1974, Eskisehir-Turkey. He graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. One year later he started to work in the same university as an assistant. He received his MSc and PhD degrees from the same department of the same university. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics and Artificial Intelligence. He also likes the graphical 2D & 3D design and high-end innovations.
Related posts
C++Introduction to C++Language FeatureLearn C++

How To Generate A Random File Name on Windows

C++Introduction to C++Language FeatureLearn C++

How To Include Or Exclude Trailing Backslashes On Windows

C++Introduction to C++Language FeatureLearn C++

How To Check Characters of File Names and Paths on Windows

C++Introduction to C++Language FeatureLearn C++

How to use SameFileName method on Windows

en_USEnglish