Join Function of String Class in C#


String.Join static method is used to concatenate items in an array or collection, items are separated by using the separator passed as a first parameter to the method.

The table below explains the overloaded static methods of Join in string class.

Name

Description

Join(String, IEnumerable<String>)

Concatenates the items of the IEnumberable<string>, using the specified separator between each item.

Join<T>(String, IEnumerable<T>)

Concatenates the items of the IEnumberable<T>, using the specified separator between each item.

Join(String, Object[])

Concatenates the elements in array of type Object, using the specified separator between each element.

Join(String, String[])

Concatenates the elements in string array, using the specified separator between each element.

Join(String, String[], Int32, Int32)

Concatenates the elements in string array, using specified separator between each element. It has additional parameters to specify the starting index and count of the elements to concatenate.

 

Below is the sample code showing String.Join usage. Please go through the code comments for explanations.

    public class Item
    {
        public string Name;
        public int ID;

        public override string ToString()
        {
            return this.Name + ":" + this.ID;
        }
    }
    
    public class StringJoinSample
    {
        public static void Main()
        {
            // List implements IEnumerable interface.
            // Join the list using " * " as seperator.
            List<string> enumerableStrings = new List<string> { "one", "two", "three", "four", "five" };
            string resultOfEnumerableStringsJoin = string.Join(" * ", enumerableStrings);
            Console.WriteLine(resultOfEnumerableStringsJoin);

            // Join Item types with " * " as seperator.
            // ToString() of Item is used to convert item to string.
            List<Item> enumerableItems = new List<Item> { new Item(){ Name = "jack", ID = 10 }, new Item(){ Name = "john", ID = 20 }};
            string resultOfEnumerableItemsJoin = string.Join(" * ", enumerableItems);
            Console.WriteLine(resultOfEnumerableItemsJoin);

            // Join array of strings using ", " as seperator.
            string[] arrayStrings = new string[] { "jack", "john", "sally" };
            string resultOfStringArrayJoin = string.Join(", ", arrayStrings);
            Console.WriteLine(resultOfStringArrayJoin);

            // Join random number of string by using Params[] overloaded Join.
            string resultStringParamsJoin = string.Join("-", "roger", "alan", "sam", "julia");
            Console.WriteLine(resultStringParamsJoin);

            // Join only 2 elements starting from index 1.
            // b at index 1 and c at index 2 are joined.
            string[] arrayStringsIndex = new string[] { "a", "b", "c", "d", "e"};
            string resultOfStringIndex = string.Join(" = ", arrayStringsIndex, 1, 2);
            Console.WriteLine(resultOfStringIndex);

            // OUTPUT
            //one * two * three * four * five
            //jack:10 * john:20
            //jack, john, sally
            //roger-alan-sam-julia
            //b = c
        }
    }