This function is used to extend the string by appending new characters at the end of the string.
Syntax
Consider two strings str1 and str2. Syntax would be :
Example
str1.operator+=(str2);
str1.operator+=(ch);
str1.operator+=(const char* s);
Parameter
str2: str2 is a string object which is to be appended.
s: s is a pointer to the null terminated sequence of characters.
ch: ch is a character which is to be appended to the current 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 see a simple example by passing single character value.
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