3

What is the best way to convert list of Int32 to a string with a separator like ',' in C#?

1

4 Answers 4

7

You can use string.Join:

var intList = new[] { 1, 2, 3, 4, 5 };
var result = string.Join(",", intList);

Edit:

If you are from .NET 4.0, string.Join accepts input parameter as IEnumerable<T>, so you don't need to convert to Array by ToArray.

But if you are in .NET 3.5: like other answers, ToArray should be used.

Sign up to request clarification or add additional context in comments.

Comments

3
string Result = string.Join(",", MyList.ToArray());

Comments

3

Join on a string: String.Join(",", list.ToArray());

Comments

2
string commaSeparated = String.Join(",", Intlist.ToArray());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.