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:
str1.operator+=(str2);
str1.operator+=(ch);
str1.operator+=(const char* s);
Parameter
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.
#include<iostream>
using namespace std;
int main()
{
string str = "java ";
string str1="programming";
str.operator+=(str1);
cout<<str;
return 0;
}
Output:
java programming
Example 2
Let's see another simple 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.
#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.
#include<iostream>
using namespace std;
int main()
{
string str ="javaCppTutorial ";
char* s="Tutorial";
str.operator+=(s);
cout<<str;
return 0;
}
Output:
javaCppTutorial Tutorial