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

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.

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 was born in 1974, Eskisehir-Turkey, started coding in college and graduated from the department of Mechanical Engineering of Eskisehir Osmangazi University in 1997. He worked as a research assistant at the same university for more than 10 years. He received his MSc and PhD degrees from the same department at the same university. Since 2012, he is the founder and CEO of Esenja LLC Company. He has married and he is a father of a son. Some of his interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Learn C++Syntax

What Is A Forced (Default) Copy Assignment Operator In Modern C++

C++C++11C++14C++17C++20Learn C++Syntax

What is Implicitly-declared Copy Assignment Operator In C++?

C++C++11C++14C++17C++20Learn C++Syntax

What is Avoiding Implicit Copy Assignment In C++?

C++C++11C++14C++17C++20Learn C++

Typical Declaration Of A Copy Assignment Operator Without std::swap