C# | String Concat with examples | Set-2

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the use 
// of Concat(String, String ) Method
using System;
  
public class GFG {
      
    // Main Method
    static public void Main()
    {
        string strA = "Hello! ";
        string strB = "Geeks.";
        string str;
  
        // print all strings
        Console.WriteLine("String A is: {0}", strA);
        Console.WriteLine("String B is: {0}", strB);
  
        // Concatenate two different strings
        // into a single String
        // using  Concat(String, String ) Method
        str = String.Concat(strA, strB);
  
        Console.WriteLine("Concatenated string is: {0}", str);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// Concat(String, String, String) Method
using System;
  
class GFG {
      
    // Main Method
    static public void Main()
    {
        string strA = "Welcome ";
        string strB = "to ";
        string strC = "GFG. ";
        string str;
  
        // print all strings
        Console.WriteLine("String A is: {0}", strA);
        Console.WriteLine("String B is: {0}", strB);
        Console.WriteLine("String C is: {0}", strC);
   
        // Concatenate three different strings 
        // into a single String using the 
        // Concat(String, String, String ) Method
        str = String.Concat(strA, strB, strC);
  
        Console.WriteLine("Concatenated string is: {0}", str);
    }
}

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to illustrate the 
// Concat(string[]) Method
using System;
  
class GFG {
      
    // Main method
    public static void Main()
    {
  
        // array 
        string[] strA = {"Hey, ", "This ","is ", "C# ", "Tutorial."};
  
        // print elements of array
        foreach(string elements in strA)
        {
            Console.WriteLine("Elements of strA array : {0}", elements);
        }
          
        // concatenate the element of array
        // into single string
        // using Concat(string[]) Method
        Console.WriteLine("After Concatenation: {0}",
                                 string.Concat(strA));
    }
}

chevron_right


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



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.