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

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

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.

In this post, you’ll learn how to correctly remove a character from a string. How do I use the pop_back() method in strings? Can I remove characters by using the erase() and resize() methods in std::string? By learning how to remove a character from string, using pop_back method, erase & resize methods in string to remove characters, it will help you build C++ applications with the use of C++ software.

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

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.


Reduce development time and get to market faster with RAD Studio, Delphi, or C++Builder.
Design. Code. Compile. Deploy.
Start Free Trial

Free C++Builder Community Edition

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

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

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

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

How To Learn The Move Constructors In Modern C++?