C# String Join Method

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

 

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

 

C# String Join() Method Representation Diagram

 

If you observe above diagram, we are concatenating two array elements ”Suresh” and “Dasari” by using 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 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<string> values)

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

C# String Join Method Example

Following is the example of using Join() method to append or concatenate all string array elements using specified separator in 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 above example, we used a string Join() method to append or concatenate all the elements of string array using specified separator.

 

When we execute above c# program, we will get the result like as shown below.

 

C# String Join() Method Example Result

 

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