C# | Join() Method | Set - 1 - GeeksforGeeks

05 Nov, 2019
In C#, Join() is a string method. This method is used to concatenates the members of a collection or the elements of the specified array, using the specified separator between each member or element. This method can be overloaded by passing different parameters to it. There are total 5 methods in the overload list of the Join() method in which 3 are discussed in this article and remaining 2 will be discussed in C# | Join() Method | Set – 2.
String.Join(String, Obj [ ])
String.Join(String, string [ ])
String.Join(String, string [ ], int pos1, int pos2)
String.Join(String, IEnumerable<String>)
String.Join<T>(String, IEnumerable<T>)
This method is used to concatenate the elements of an object array with the help of a separator between each element of the object array.
Syntax:
public static string Join(string separator, params obj[] array)

Parameters: This method takes two parameters, One of them is the separator of the type System.Sting which is used to define a separator according to the user’s choice. Another parameter is an array of type System.Object[] which contains the elements to be concatenated.
Return Type: The return type of this method is System.String.
Exception: This method can give ArgumentNullException if the array is null.
Example: In the below code, first an array of objects is created and then it is passed to the join method along with the separator to be used, here ‘, ‘ comma separator is used and after method’s return, the string is taken as the output.
// C# program to demonstrate the// Join(String, Obj [ ]) methodusing System;namespace ConsoleApplication1 {  class Geeks {          // Main Method    static void Main(string[] args)    {          // Creating an object array        // Here, It is consist of four         // elements only        object[] array = {"Hello", "Geeks", 12345, 786};          // Using Join method        // Here separator is ', '( comma )        string s1 = string.Join(", ", array);          // Finally after joining process gets over        // Getting the output of value of string s1        Console.WriteLine("Value of string  s1 is " + s1);    }}}
Output:
Value of string  s1 is Hello, Geeks, 12345, 786
This method is used to concatenate the elements of a String array with the help of a user-specified separator between each element of the string array.
Syntax:
public static string Join(string separator, params string[ ] array)

Parameters: This method takes two parameters, One of them is the separator of the type System.Sting which is used to define a separator according to the user’s choice. Another parameter is an array of type System.String[] which contains the elements to be concatenated.
Return Type: The return type of this method is System.String.
Exception: This method can give ArgumentNullException if the array is null.
Example:In below code, first an array of strings is created and is passed to the join method along with the separator to be used, here ‘/’ slash separator is used and after method’s return, the string is print as the output.
// C# program to demonstrate the// Join(String, String [])using System;namespace ConsoleApplication2 {  class Geeks {          // Main Method    static void Main(string[] args)    {          // Creating a string array        // Here It contains five         // elements only        string[] array = {"hello", " World ",                 "Geeks", " are ", "  here " };          // Using Join method        // Here separator used is '/'( slash )        string s1 = string.Join("/", array);          // Finally after join method        // Getting the output of value of string s1        Console.WriteLine("Value of string  s1 is " + s1);    }}}
Output:
Value of string  s1 is hello/ World /Geeks/ are /  here 
This method is used to concatenate the elements of a String array between the specified positions with the help of a user-defined separator between each element of the array.
Syntax:
public static string Join(string separator, params string[] array, int pos1, int pos2)

Parameters: This method takes four parameters, first one is the separator of the type System.Sting which is used to define a separator according to the user’s choice. Second parameter is an array of type System.String[] which contains the elements to be concatenated. Third parameter is pos1 of type System.Int32 which is the first element in array to be used and important point to remember about this parameter is that it used zero-based index. Fourth parameter is pos2 of type System.Int32 which is used to specify the number of elements of array to be used and it always starts from 1 i.e it counts the pos1’value as its first element.
Return Type: The return type of this method is System.String. Also it can return of type String.Empty if value of pos2 is zero.
Exceptions: This method can give ArgumentNullException if the array is null. ArgumentOutOfRangeException can be given if the pos1 or pos2 is less than zero or pos1 + pos2 is greater than the number of elements in array and also this method can give OutOfMemoryException.
Example: In below code, an array of strings is created and suppose that user wants to join the strings from position index 2 and want to cover five elements.
// C# program to demonstrate the// Join(String, string [ ], int // pos1, int pos2) methodusing System;namespace ConsoleApplication3 {  class Geeks {          // Main Method    static void Main(string[] args)    {          // Creating a string array        string[] array = {"lazy", "dog", "jumps", "Over",                                  "the", "Lazy", "fox" };          // Using Join method        // Here separator used is '-'( hiphen )        // from index 2 and covers upto 5         // elements from index 2        string s1 = string.Join("-", array, 2, 5);          // Finally after joining process gets over        // Getting the output of value of string s1        Console.WriteLine("Value of string is " + s1);              }}}
Output:
Value of string is jumps-Over-the-Lazy-fox
References:
https://msdn.microsoft.com/en-us/library/tk0xe5h0(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/57a79xd0(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/tk0xe5h0(v=vs.110).aspx
Akanksha_Rai
CSharp-method
CSharp-string
C#
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
C# | Method Overriding
Difference between Ref and Out keywords in C#
C# | Delegates
Top 50 C# Interview Questions & Answers
Introduction to .NET Framework
C# | Constructors
Extension Method in C#
C# | Class and Object
C# | Abstract Classes
Common Language Runtime (CLR) in C#