C# String Join Method

In c#, the string Join method is used to concatenate all the elements of the string array using a specified separator between each element.

 

Following is the pictorial representation of joining two-string elements in an array with a specified separator using the Join() method in the c# programming language.

 

C# String Join() Method Representation Diagram

 

If you observe the above diagram, we are concatenating two array elements ”Suresh” and “Dasari” by using the Join method with defined separator “, ” and the Join method has returned the string like “Suresh, Dasari”.

C# String Join Method Syntax

Following is the syntax of defining a Join method to append or concatenate string array elements using a specified separator in c# programming language.

 

public static string Join(string separator, params string[] values)
public static string Join(string separator, params object[] values)
public static string Join(string separator, IEnumerable values)

If you observe syntaxes, the Join method will append or concatenate all the items of the array or list using the specified separator between each element.

C# String Join Method Example

Following is the example of using the Join() method to append or concatenate all string array elements using a specified separator in the c# programming language.

 

using System;
using System.Collections.Generic;

namespace Tutlane
{
    class Program
    {
        static void Main(string[] args)
        {
           string[] sArr = { "Welcome", "to", "Tutlane" };
           Console.WriteLine("Join with Hypen: {0}", string.Join("-", sArr));
           string[] sArr1 = { "Suresh", "Rohini", "Trishika" };
           Console.WriteLine("Join with Comma: {0}", string.Join(", ", sArr1));
           Console.WriteLine("\nPress Enter Key to Exit..");
           Console.ReadLine();
        }
    }
}

If you observe the above example, we used a string Join() method to append or concatenate all the elements of the string array using a specified separator.

 

When you execute the above c# program, you will get the result below.

 

C# String Join() Method Example Result

 

This is how you can use the string Join() method to append or concatenate all string array elements using a specified separator in c# programming language.