String concatenation

String concatenation

String concatenation is the process of joining two or more strings together to form a new string. In C++, there are several ways to concatenate strings:

  1. Using the + operator:

You can use the + operator to concatenate two strings. For example:

				
					std::string
str1 = "Hello";

std::string
str2 = "world";

std::string
result = str1 + " " + str2;

//result = "Hello world"
				
			
  1. Using the append() function:

The append() function can be used to add one string to the end of another string. For example:

				
					std::string
str1 = "Hello";

std::string
str2 = "world";

str1.append("");

str1.append(str2);

//str1 = "Hello world"

				
			
  1. Using the += operator:

The += operator can be used to concatenate one string to another string. For example:

				
					std::string str1 = "Hello";

std::string  str2 = "world";

str1 +=" ";

str1 +=str2;

// str1= "Hello world"
				
			

Note that in all of these examples, the original strings are not modified. Instead, a new string is created that contains the concatenated result. If you want to modify one of the original strings, you can use the append() or += operator on the original string directly.

Join To Get Our Newsletter
Spread the love