Open In App

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)

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

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)

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 

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)

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

Output:

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

References:


Article Tags :
C#