29

having a List<int> of integers (for example: 1 - 3 - 4) how can I convert it in a string of this type?

For example, the output should be:

string values = "1,3,4";
0

6 Answers 6

115
var nums = new List<int> {1, 2, 3};
var result = string.Join(", ", nums);
Sign up to request clarification or add additional context in comments.

2 Comments

Note that this doesn't work in .NET 3.5 and earlier (the second parameter must be a string[]). See Albin Sunnanbo's answer.
The most suitable and clean solution.
20
var ints = new List<int>{1,3,4};
var stringsArray = ints.Select(i=>i.ToString()).ToArray();
var values = string.Join(",", stringsArray);

3 Comments

Thank you Albin. E for the revers task, from a string of integer with commas, I need to obtain a List<int>. Luigi
In .NET 4 String.Join has an overload taking an IEnumerable<T> so this can be done without the intermediate array (this overload calls ToString() on each input element): string.Join(",", ints).
@Ciupaz, for the reverse task, use String.Split().
15

Another solution would be the use of Aggregate. This is known to be much slower then the other provided solutions!

var ints = new List<int>{1,2,3,4};
var strings =
            ints.Select(i => i.ToString(CultureInfo.InvariantCulture))
                .Aggregate((s1, s2) => s1 + ", " + s2);

See comments below why you should not use it. Use String.Join or a StringBuilder instead.

3 Comments

-1 Notoriously bad solution to create strings like this (especially when the list is long). String.Join or StringBuilder are the way to go. Sad for Stack Overflow that a low quality OP does not even know how to accept the best answer.
@GertArnold I agree with you. Just wanted to show another solution and forgot to mention anything performance. I tried to delete this answer but I can't. I will update the answer to not use it.
Perfect, could be fine this wau. Thank you
0
public static string ToCommaString(this List<int> list)
{
    if (list.Count <= 0)
        return ("");
    if (list.Count == 1)
        return (list[0].ToString());
    System.Text.StringBuilder sb = new System.Text.StringBuilder(list[0].ToString());
    for (int x = 1; x < list.Count; x++)
        sb.Append("," + list[x].ToString());
    return (sb.ToString());
}

public static List<int> CommaStringToIntList(this string _s)
{
    string[] ss = _s.Split(',');
    List<int> list = new List<int>();
    foreach (string s in ss)
        list.Add(Int32.Parse(s));
    return (list);
}

Usage:

String s = "1,2,3,4";
List<int> list = s.CommaStringToIntList();
list.Add(5);
s = list.ToCommaString();
s += ",6";
list = s.CommaStringToIntList();

1 Comment

Why did you post this alternate solution, when some very compact solutions using string.Join() had already been given?
0

You can use the delegates for the same

List<int> intList = new List<int>( new int[] {20,22,1,5,1,55,3,10,30});
string intStringList = string.Join(",", intList.ConvertAll<string>(delegate (int i) { return i.ToString(); });

Comments

0

Use the Stringify.Library nuget package

Example 1 (Default delimiter is implicitly taken as comma)

string values = "1,3,4";
var output = new StringConverter().ConvertFrom<List<int>>(values);

Example 2 (Specifying the delimiter explicitly)

string values = "1 ; 3; 4";
var output = new StringConverter().ConvertFrom<List<int>>(values), new ConverterOptions { Delimiter = ';' });

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.