C# Join() Method


The Join() method in C# is used to concatenate all the elements of a string array, using the specified separator between each element.

Syntax

The syntax is as follows -

public static string Join (string separator, string[] val);

Above, separator is the separator that gets included in the string. The val parameter is the array containing elements to concatenate.

Example

Let us now see an example -

 Live Demo

using System;
public class Demo {
   public static void Main(string[] args) {
      string[] strArr = {"One", "Two", "Three", "Four" };
      Console.WriteLine("String Array...");
      foreach(string s in strArr) {
         Console.WriteLine(s);
      }
      string str = string.Join("/", strArr);
      Console.WriteLine("Result (after joining) = " + str);
   }
}

Output

This will produce the following output -

String Array...
One
Two
Three
Four
Result (after joining) = One/Two/Three/Four

Example

Let us now see another example -

 Live Demo

using System;
public class Demo {
   public static void Main(string[] args) {
      string[] strArr = {"AB", "BC", "CD", "DE", "EF", "FG", "GH", "IJ" };
      Console.WriteLine("String Array...");
      foreach(string s in strArr) {
         Console.WriteLine(s);
      }
      string str = string.Join("*", strArr);
      Console.WriteLine("Result (after joining) = " + str);
   }
}

Output

This will produce the following output -

String Array...
AB
BC
CD
DE
EF
FG
GH
IJ
Result (after joining) = AB*BC*CD*DE*EF*FG*GH*IJ

Updated on: 03-Dec-2019

13K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements