A string is a collection of characters. For example, “w3wiki” is a string. C++ provides primitive data types to create a string. The string can also be initialized at the time of declaration.
string str;
string str = “w3wiki”
Here, “w3wiki” is a string literal.
This article shows the difference between the concatenation of the strings using the addition assignment operator (+=) and the addition (+) operator used with strings. Concatenation is the process of joining end-to-end.
Addition assignment (+=) operator
In C++, a string addition assignment operator is used to concatenate one string to the end of another string.
str += value
Here,
value is a string to be concatenated with str.
It appends the value (literal) at the end of the string, without any reassignment.
Example: Below is the C++ program to demonstrate the addition assignment operator.
C++
#include <iostream>
using namespace std;
int main()
{
string str = "Beginner" ;
string str1 = "forBeginner" ;
str += str1;
cout << str;
return 0;
}
|
Java
import java.util.*;
class GFG{
public static void main(String[] args)
{
String str = "Beginner" ;
String str1 = "forBeginner" ;
str += str1;
System.out.print(str);
}
}
|
Python3
str = "Beginner" ;
str1 = "forBeginner" ;
str + = str1;
print ( str );
|
C#
using System;
public class GFG{
public static void Main(String[] args)
{
String str = "Beginner" ;
String str1 = "forBeginner" ;
str += str1;
Console.Write(str);
}
}
|
Javascript
<script>
let str = "Beginner" ;
let str1 = "forBeginner" ;
str += str1;
document.write(str);
</script>
|
In C++, a string addition operator is used to concatenate one string to the end of another string. But in this case the after the concatenation of strings, the modified string gets assigned to the string.
str = str + value
Here,
value is a string to be concatenated with str.
It firstly appends the value (literal) at the end of the string and then reassigns it to str.
Example: Below is the C+ program to demonstrate the above approach.
C++
#include <iostream>
using namespace std;
int main()
{
string str = "Beginner" ;
string str1 = "forBeginner" ;
str = str + str1;
cout << str;
return 0;
}
|
Java
class GFG{
public static void main(String[] args)
{
String str = "Beginner" ;
String str1 = "forBeginner" ;
str = str + str1;
System.out.print(str);
}
}
|
Python3
str1 = "Beginner"
str2 = "forBeginner"
str1 + = str2
print (str1)
|
C#
using System;
public class GFG {
public static void Main(String[] args) {
String str = "Beginner" ;
String str1 = "forBeginner" ;
str = str + str1;
Console.Write(str);
}
}
|
Javascript
function main() {
let str = "Beginner" ;
let str1 = "forBeginner" ;
str = str + str1;
console.log(str);
}
main();
|
Although both operators when used with strings can be used for the concatenation of strings, there are some differences between them:
Factor 1: Assignment of the modified string:
- The addition assignment operator (+=) concatenates two strings by appending one string at the end of another string.
- The addition operator(+) concatenates two strings by appending one string at the end of the original string and then assigning the modified string to the original string.
Example: Below is the C++ program to demonstrate the above approach.
C++
#include <iostream>
using namespace std;
int main()
{
string str = "Beginner" ;
string str1 = "forBeginner" ;
str += str1;
cout << "Resultant string using += "
<< str << '\n' ;
str = "Beginner" ;
str = str + str1;
cout << "Resultant string using + "
<< str;
return 0;
}
|
Java
public class StringConcatenation {
public static void main(String[] args) {
String str = "Beginner" ;
String str1 = "forBeginner" ;
str += str1;
System.out.println( "Resultant string using += " + str);
str = "Beginner" ;
str = str + str1;
System.out.println( "Resultant string using + " + str);
}
}
|
Python3
str = "Beginner"
str1 = "forBeginner"
str + = str1
print ( "Resultant string using += " , str )
str = "Beginner"
str = str + str1
print ( "Resultant string using + " , str )
|
C#
using System;
class Program {
static void Main( string [] args) {
string str = "Beginner" ;
string str1 = "forBeginner" ;
str += str1;
Console.WriteLine( "Resultant string using += " + str);
str = "Beginner" ;
str = str + str1;
Console.WriteLine( "Resultant string using + " + str);
}
}
|
Javascript
let str = "Beginner" ;
let str1 = "forBeginner" ;
str += str1;
console.log( "Resultant string using += " + str);
str = "Beginner" ;
str = str + str1;
console.log( "Resultant string using + " + str);
|
Output
Resultant string using += w3wiki
Resultant string using + w3wiki
Factor 2: Operator overloaded functions used:
- The addition assignment operator (+=) concatenates two strings because the operator is overloaded internally.
- In this case, also, the addition operator (+) concatenates two strings because the operator is overloaded internally.
Factor 3: Number of strings concatenated:
- The addition assignment operator (+=) can concatenate two strings at a time in a single statement.
- The addition operator (+) can concatenate multiple strings by using multiple addition (+) operators between the string in a single statement. For example, str = str1 + str2 + str3 + … + strn
Example: In this program, three different statements are required to concatenate three strings; str, str1, str2, and str3 using the assignment addition operator (+=) and a single statement is required to concatenate three strings; str, str1, str2, and str3 using the addition operator (+).
C++
#include <iostream>
using namespace std;
int main()
{
string str = "w3wiki" ;
string str1 = " w3wiki" ;
string str2 = " w3wiki" ;
string str3 = " w3wiki" ;
str += str1;
str += str2;
str += str3;
cout << "Resultant string using +="
<< str << '\n' ;
str = "w3wiki" ;
str = str + str1 + str2 + str3;
cout << "Resultant string using + "
<< str;
return 0;
}
|
Java
import java.io.*;
class GFG {
public static void main (String[] args)
{
String str = "w3wiki" ;
String str1 = " w3wiki" ;
String str2 = " w3wiki" ;
String str3 = " w3wiki" ;
str += str1;
str += str2;
str += str3;
System.out.println( "Resultant string using +="
+str);
str = "w3wiki" ;
str = str + str1 + str2 + str3;
System.out.print( "Resultant string using + "
+ str);
}
}
|
Python3
str = "w3wiki"
str1 = " w3wiki"
str2 = " w3wiki"
str3 = " w3wiki"
str + = str1
str + = str2
str + = str3
print ( "Resultant string using +=" , str )
str = "w3wiki"
str = str + str1 + str2 + str3
print ( "Resultant string using +" , str )
|
C#
using System;
class Program
{
static void Main( string [] args)
{
string str = "w3wiki" ;
string str1 = " w3wiki" ;
string str2 = " w3wiki" ;
string str3 = " w3wiki" ;
str += str1;
str += str2;
str += str3;
Console.WriteLine( "Resultant string using +=" + str);
str = "w3wiki" ;
str = str + str1 + str2 + str3;
Console.WriteLine( "Resultant string using + " + str);
}
}
|
Javascript
let str = "w3wiki" ;
let str1 = " w3wiki" ;
let str2 = " w3wiki" ;
let str3 = " w3wiki" ;
str += str1;
str += str2;
str += str3;
console.log( "Resultant string using +=" + str);
str = "w3wiki" ;
str = str + str1 + str2 + str3;
console.log( "Resultant string using + " + str);
|
Output
Resultant string using +=w3wiki w3wiki w3wiki w3wiki
Resultant string using + w3wiki w3wiki w3wiki w3wiki
- The addition assignment operator (+=) when used for the concatenation of strings gives better efficiency as compared to the addition(+) operator. This is because no reassignment of strings takes place in this case.
- The addition operator (+) when used for the concatenation of strings, is less efficient as compared to the addition (+=) operator. This is because the assignment of strings takes place in this case.
Example: Below is the program to demonstrate the performance of the += string concatenation method.
C++
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
void fun()
{
string str = "" ;
for ( int i = 0; i < 26; i++) {
char c = 'a' + i;
str += c;
}
}
int main()
{
struct timeval start, end;
gettimeofday(&start, NULL);
ios_base::sync_with_stdio( false );
fun();
gettimeofday(&end, NULL);
double time_taken;
time_taken = (end.tv_sec
- start.tv_sec)
* 1e6;
time_taken = (time_taken
+ (end.tv_usec
- start.tv_usec))
* 1e-6;
cout << "Time taken by program is : "
<< fixed
<< time_taken << setprecision(6);
cout << " sec" << endl;
return 0;
}
|
Java
import java.util.Date;
public class Main {
static void fun() {
StringBuilder str = new StringBuilder();
for ( int i = 0 ; i < 26 ; i++) {
char c = ( char ) ( 'a' + i);
str.append(c);
}
}
public static void main(String[] args) {
long startTime = System.nanoTime();
fun();
long endTime = System.nanoTime();
double timeTaken = (endTime - startTime) / 1e9;
System.out.printf( "Time taken by program is : %.6f sec%n" , timeTaken);
}
}
|
Python3
import time
def fun():
str = ""
for i in range ( 26 ):
c = chr ( ord ( 'a' ) + i)
str + = c
if __name__ = = "__main__" :
startTime = time.time()
fun()
endTime = time.time()
timeTaken = endTime - startTime
print (f "Time taken by program is: {timeTaken:.6f} sec" )
|
C#
using System;
using System.Diagnostics;
class Program {
static void Fun()
{
string str = "" ;
for ( int i = 0; i < 26; i++) {
char c = ( char )( 'a' + i);
str += c;
}
}
static void Main()
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Fun();
stopwatch.Stop();
double timeTaken = stopwatch.Elapsed.TotalSeconds;
Console.WriteLine(
"Time taken by program is: {0:F6} sec" ,
timeTaken);
}
}
|
Javascript
function fun() {
let str = '' ;
for (let i = 0; i < 26; i++) {
let c = String.fromCharCode( 'a' .charCodeAt(0) + i);
str += c;
}
}
let startTime = new Date().getTime();
fun();
let endTime = new Date().getTime();
let timeTaken = (endTime - startTime) / 1000;
console.log(`Time taken by program is: ${timeTaken.toFixed(6)} sec`);
|
Output
Time taken by program is : 0.000490 sec
Example: Below is the program to demonstrate the performance of the + string concatenation method.
C++
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
void fun()
{
string str = "" ;
for ( int i = 0; i < 26; i++) {
char c = 'a' + i;
str = str + c;
}
}
int main()
{
struct timeval start, end;
gettimeofday(&start, NULL);
ios_base::sync_with_stdio( false );
fun();
gettimeofday(&end, NULL);
double time_taken;
time_taken = (end.tv_sec
- start.tv_sec)
* 1e6;
time_taken = (time_taken
+ (end.tv_usec
- start.tv_usec))
* 1e-6;
cout << "Time taken by program is : "
<< fixed
<< time_taken << setprecision(6);
cout << " sec" << endl;
return 0;
}
|
Java
import java.util.*;
public class PerformanceTest {
static void fun() {
StringBuilder str = new StringBuilder();
for ( int i = 0 ; i < 26 ; i++) {
char c = ( char )( 'a' + i);
str.append(c);
}
}
public static void main(String[] args) {
long start = System.currentTimeMillis();
fun();
long end = System.currentTimeMillis();
double timeTaken = (end - start) / 1000.0 ;
System.out.printf( "Time taken by program is: %.6f sec%n" , timeTaken);
}
}
|
Python3
import time
def fun():
string = ""
for i in range ( 26 ):
c = chr ( ord ( 'a' ) + i)
string = string + c
if __name__ = = "__main__" :
start_time = time.time()
fun()
end_time = time.time()
time_taken = end_time - start_time
print ( "Time taken by program is : {:.6f} sec" . format (time_taken))
|
C#
using System;
using System.Diagnostics;
class GFG
{
static void Fun()
{
string str = "" ;
for ( int i = 0; i < 26; i++)
{
char c = ( char )( 'a' + i);
str = str + c;
}
}
static void Main( string [] args)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
Fun();
stopwatch.Stop();
double time_taken = stopwatch.Elapsed.TotalSeconds;
Console.WriteLine( "Time taken by program is : " + time_taken.ToString( "F6" ) + " sec" );
}
}
|
Javascript
function fun() {
let str = "" ;
for (let i = 0; i < 26; i++) {
let c = String.fromCharCode( 'a' .charCodeAt(0) + i);
str = str + c;
}
}
console.time( "Time taken by program is" );
fun();
console.timeEnd( "Time taken by program is" );
|
Output
Time taken by program is : 0.000715 sec
S No. |
Factor |
+= operator |
+ operator |
1 |
Assignment |
It appends a string at the end of the original string. |
It appends a string at the end of the original string and then reassigns the modified string to the original string. |
2 |
Overloaded functions |
operator overloaded function used with strings is different from the += operator. |
operator overloaded function used with strings is different from the + operator. |
3 |
Number of strings concatenated |
It can concatenate two strings at a time in a single statement. |
Multiple strings can be concatenated using multiple addition (+) operators between the string. For example, str = str1 + str2 + str3 + … + strn |
4 |
Performance |
This operator when used for the concatenation of strings gives better efficiency as compared to the addition(+) operator. This is because no reassignment of strings takes place in this case. |
This operator when used for the concatenation is not as efficient as compared to the addition(+=) operator. This is because reassignment of strings takes place in this case. |
Contact Us