C# | Join() Method | Set – 1

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 [ ])

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Join(String, Obj [ ]) method
using 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);
    }
}
}

chevron_right


Output:

Value of string  s1 is Hello, Geeks, 12345, 786

String.Join(String, string [ ])

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}
}

chevron_right


Output:

Value of string  s1 is hello/ World /Geeks/ are /  here 

String.Join(String, string [ ], int pos1, int pos2)

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 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.

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to demonstrate the
// Join(String, string [ ], int 
// pos1, int pos2) method
using 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);
          
    }
}
}

chevron_right


Output:

Value of string is jumps-Over-the-Lazy-fox

References:



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.