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,
1 2 3 |
std::string str = "This is a String"; |
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.
Table of Contents
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:
1 2 3 |
void pop_back(); |
We can remove the last character with pop_back() method without using any parameter. Here is the example below,
1 2 3 4 |
std::string str = "This is a String"; str.pop_back(); |
This deletes the last character of the string str. See full example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::string s1 = "This is a String"; std::cout << s1 << std::endl; s1.pop_back(); // delete the last character std::cout << s1 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
This is a String This is a Strin |
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:
1 2 3 |
basic_string& erase (index i); |
We can remove the last character with erease() as in the example below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::string s1 = "This is a String"; std::cout << s1 << std::endl; s1.erase( s1.length()-1 ); std::cout << s1 << std::endl; getchar(); return 0; } |
and the outputs will be,
1 2 3 4 |
This is a String This is a Strin |
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 .
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::string s1 = "This_is_a_String"; std::cout << s1 << std::endl; s1.erase(4); std::cout << s1 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
This_is_a_String This |
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,
1 2 3 |
basic_string& erase (size_type pos = 0, size_type len = npos); |
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:
1 2 3 |
basic_string& resize (size_type n); |
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,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
#include <iostream> #include <string> int main() { std::string s1 = "This_is_a_String"; std::cout << s1 << std::endl; s1.resize( s1.length()-6 ); //set new size of string std::cout << s1 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
This_is_a_String This_is_a_ |
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
Design. Code. Compile. Deploy.
Start Free Trial
Free C++Builder Community Edition