C# String Join

Summary: in this tutorial, you will learn how to use the C# string Join() method to concatenate a collection of elements using a specified separator.

Introduction to the C# String Join() method

The C# string Join() method allows you to concatenate a collection of elements into a single string using a specified separator.

Here’s the syntax of the String Join() method:

public static string Join<T> (
    string? separator, 
    IEnumerable<T> values
);Code language: C# (cs)

In this syntax:

  • T is the type of the members of values.
  • separator is a string used as a separator between elements in the values collection.
  • values is a collection of elements to concatenate. It can be any collection that implements the IEnumerable<T> interface.

The Join() method returns a single string that consists of elements of values separated by the separator string.

If the values collection has no elements, the Join() method returns an empty string i.e. String.Empty.

C# String Join() method examples

Let’s take some examples of using the Join() method.

1) Using the string Join() method to concatenate an array of strings

The following example illustrates how to use the Join() method to concatenate strings of an array using a comma as a separator:

using static System.Console;

string[] fruits = { 
    "apple", 
    "banana", 
    "orange", 
    "mango", 
};

var result = string.Join(",", fruits);

WriteLine(result);Code language: C# (cs)

Output:

apple,banana,orange,mangoCode language: C# (cs)

2) Using the string Join() method with a space as a separator

The following example illustrates how to use the Join() method to concatenate a list of strings using a space as the separator:

using static System.Console;

var names = new List<string> { "John", "Jane", "Bob", "Alice" };
var result = string.Join(" ", names);

WriteLine(result);Code language: C# (cs)

Output:

John Jane Bob AliceCode language: C# (cs)

3) Using the string Join() method to concatenate an array of characters

The following example shows how to use the Join() method to concatenate an array of characters into a single string:

using static System.Console;

char[] characters = { 'H', 'e', 'l', 'l', 'o' };
var result = string.Join("", characters);

WriteLine(result);Code language: C# (cs)

Output:

HelloCode language: C# (cs)

4) Using the string join() method to concatenate an array of objects

If you pass an array of objects to the Join() method, it’ll use the string representation of the object to concatenate. For example:

using static System.Console;

var members = new List<Person>() {
    new Person("John", "Doe"),
    new Person("Jane", "Doe")
};

var result = string.Join(",", members);
WriteLine(result);

public class Person
{
    public string FirstName { get; set;}
    public string LastName { get; set;}

    public Person(string firstName, string lastName)
    {
        FirstName = firstName;
        LastName = lastName;
    }

    public override string ToString() => $"{FirstName} {LastName}";
}Code language: C# (cs)

Output:

John Doe,Jane DoeCode language: C# (cs)

Selecting elements to concatenate

The following overload of the Join() method to concatenate elements of a collection selectively:

public static string Join (
    string? separator, 
    string?[] value, 
    int startIndex, 
    int count
);Code language: C# (cs)

In this syntax:

  • startIndex is the first element in the values collection to concatenate.
  • count specifies the number of elements from the value array to concatenate, starting with the element in the startIndex.

The Join() method returns a single string that consists of count elements of the value array starting at startIndex separated by the separator.

The following example illustrates how to use the Join() method to concatenate two elements starting from the second element in the colors array:

using static System.Console;


var colors = new string[] { "red", "green", "blue", "yellow" };

var result = string.Join(
    separator: ",",
    value: colors,
    startIndex: 1,
    count: 2
);

WriteLine(result);Code language: C# (cs)

Output:

green,blueCode language: C# (cs)

Summary

  • Use the C# string Join() method to concatenate the elements of a collection into a string using a specified separator.
Was this tutorial helpful ?