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

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

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.

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.

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.

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

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++?