In this article, we will discuss C# Concatenate Strings. We will look at six ways of C# Concatenate Strings and how to implement them.
Concatenation of Strings in C#
String addition or concatenation, is a frequent operation in C# and .NET.
String.Concate(), String.Join(), String.Format(), StringBuilder.Append() and String Interpolation are all methods for adding, inserting, and merging strings provided by the String class.
String concatenation is adding or putting one at the end of another string. Strings are immutable in C# and .NET Core. Each operation creates a new string object even if you utilize the same string variable.
6 Effective Ways To Concatenate Strings In C#
Here are six different ways to concatenate strings in C#.
// Concatenate two string variables string name = fName + " " + lName; Console.WriteLine(name);
} }
Output 1:
Concatenation occurs at compile time for string literals and string constants; no concatenation occurs at run time. Concatenation of string variables happens only at run time.
String Interpolation in C#
String interpolation is a technique for joining variables as part of a string. String interpolation syntax begins with a '$' symbol, and code variables are enclosed in brackets.
The following code example concatenates strings to form a longer string.
Example 2:
C#
C#
using System;
class Example2 { static void Main() { // String Interpolation string city= "Mumbai"; string country = "India";
// Use string interpolation to concatenate strings. string cityOfcountry = $"{city} is a city in {country}."; Console.WriteLine(cityOfcountry); } }
Output 2:
String.Concatenate() in C#
String.Concate() is a method that adds two strings, objects, and arrays of strings and string combinations. Let us see hoe to use it with an example.
We can also use the C# Concatenate String method “concat”, to add two concats together. The last line might seem intimidating initially, but it is easy to implement. Let us see how to do it.
String.Join() concatenates array elements or collection members using the specified separator between each of the elements or members. The array or collection can contain any data, including numbers and objects. String.Join() can also concatenate string items of any data type with a range.
String.format() method formats strings by inserting objects and variables into other strings and literals with specified space and alignments. It is also frequently used to format strings into predefined formats.
String.Format() method has eight overloaded formats to provide options for formatting various objects and variables, including variables that can format strings. The eight overloaded methods are
namespace singleVariable { class Example6 { public static void Main(string [] args) {
int number = 8; // Format string string strFormat = String.Format("There are {0} mangoes in the basket.", number); Console.WriteLine(strFormat); } } }
Output 6:
Example 7: To insert multiple objects in a string.
C#
C#
using System;
namespace multipleObject { class Example7 { public static void Main(string [] args) {
string name = "Rahul"; string programmingLanguage = "Csharp";
// Format the string string strFormat = String.Format("{0} likes to code in {1}", name, programmingLanguage); Console.WriteLine(strFormat);
Console.ReadLine(); } } }
Output 7:
StringBuilder.Append() in C#
In .NET, string objects are immutable.
What does this imply? It means that every time you use one of the String class methods, whether, with the same or a different variable, a new string object is created in memory. This means that memory space in your computer's memory is reserved for that new string.
The more string manipulation methods you use, the more memory space is allocated in memory. That is, in a string manipulation-heavy code, if strings are not used wisely, they can cause serious app performance issues.
.NET provides the System.Text.StringBuilder class that allows you to modify strings without creating new ones. StringBuilder is highly recommended if your code contains more than a few hundred string concatenation operations. StringBuilder should not be used for some string concatenation operations. StringBuilder class is defined in the System.Text namespace. This namespace must be imported into your class or referenced directly in the object instantiation.
Let us understand this method of “C# Concatenate Strings” with the help of an example.
Example 8:
C#
C#
using System; using System.Text;
class Example8 {
// Main Method public static void Main() { // The capacity is 25 StringBuilder sampleString = new StringBuilder("Welcome to ", 25);
To add two strings together in C#, you can use the + operator or the string.Concat() method.
What is the best way to concat strings in C?
The best way to concatenate strings in C# is by using the StringBuilder class when dealing with large string manipulations. For smaller concatenations or simplicity, you can use the + operator or string.Concat().
Is string format quicker than C# concatenation?
Format (via Reflector) creates a StringBuilder and then calls AppendFormat on it. As a result, it is faster than concate for multiple strings.
What is the difference between append and concat in C#?
The difference between append and concat in C#, append is for efficiently building large strings using StringBuilder, avoiding many intermediate objects. On the other hand, concat in C# is for joining multiple strings into one, creating a new string object with string.Concat().
Conclusion
This blog discusses different ways to concatenate strings in C#. As we discussed a total of 6 methods: using + operator, using interpolation, using format(), join(), concatenate() methods, and using StringBuilder.Append() method. We looked at an example for each for clarity of concepts. Toward the end, we looked at some frequently asked questions on this topic. We hope that you got a clear understanding of C# Concatenate Strings. Check out this problem - Longest String Chain
To explore more interesting coding-related articles refer to the following links:
Refer to our guided pathways on Code studio to learn more about Competitive Programming, JavaScript, System Design, DSA C++, etc. Enroll in our courses, and use the accessible sample exams and questions as a guide. For placement preparations, look at the interview experiences and interview package.