String Operator+ - C++ Programming Tutorial
C++ Course / Strings / String Operator+

String Operator+

BLUF: Mastering String Operator+ 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 Operator+

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

This function serves the purpose of lengthening the string by adding additional characters to the end of it.

Syntax

Consider two strings referred to as str1 and str2. The syntax is as follows:

Example

str1.operator+=(str2);
str1.operator+=(ch);
str1.operator+=(const char* s);

Parameter

Example

str1.operator+=(str2);
str1.operator+=(ch);
str1.operator+=(const char* s);

s: s represents a pointer to a sequence of characters that ends with a null character.

ch: 'ch' represents a character that will be added to the existing value of the string.

Return value

It returns *this.

Example 1

Let's see a simple example.

Example

#include<iostream>
using namespace std;
int main()
{
string str = "java ";
string str1="programming";
str.operator+=(str1);
cout<<str; 
return 0;
}

Output:

Output

java programming

Example 2

Let's see another simple example.

Example

#include<iostream>
using namespace std;
int main()
{
string s1 ="12";
string s2 ="34";
s1.operator+=(s2);
cout<<s1;
return 0; 
}

Output:

Example 3

Let's explore a basic illustration by providing a single character input.

Example

#include<iostream>
using namespace std;
int main()
{
string s="C+";
char ch='+';
cout<<s.operator+=(ch);
return 0; 
}

Output:

Example 4

Let's see this example.

Example

#include<iostream>
using namespace std;
int main()
{
string str ="javaCppTutorial ";
char* s="Tutorial";
str.operator+=(s);
cout<<str;
return 0; 
}

Output:

Output

javaCppTutorial Tutorial

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