Concatenate or Combine Two Strings in .NET Core Using C#.Net

Concatenate or combine or join strings in .Net core using c#.net. Best practice in .Net are + (Plush), StringBuilder(), string.Format().
In today's article I will show you how you can concatenate or combine or join two or more string in .net core using c#.net. To concatenate or combine we will use console application in .NET core using c#.net.  

So here in this article we will learn multiple ways to combine the string using C#.net. This article you can use in your asp.net, asp.net core mvc, console and windows application also. 

How to Use + (Plush) To Concatenate in C#.Net

For this first we will define a variable of string type. In this variable initially we will assign a blank value. 

string finalString = "";
string str1 = "This is FIRST string.";
string str2 = "This is SECOND string.";

finalString = str1 + str2;

Console.WriteLine("STRING 1: " + str1);
Console.WriteLine("STRING 2: " + str2);
Console.WriteLine("FINAL STRING: " + finalString);
Console.ReadLine();
In above code I have declared a variable finalstring to store the concinnated or combine string of two string which I have stored in str1 and str2. Now let's run the code to check the by putting a break point to check output. 

code to combine two string using c#.net
In above screenshot we can see that the tow string got combine or concatenate by using +(Plush). Now press F5 to check the output.

Concatenate or Combine Two String in .NET Core Using C#.Net

How to Use StringBuilder() to Concatenate String Using C#.Net

StringBuilder is the class which we will use to concatenate to or more string in c#.net. Please check the below code. 
using System.Text;

string str1 = "This is FIRST string.";
string str2 = "This is SECOND string.";

StringBuilder stringBuilder = new StringBuilder();
stringBuilder.Append(str1);
stringBuilder.Append(str2);

Console.WriteLine("STRING 1: " + str1);
Console.WriteLine("STRING 2: " + str2);
Console.WriteLine("FINAL STRING: " + stringBuilder.ToString());
Console.ReadLine();
In above code I have declared tow variables names as str1 and str2 which is holding two of the different string values which we are going to concatenate in out c#.net code. After declaring the string variable, I have created object for StringBuilder() class. After that we need to use Append() to concatenate the string by passing the string values one by one. Now let's run the code by putting break point. 

Concatenate or Combine Two String By StringBuilder() in .NET Core Using C#.Net
In above code just check the above final data. We are able to get the combined string. Now press F5 to check the final output.

StringBuilder() to combine Tow String Using C#.net

How to Use string.Format() to Concatenate String Using C#.Net

string.Format() to use combine or concatenate one more string. By using string.Format() we combine multiple string . In this we pass index with curly braises Eg. {0}{1}. Please check the below code. 
using System.Text;

string finalString = "";
string str1 = "This is FIRST string.";
string str2 = "This is SECOND string.";

Console.WriteLine("STRING 1: " + str1);
Console.WriteLine("STRING 2: " + str2);

finalString = string.Format("{0}{1}", str1, str2);

Console.WriteLine("FINAL STRING: " + finalString);
Console.ReadLine();
In above code taken two variables same as above two examples which is str1 and str 2. Now check the highlighted code having string.Format(). In this I have defined tow index and passed two values. If you pass less value as per passed index. You will get error "System.FormatException: 'Index (zero based) must be greater than or equal to zero and less than the size of the argument list.". So, you need to pass the values as per the number of Index define. You can pass more but cannot pass less. 

System.FormatException
Now let's correct the code and run the code by putting break point to check the output.

Concatenate or Combine Two String in .NET Core Using C#.Net

In above code we can see the combines two strings. Now press F5 to check the final output.

Concatenate or Combine Two String in .NET Core Using C#.Net

Post a Comment