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

How Do I Add Characters to Strings in C++ The Right Way?

How can I add a char to a string? How can I remove char from a string? How can I use push_back() method in strings? Can I use push_back() and pop_back() methods in std::string as same as in std::wstring?

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,

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.

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,

Adding a character by using += operator

Here is full example that adds ‘X’ and ‘Y’ characters to a string,

and the string outputs will be,

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;

and the output will be as follows,

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:

We can add characters with push_back() method by giving the character number as a parameter. See example below,

and the output will be same as before,

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:

In C++, we can add characters by using insert() method by giving iterator i and the character number c. See example below,

and the output will be same as before,

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.

and the output will be

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.

Let’s see how we can use single character strings to add a string.

this is not wrong but string forms are slower than using character forms, and the output will be

We hope you like different ways of adding strings. Note that these same methods can be used with std::wstrings too.

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.


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

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++ComponentsDatabaseIntroduction to C++Language FeatureLearn C++

Get To Know The Powerful C++ Data Bindings In Windows Apps

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

How to Create a New Windows FMX Static Library In C++

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

How To Use C++ front() And back() Methods Of Vectors

C++Generic ProgrammingIntroduction to C++Language FeatureLearn C++

5 Things You Need To Know About Optimization In C++ Builder

ru_RURussian