.NET
C#
string manipulation
arrays
programming

How can I join int to a character-separated string in .NET?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In .NET, the normal way to join integers into a separator-delimited string is string.Join. You usually do not need a manual loop, and you do not need to convert every number yourself unless you want custom formatting.

The Simplest Approach

If you already have an int[], List<int>, or another numeric sequence, string.Join handles the concatenation directly.

csharp
1int[] numbers = { 10, 20, 30, 40 };
2string csv = string.Join(",", numbers);
3
4Console.WriteLine(csv);

The output is:

text
10,20,30,40

Even though the separator is described as a character in everyday language, the API usually accepts a string separator such as ",", "|", or "-".

Joining with Different Separators

A one-character separator is just as straightforward:

csharp
1var ids = new[] { 7, 8, 9 };
2
3Console.WriteLine(string.Join("|", ids));
4Console.WriteLine(string.Join("-", ids));
5Console.WriteLine(string.Join(" ", ids));

This works because string.Join converts each numeric element to text using its standard string representation.

When You Need Custom Formatting

Sometimes plain conversion is not enough. You may want leading zeroes, culture-independent formatting, or hexadecimal output. In that case, format the numbers first and then join the strings.

csharp
1using System.Globalization;
2using System.Linq;
3
4int[] numbers = { 3, 15, 200 };
5string result = string.Join(
6    ";",
7    numbers.Select(n => n.ToString("D4", CultureInfo.InvariantCulture))
8);
9
10Console.WriteLine(result);

The output becomes:

text
0003;0015;0200

This pattern is especially useful when the joined string is consumed by another system that expects a strict representation.

Joining Other Numeric Collections

You are not limited to arrays. string.Join also works well with List<int> and other sequences.

csharp
1List<int> values = new() { 1, 2, 3, 4 };
2string line = string.Join(":", values);
3
4Console.WriteLine(line);

If the values come from LINQ, you can join them directly without forcing a manual loop.

csharp
1var evenSquares = Enumerable.Range(1, 6)
2    .Select(n => n * n)
3    .Where(n => n % 2 == 0);
4
5Console.WriteLine(string.Join(",", evenSquares));

Why Manual Loops Are Usually Unnecessary

A beginner solution often looks like repeated string concatenation in a loop. That works for small inputs, but it is more verbose and easier to get wrong, especially around the final separator.

csharp
1var numbers = new[] { 10, 20, 30 };
2var builder = new System.Text.StringBuilder();
3
4for (int i = 0; i < numbers.Length; i++)
5{
6    if (i > 0)
7    {
8        builder.Append(',');
9    }
10
11    builder.Append(numbers[i]);
12}
13
14Console.WriteLine(builder.ToString());

This is valid code, but string.Join is shorter and clearer for the same job.

Empty Sequences and Null Values

An empty numeric sequence joins to an empty string.

csharp
int[] empty = Array.Empty<int>();
Console.WriteLine($"'{string.Join(",", empty)}'");

That prints two quotes with nothing between them.

When dealing with nullable numbers such as int?[], decide how you want to handle missing values before joining.

csharp
1int?[] values = { 1, null, 3 };
2string result = string.Join(",", values.Where(v => v.HasValue).Select(v => v!.Value));
3
4Console.WriteLine(result);

Filtering first avoids accidental blank fields or inconsistent output.

Common Pitfalls

A common mistake is manually calling ToString in a loop when string.Join would already do the job.

Another pitfall is assuming formatting will be consistent across cultures when the output is exchanged with other systems. If formatting matters, use ToString with an explicit format and CultureInfo.InvariantCulture before joining.

Developers also sometimes forget to handle nullable values or empty collections, which leads to unexpected output.

Finally, avoid building large delimited strings with ad hoc concatenation inside loops unless you have a specific reason. The standard library API is clearer and less error-prone.

Summary

  • Use string.Join to combine integers into a separator-delimited string.
  • A one-character separator is typically passed as a one-character string such as "," or "|".
  • Format numbers first with Select if you need custom output.
  • 'string.Join works with arrays, lists, and LINQ sequences.'
  • Manual loops are usually unnecessary unless you have specialized formatting rules.

Course illustration
Course illustration