C# | String Concat with examples | Set-2
Last Updated :
01 Feb, 2019
String.Concat Method is used to concatenate one or more instances of String or the String representations of the values of one or more instances of Object. It always returns a concatenated string.
This method can be overloaded by passing different types and number of parameters to it. There are total 11 methods in the overload list of the Concat method in which first 3 are discussed in Set-1 and remaining are discussed in Set-2, Set-3, and Set-4.
4. Concat(String, String)
This method is used to concatenate two different instances of the string. You can also use Concatenation operator(+)to concatenate strings.
Note: If an array contains a null object, then the Empty string is used in place of a null object.
Syntax:
public static string Concat (string strA, string strB);
Parameters:
strA: First string to concatenate.
strB: Second string to concatenate.
Return Value: The return type of this method is System.String. This method returns a string as an result of concatenation of two strings, i.e. strA, and strB.
Example:
using System;
public class GFG {
static public void Main()
{
string strA = "Hello! " ;
string strB = "Geeks." ;
string str;
Console.WriteLine( "String A is: {0}" , strA);
Console.WriteLine( "String B is: {0}" , strB);
str = String.Concat(strA, strB);
Console.WriteLine( "Concatenated string is: {0}" , str);
}
}
|
Output:
String A is: Hello!
String B is: Geeks.
Concatenated string is: Hello! Geeks.
5. Concat(String, String, String)
This method is used to concatenate three different string into a single string. You can also use Concatenation operator(+)to concatenate strings.
Note: If an array contains a null object, then Empty string is used in place of a null object.
Syntax:
public static string Concat (string strA, string strB, string strC);
Parameters:
strA: First string to concatenate.
strB: Second string to concatenate.
strC: Third string to concatenate.
Return Value:The return type of this method is System.String. This method returns a string which is created from the concatenation of three strings, i.e. strA, strB, and strC.
Example:
using System;
class GFG {
static public void Main()
{
string strA = "Welcome " ;
string strB = "to " ;
string strC = "GFG. " ;
string str;
Console.WriteLine( "String A is: {0}" , strA);
Console.WriteLine( "String B is: {0}" , strB);
Console.WriteLine( "String C is: {0}" , strC);
str = String.Concat(strA, strB, strC);
Console.WriteLine( "Concatenated string is: {0}" , str);
}
}
|
Output:
String A is: Welcome
String B is: to
String C is: GFG.
Concatenated string is: Welcome to GFG.
6. Concat(String[])
This method is used to concatenates the elements of a specified String array.
Syntax:
public static string Concat (params string[] items);
Here, items are the array of string instance.
Return Value: The return type of this method is System.String. This method returns the concatenated elements of items.
Exceptions:
- If the value of the given string items is null then this method will give ArgumentNullException.
- If the array is out of memory, then this method will give OutOfMemoryException .
Example:
using System;
class GFG {
public static void Main()
{
string [] strA = { "Hey, " , "This " , "is " , "C# " , "Tutorial." };
foreach ( string elements in strA)
{
Console.WriteLine( "Elements of strA array : {0}" , elements);
}
Console.WriteLine( "After Concatenation: {0}" ,
string .Concat(strA));
}
}
|
Output:
Elements of strA array : Hey,
Elements of strA array : This
Elements of strA array : is
Elements of strA array : C#
Elements of strA array : Tutorial.
After Concatenation: Hey, This is C# Tutorial.
Next : Set 3
Reference: https://docs.microsoft.com/en-us/dotnet/api/system.string.concat?view=netframework-4.7.2
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...