Modern C++ uses Strings, Wide Strings, and Unicode Strings to support worldwide languages. Strings (std::string) use 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++, we can simply define a string as below.
1 2 3 |
std::string str = "This is a String"; |
a 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 push_back() and insert() methods to add chars to a string. Let’s see how we add and remove characters to strings.
In this post, you’ll learn how to add a char to a string, and how do I remove a character from a string? How do I use the push-back() method in strings? Can I use the push-back() and pop-back() methods in std::string the same way I do in std::wstring? By learning how to add a char to a string and using push-back methods in string and pop-back methods in std: string, it will help you build C++ applications with the use of C++ software.
Table of Contents
How do we add a character to a string in C++
Strings are arrays of characters, and we can add characters. In C++, while string contents are defined between ” and ” chars are defined between ‘ and ‘. So we can add a character by typing it between the ‘ and the ‘. For example we can define a string and we can add a ‘!’ character as below,
1 2 3 4 |
std::string str = "This is a String"; str += '!'; |
Adding a character by using += operator
Here is full example that adds ‘X’ and ‘Y’ characters to a string,
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 ="Characters Added to String:"; s1 += 'X'; std::cout << s1 << std::endl; s1 += 'Y'; std::cout << s1 << std::endl; getchar(); return 0; } |
and the string outputs will be,
1 2 3 4 |
Characters Added to String:X Characters Added to String:XY |
We can add characters in their ASCII Character numbers. For example upper case letters starts from 65, Lets try to add some chars with a for loop as 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 ="Characters Added to String:"; for(int i=65; i<=75; i++) { s1 += (char)(i); std::cout << s1 << std::endl; } getchar(); return 0; } |
and the output will be as follows,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Characters Added to String:A Characters Added to String:AB Characters Added to String:ABC Characters Added to String:ABCD Characters Added to String:ABCDE Characters Added to String:ABCDEF Characters Added to String:ABCDEFG Characters Added to String:ABCDEFGH Characters Added to String:ABCDEFGHI Characters Added to String:ABCDEFGHIJ Characters Added to String:ABCDEFGHIJK |
Adding a character by using push_back() method of strings
push_back() method is a String Method that appends a character to the end of the basic_string which means it is also increasing its length by one. Here is the general syntax of the push_back() method to string.
Syntax:
1 2 3 |
void push_back (charT c); |
We can add characters with push_back() method by giving the character number as a parameter. See 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 ="Characters Added to String:"; for(int i=65; i<=75; i++) { s1.push_back(i); std::cout << s1 << std::endl; } getchar(); return 0; } |
and the output will be same as before,
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Characters Added to String:A Characters Added to String:AB Characters Added to String:ABC Characters Added to String:ABCD Characters Added to String:ABCDE Characters Added to String:ABCDEF Characters Added to String:ABCDEFG Characters Added to String:ABCDEFGH Characters Added to String:ABCDEFGHI Characters Added to String:ABCDEFGHIJ Characters Added to String:ABCDEFGHIJK |
Adding a character by using insert() method of strings
insert() method of string is a String Method that inserts character or string to the basic_string which means it is also increasing its length. We can use begin() or end() iterators to add characters. Here is the general syntax of the insert() method to string.
Syntax:
1 2 3 |
iterator insert (iterator iter, charT c); |
In C++, we can add characters by using insert() method by giving iterator i and the character number c. See 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 =":Characters Added to String:"; s1.insert(s1.begin(), 'X'); std::cout << s1 << std::endl; s1.insert(s1.end(), 'Y'); std::cout << s1 << std::endl; getchar(); return 0; } |
and the output will be same as before,
1 2 3 4 |
X:Characters Added to String: X:Characters Added to String:Y |
Adding a character by using append() method of strings
append() method of string is a String Method that appends constant character arrays or string to the basic_string which means it is also increasing its length. Here is the general syntax of the iappend() method to string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
#include <iostream> #include <string> int main() { std::string s1 =":Characters Added to String:"; const char c1[]="X"; const char c2[]="Y"; s1.append(c1); std::cout << s1 << std::endl; s1.append(c2); std::cout << s1 << std::endl; std::cout << s1 << std::endl; getchar(); return 0; } |
and the output will be
1 2 3 4 |
Characters Added to String:X Characters Added to String:XY |
Adding a character by using single character strings
Note that you can also add characters by using single character strings like “X”, “Y”. We don’t recommend this, professionally not a good way. Because adding a string is slower than adding a char. Hence, try to use ‘ and ‘ instead of ” and “. For example those both lines prints out ‘/n’ character to the end which is about the same with std::endl. Here the first line is using string and the second one is using char which is the correct way and faster.
1 2 3 4 |
std::cout << "\n"; std::cout << '\n'; |
Let’s see how we can use single character strings to add a string.
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 ="Characters Added to String:"; s1 += "X"; std::cout << s1 << std::endl; s1 += "Y"; std::cout << s1 << std::endl; getchar(); return 0; } |
this is not wrong but string forms are slower than using character forms, and the output will be
1 2 3 4 |
Characters Added to String:X Characters Added to String:XY |
We hope you like different ways of adding strings. Note that these same methods can be used with std::wstrings too.