String Append Function - C++ Programming Tutorial
C++ Course / Strings / String Append Function

String Append Function

BLUF: Mastering String Append Function is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: String Append Function

C++ is renowned for its efficiency. Learn how String Append Function enables low-level control and high-performance computing in the tutorial below.

C++ String append

This function is employed to elongate the string by adding content to the end of the existing value.

Syntax

Consider string str1 and str2. Syntax would be :

Example

Str1.append(str2);
Str1.append(str2,pos,len);
Str1.append(str2,n);

Parameters

str: A String object that needs to be concatenated with another String object.

It specifies the index of the initial character to add to a different object.

The variable len represents the number of characters that will be copied into another string object as a substring.

n : Number of characters to copy.

Return value

This function does not return any value.

Example 1

Let's explore an example of adding a string to another string object.

Example

#include<iostream>
using namespace std;
int main()
{
string str1="Welcome to C++ programming";
string str2="language";
cout<<"Before appending,string value is"<<str1<<'\n';
str1.append(str2);
cout<<"After appending, string value is"<<str1<<'\n';
return 0;
}

Output:

Output

Before appending,string value is Welcome to C++ programming
After appending,string value is Welcome to C++ programming language

Example 2

Let's explore an example of extending a string by specifying both the starting position and the length as parameters.

Example

#include<iostream>
using namespace std;
int main()
{
 string str1 = "Mango is my favourite" ;
string str2 ="fruit";
cout<<"Before appending, string value is :" <<str1<<'\n';
str1.append(str2,0,5);
cout<<"After appending, string value is :" <<str1<<'\n';
return 0;
}

Output:

Output

Before appending, string value is Mango is my favourite
After appending, string value is Mango is my favourite fruit

Example 3

Let's see another example.

Example

#include<iostream>
using namespace std;
int main()
{
 string str1 = "Kashmir is nature";
str1.append("of  beauty",9) ;
cout<<"String value is :"<<str1;
return 0;
}

Output:

Output

String value is Kashmir is nature of beauty

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience