Wednesday, November 20, 2024
Google search engine
HomeData Modelling & AIDifference between concatenation of strings using (str += s) and (str =...

Difference between concatenation of strings using (str += s) and (str = str + s)

A string is a collection of characters. For example, “neveropen” is a string. C++ provides primitive data types to create a string. The string can also be initialized at the time of declaration.

Syntax:

string str;

string str = “neveropen”

Here, “neveropen” 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.

 Syntax:

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++




// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    str += str1;
 
    // Print the string
    cout << str;
    return 0;
}


Java




// Java program to implement
// the above approach
import java.util.*;
class GFG{
 
// Driver code
public static void main(String[] args)
{
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    str += str1;
 
    // Print the String
    System.out.print(str);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python code for the above approach
# Driver code
 
# Declaring an empty string
str = "Geeks";
 
# String to be concatenated
str1 = "forGeeks";
 
# Concatenate str and str1
# using addition assignment operator
str += str1;
 
# Print the string
print(str);
 
# This code is contributed by gfgking


C#




// C# program to implement
// the above approach
using System;
 
public class GFG{
 
// Driver code
public static void Main(String[] args)
{
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    str += str1;
 
    // Print the String
    Console.Write(str);
}
}
 
// This code is contributed by 29AjayKumar


Javascript




<script>
        // JavaScript code for the above approach
        // Driver code
 
        // Declaring an empty string
        let str = "Geeks";
 
        // String to be concatenated
        let str1 = "forGeeks";
 
        // Concatenate str and str1
        // using addition assignment operator
        str += str1;
 
        // Print the string
        document.write(str);
 
  // This code is contributed by Potta Lokesh
    </script>


Output

neveropen

Addition(+) operator

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.

Syntax:

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++




// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the string
    cout << str;
    return 0;
}


Java




// Java program to implement
// the above approach
 
class GFG{
 
// Driver code
public static void main(String[] args)
{
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the String
    System.out.print(str);
}
}
 
// This code is contributed by 29AjayKumar


Python3




# Python program to implement
# the above approach
 
# Driver code
 
# Declaring an empty string
str1 = "Geeks"
 
# String to be concatenated
str2 = "forGeeks"
 
# Concatenate str and str1
# using addition operator
str1 += str2
 
# Print the string
print(str1)
 
# This code is contributed by phasing17


C#




// C# program to implement
// the above approach
using System;
public class GFG {
 
  // Driver code
  public static void Main(String[] args) {
 
    // Declaring an empty String
    String str = "Geeks";
 
    // String to be concatenated
    String str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the String
    Console.Write(str);
  }
}
 
// This code is contributed by umadevi9616


Javascript




// JavaScript program to implement
// the above approach
 
// Driver code
function main() {
    // Declaring an empty string
    let str = "Geeks";
 
    // String to be concatenated
    let str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition operator
    str = str + str1;
 
    // Print the string
    console.log(str);
}
 
main();
 
// Contributed by adityashae15


Output

neveropen

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()
{
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    // Concatenate str1 at the end of str
    str += str1;
 
    // Print the string
    cout << "Resultant string using += "
         << str << '\n';
 
    str = "Geeks";
 
    // Concatenate str and str1
    // using addition operator
    // Concatenate str and str1
    // and assign the result to str again
    str = str + str1;
 
    // Print the string
    cout << "Resultant string using + "
         << str;
    return 0;
}


Java




public class StringConcatenation {
    public static void main(String[] args) {
        // Declaring an empty string
        String str = "Geeks";
 
        // String to be concatenated
        String str1 = "forGeeks";
 
        // Concatenate str and str1 using +=
        // Concatenate str1 at the end of str
        str += str1;
 
        // Print the string
        System.out.println("Resultant string using += " + str);
 
        // Reset str to its original value
        str = "Geeks";
 
        // Concatenate str and str1 using +
        // Concatenate str and str1
        // and assign the result to str again
        str = str + str1;
 
        // Print the string
        System.out.println("Resultant string using + " + str);
    }
}


Python3




# Python equivalent of above C++ code
# Declaring an empty string
str = "Geeks"
 
# String to be concatenated
str1 = "forGeeks"
 
# Concatenate str and str1
# using addition assignment operator
# Concatenate str1 at the end of str
str += str1
 
# Print the string
print("Resultant string using += ",str)
 
str = "Geeks"
 
# Concatenate str and str1
# using addition operator
# Concatenate str and str1
# and assign the result to str again
str = str + str1
 
# Print the string
print("Resultant string using + ",str)


C#




using System;
 
class Program {
  static void Main(string[] args) {
 
    // Declaring an empty string
    string str = "Geeks";
 
    // String to be concatenated
    string str1 = "forGeeks";
 
    // Concatenate str and str1
    // using addition assignment operator
    // Concatenate str1 at the end of str
    str += str1;
 
    // Print the string
    Console.WriteLine("Resultant string using += " + str);
 
    // Reset str
    str = "Geeks";
 
    // Concatenate str and str1
    // using addition operator
    // Concatenate str and str1
    // and assign the result to str again
    str = str + str1;
 
    // Print the string
    Console.WriteLine("Resultant string using + " + str);
  }
}


Javascript




// Declaring an empty string
let str = "Geeks";
 
// String to be concatenated
let str1 = "forGeeks";
 
// Concatenate str and str1 using +=
// Concatenate str1 at the end of str
str += str1;
 
// Print the string
console.log("Resultant string using += " + str);
 
// Reset str to its original value
str = "Geeks";
 
// Concatenate str and str1 using +
// Concatenate str and str1
// and assign the result to str again
str = str + str1;
 
// Print the string
console.log("Resultant string using + " + str);


Output

Resultant string using += neveropen
Resultant string using + neveropen

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++




// C++ program to implement
// the above approach
#include <iostream>
using namespace std;
 
// Driver code
int main()
{
 
    // Declaring an empty string
    string str = "neveropen";
 
    // String to be concatenated
    string str1 = " neveropen";
 
    // String to be concatenated
    string str2 = " neveropen";
 
    // String to be concatenated
    string str3 = " neveropen";
 
    // Concatenate str, str1, str2 and str3
    // using addition assignment operator
    // in multiple statements
    str += str1;
 
    str += str2;
 
    str += str3;
 
    // Print the string
    cout << "Resultant string using +="
         << str << '\n';
 
    str = "neveropen";
 
    // Concatenate str, str1, str and str3
    // using addition operator
    // in a single statement
    str = str + str1 + str2 + str3;
 
    // Print the string
    cout << "Resultant string using + "
         << str;
    return 0;
}


Java




// Java program to implement
// the above approach
import java.io.*;
 
class GFG {
 
// Driver code
public static void main (String[] args)
{
 
    // Declaring an empty string
    String str = "neveropen";
 
    // String to be concatenated
    String str1 = " neveropen";
 
    // String to be concatenated
    String str2 = " neveropen";
 
    // String to be concatenated
    String str3 = " neveropen";
 
    // Concatenate str, str1, str2 and str3
    // using addition assignment operator
    // in multiple statements
    str += str1;
 
    str += str2;
 
    str += str3;
 
    // Print the string
    System.out.println("Resultant string using +="
         +str);
 
    str = "neveropen";
 
    // Concatenate str, str1, str and str3
    // using addition operator
    // in a single statement
    str = str + str1 + str2 + str3;
 
    // Print the string
    System.out.print("Resultant string using + "
         + str);
}
}
 
//this code is contributed by shivanisinghss2110


Python3




# Initialize an empty string
str = "neveropen"
 
# Strings to be concatenated
str1 = " neveropen"
str2 = " neveropen"
str3 = " neveropen"
 
# Concatenate str, str1, str2, and str3
# using the addition assignment operator
# in multiple statements
str += str1
str += str2
str += str3
 
# Print the resultant string
print("Resultant string using +=", str)
 
# Reset the string to the original value
str = "neveropen"
 
# Concatenate str, str1, str2, and str3
# using the addition operator in a single statement
str = str + str1 + str2 + str3
 
# Print the resultant string
print("Resultant string using +", str)


C#




using System;
 
class Program
{
    static void Main(string[] args)
    {
        // Declaring an empty string
        string str = "neveropen";
 
        // Strings to be concatenated
        string str1 = " neveropen";
        string str2 = " neveropen";
        string str3 = " neveropen";
 
        // Concatenate str, str1, str2, and str3
        // using addition assignment operator
        // in multiple statements
        str += str1;
        str += str2;
        str += str3;
 
        // Print the string
        Console.WriteLine("Resultant string using +=" + str);
 
        // Reset the string
        str = "neveropen";
 
        // Concatenate str, str1, str2, and str3
        // using addition operator in a single statement
        str = str + str1 + str2 + str3;
 
        // Print the string
        Console.WriteLine("Resultant string using + " + str);
    }
}


Output

Resultant string using +=neveropen neveropen neveropen neveropen
Resultant string using + neveropen neveropen neveropen neveropen

Factor 4: Performance:

  • 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++




// C++ program to calculate
// performance of +=
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
 
// Function whose time is to
// be measured
void fun()
{
    // Initialize a n empty string
    string str = "";
 
    // concatenate the characters
    // from 'a' to 'z'
    for (int i = 0; i < 26; i++) {
        char c = 'a' + i;
        str += c;
    }
}
 
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
 
    // Start timer
    gettimeofday(&start, NULL);
 
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
 
    // Function Call
    fun();
 
    // Stop timer
    gettimeofday(&end, NULL);
 
    // Calculating total time taken
    // by the program.
    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;
}


Output

Time taken by program is : 0.000046 sec

Example: Below is the program to demonstrate the performance of the + string concatenation method.

C++




// C++ program to calculate
// performance of +
#include <bits/stdc++.h>
#include <sys/time.h>
using namespace std;
 
// Function whose time is to
// be measured
void fun()
{
    // Initialize a n empty string
    string str = "";
 
    // concatenate the characters
    // from 'a' to 'z'
    for (int i = 0; i < 26; i++) {
 
        char c = 'a' + i;
        str = str + c;
    }
}
 
// Driver Code
int main()
{
    // Use function gettimeofday()
    // can get the time
    struct timeval start, end;
 
    // Start timer
    gettimeofday(&start, NULL);
 
    // unsync the I/O of C and C++.
    ios_base::sync_with_stdio(false);
 
    // Function Call
    fun();
 
    // Stop timer
    gettimeofday(&end, NULL);
 
    // Calculating total time taken
    // by the program.
    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;
}


Output

Time taken by program is : 0.000034 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.

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments