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

What You Need To Append One String To Another In A C++ App

What You Need To Append One String To Another In A C++ App

How can I use the append() method of std::string? How can I append a string to another string in a C++ app? Can we append a char array to a string? How can I append a number of chars to a string? Is it possible to add a substring of a string to another string? Here are the answers with C++ examples.

A little word about C++ strings

Generally, as an introduction to C++, in addition to int, float, double there is another data type, string that we use for alphanumeric variables. In C++ there are several typedefs for common character types are provided: String types 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 another terms string stores for the alphanumeric text with 1 byte chars, called ASCII chars. Strings are the instantiation of the basic_string class template that uses char as the character type. Simply we can define a string as below,

String has methods to assign, copy, align, replace or to operate with other strings. These methods can be used in all string methods with their appropriate syntax. We can append a string to another string in different methods.

How to append a string to another string in C++ with the += Operator

We can use ‘+=’ operator to append strings as given example below,

Did you know you can append a string to another string with the C++ append() method?

The append() method of std::string appends additional characters to the string. Here is the syntax of append() method.

As given in this syntax, we can append a string to another string with append() method as in example below,

This is how to append a Char Array to a string in a C++ app

We can append a char array to a string with append() method as given above. Here is the syntax of append() method.

We can add char array to a string and then we can append a new string as in example below,

In C++ how can I append a number of repeated characters to a string?

The append() method appends count copies of character to a string as given syntax below (until C++20).

Here is an example that appends charcters in given number to a string, see example below,

How to append a substring to a string in C++

The append() method of std::string can be used to append a substring (with pos, pos+count) to a string. If the requested substring lasts past the end of the string, or if count == npos, the appended substring is [pos, size()). If pos > str.size(), std::out_of_range is thrown. (since C++14, until C++20). Here is a Syntax below,

Here is a example to append a substring of a string to a string,

There are many other ways to use append methods(). Note that we can also use these append() methods to append wstring in C++.

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