C# Reference ยท Strings and Text
C# String.Join() and String.Concat() methods
Reference for C# string.Join() and string.Concat(): join a sequence with a separator, concatenate values, handle nulls, and avoid slow loops of +.
string.Join glues the elements of a sequence into one string, putting a separator between neighbors. string.Concat does the same without a separator, and it also accepts a plain list of values. Both are static methods and both build the result in a single allocation, so they are much cheaper than appending in a loop with +=.
Elements that are null become empty strings. Elements that are not strings are converted with ToString() using the current culture, which in our runner is the invariant culture.
string csv = string.Join(", ", new[] { "red", "green", "blue" }); string joined = string.Concat("a", "b", "c");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
public static string Join(string? separator, IEnumerable<string?> values) public static string Join(string? separator, params string?[] value) public static string Join(string? separator, params object?[] values) public static string Join<T>(string? separator, IEnumerable<T> values) public static string Join(char separator, params string?[] value) public static string Join(string? separator, string?[] value, int startIndex, int count) public static string Concat(params string?[] values) public static string Concat(params object?[] args) public static string Concat(string? str0, string? str1) public static string Concat<T>(IEnumerable<T> values)
There are also char separator versions for the object and generic overloads, and in recent .NET versions params ReadOnlySpan<string?> overloads that avoid allocating an array for the call.
| Name | Description |
|---|---|
separator |
Text (or one char) inserted between elements. null is treated as an empty string. |
values / value |
The elements to join. Non-string elements are converted with ToString(). |
startIndex, count |
For the array overload: the first element to use and how many to take. |
Returns: the joined string. An empty sequence gives "".
Errors / undefined behavior: ArgumentNullException if the sequence itself is null. ArgumentOutOfRangeException if startIndex or count is negative or goes past the end of the array.
Usage notes
- Separator goes between, not after.
Join(", ", ...)never leaves a trailing separator, which is why it beats manual loops. nullelements are not skipped. They become empty strings, soJoin(",", "a", null, "c")givesa,,cwith the separator still present. Filter withWhere(x => x != null)first if you want them gone.- Prefer
Joinover+=in loops. Each+=copies the whole string so far, giving quadratic time.Joinmeasures once and copies once. For incremental building with branching logic, useStringBuilder. - Any
IEnumerable<T>works. Integers, enums, and your own types are joined throughToString(). The sequence is enumerated once. - Format first when you need control.
Join(", ", prices.Select(p => p.ToString("F2", CultureInfo.InvariantCulture)))formats each price before joining. Concatversus+.a + b + con strings compiles toConcat(a, b, c), so both are the same thing. UseConcatfor arrays or sequences.- Opposite operation:
Splitcuts a string at separators.Splitfollowed byJoinis a common way to normalize delimiters. - Joining with a final "and" is not built in; join all but the last element and add the last by hand.