C# join string tutorial shows how to join strings in C# with string.Join. The string.Join method concatenates the elements of a specified array or collection using the provided separator between each element.
last modified July 5, 2023
C# join string tutorial shows how to join strings in C# with string.Join.
The string.Join method concatenates the elements of a specified array or collection using the provided separator between each element.
Join(Char, Object[]) Join(Char, String[]) Join(String, IEnumerable<String>) Join(String, Object[]) Join(String, String[]) Join(Char, String[], Int32, Int32) Join(String, String[], Int32, Int32) Join<T>(Char, IEnumerable<T>) Join<T>(String, IEnumerable<T>)
There are nine overloaded string.Join methods.
The following example joins a list of strings.
Program.cs
var words = new List<string> {“falcon”, “wood”, “cloud”, “cup”, “sky”, “water”};
var text = string.Join(’,’, words); Console.WriteLine(text);
We have a list of words. We join all the words of the list with comma.
$ dotnet run falcon,wood,cloud,cup,sky,water
We can specify which elements to join.
Program.cs
var words = new string[] {“falcon”, “wood”, “cloud”, “cup”, “sky”, “water”};
var text = string.Join(’,’, words, 0, 3); Console.WriteLine(text);
In the example, we join the first three elements of the array.
var text = string.Join(’,’, words, 0, 3);
The third element is the starting index, the fourth is the number of elements to join.
$ dotnet run falcon,wood,cloud
We can join various types of objects in an array.
Program.cs
object[] vals = { 1, 2.3, false, “falcon” };
var text = string.Join("-", vals); Console.WriteLine(text);
We have an array of objects: an integer, a floating point value, a boolean, and a string. We join all these objects with a dash character.
$ dotnet run 1-2.3-False-falcon
In the following example, we use LINQ and the overloaded Join method, which uses IEnumerable.
Program.cs
var words = new List<string> {“sky”, “cup”, “ocean”, “dry”, “tool”, “rust”};
var text = string.Join(",", words.Where(e => e.Length == 3)); Console.WriteLine(text);
We have a list of words. We join all words from the list that have three letters.
$ dotnet run sky,cup,dry
In the following example we join records.
Program.cs
var users = new List<User> { new (“John Doe”, “garderner”), new (“Roger Roe”, “driver”), new (“Lucia Smith”, “teacher”)};
var text = string.Join("\n", users); Console.WriteLine(text);
record User(string name, string occupation);
We have a list of users. We join them with a newline character.
$ dotnet run User { name = John Doe, occupation = garderner } User { name = Roger Roe, occupation = driver } User { name = Lucia Smith, occupation = teacher }
In the next example, we create an ASCII alphabet.
Program.cs
int idx1 = 97; int idx2 = 65;
var text = string.Join(" “, CreateAlphabet()); Console.WriteLine(text);
List<char> CreateAlphabet() {
var vals = new List<char>();
int span1 = idx1 + 25;
for (int i = idx1; i <= span1; i++)
{
vals.Add(Convert.ToChar(i));
}
int span2 = idx2 + 25;
for (int i = idx2; i <= span2; i++)
{
vals.Add(Convert.ToChar(i));
}
return vals;
}
In the ASCII table, there are 25 lower-case letters and 25 upper-case letters. Their indexes start from 97 and 65, respectively.
$ dotnet run a b c d e f g h i j k l m n o p q r s t u v w x y z A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
In this article we have joined strings in C# language.
My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.
List all C# tutorials.