5 Different Ways To Concatenate Strings In C#
In this post I will explain about several different ways To Concatenate Strings In C# with example. Adding strings is a commonly used operation in C# and .NET. The String class in C# provides multiple ways to add/merge strings which include, + operator, String.Concate(), String.Join(), String.Format(), StringBuilder.Append(), and String Interpolation.
Below are the 5 ways to concatenate strings in C#.
- Using + operator
- String Interpolation
- String.Concat()
- String.Join()
- String.Format()
1. Using + operator
The easiest method of adding two(or multiple) strings in C# is using + operators.
Now open Visual Studio and write the below line of code.
Syntax : str1 + str2
Example :
static void Main(string[] args)
{
// Declare strings variable
string firstName = "Rahul";
string lastName = "Patel";
// Concatenate two string variables using + operator
string fullName = firstName + " " + lastName;
Console.WriteLine(fullName);
Console.Read();
}
Output: Rahul Patel
2. String Interpolation
In string interpolation values of variables are replaced in the respective placeholder. It is required to add a $ sign at the start of the string. This was introduced in C# version 6.
Syntax : $”{var1} some other optional text {var2}.”
Example :
static void Main(string[] args)
{
// Declare strings variable
string playerName = "Virat Kohli";
string formate = "T20";
string milestone = "4000";
// Concatenate two string variables using String Interpolation
string message = $"{playerName} became the first batter in history to score {milestone} runs in {formate} Internationals";
Console.WriteLine(message);
Console.ReadKey();
}
Output: Virat Kohli became the first batter in history to score 4000 runs in T20 Internationals
3. String.Concat()
Using this method(String.Concat()), you concatenate one or more variables of String. It always returns a concatenated string.
Syntax: public static string Concat (string val1, string val2);
Example :
static void Main(string[] args)
{
// Declare strings variable
string str1 = "Welcome to ";
string str2 = "dotnetbyexample blog.";
string str3 = " This article describes various ways to concat string in c#.";
// Concatenate two string variables using String.Concat
string message = String.Concat(str1, str2, str3);
Console.WriteLine(message);
Console.ReadKey();
}
Output: Welcome to dotnetbyexample blog. This article describes various ways to concat string in c#.
4. String.Join()
The Join() method of C# is used to concatenate/join all the values of a string array, by making use of a specified separator between each value.
Syntax: public static string Join (string separator, string[] values);
Example:
static void Main(string[] args)
{
// Declare string array
string[] fruits = { "Mango ", "Banana ", "Pineapple ", "Sapota " };
// Concatenate string array using + operator
string fruitsWithSeparator = string.Join("&", fruits);
string fruitsWithOutSeparator = string.Join("", fruits);
Console.WriteLine("With separator : " + fruitsWithSeparator);
Console.WriteLine("Without separator : " + fruitsWithOutSeparator);
Console.ReadKey();
}
Output :
With separator : Mango &Banana &Pineapple &Sapota
Without separator: Mango Banana Pineapple Sapota
5. String.Format()
The String.Format() method returns a formatted string based on the argument passed to the method parameter.
Apart from string concatenation, it also serves some other purposes too. To check its other usage click here.
Syntax: String.Format(String format, Object...args);
Example :
static void Main(string[] args)
{
// Declare strings variable
string name = "Krunal Sisodiya. ";
string smallIntroduction = "I am Dot net developer.";
// Concatenate two string variables using String.Format()
string strFormat = String.Format("Hello, I am {0} {1}", name, smallIntroduction);
Console.WriteLine(strFormat);
Console.ReadKey();
}
Output: Hello, I am Krunal Sisodiya. I am Dot net developer.
Conclusion
I hope you get an idea about Different Ways To Concatenate Strings In C#.
I would like to have feedback on my blog
Your valuable feedback, question, or comments about this article are always welcome.
If you liked this post, don’t forget to share this.