Convert a list to a string in C#



  • 6 Answer(s)

    Try to do this solution:

    string combindedString = string.Join( ",", myList.ToArray() );
    

    Also  can change  “,” along  split the elements in the list.

    Answered on December 20, 2018.
    Add Comment

    Consider, the result of calling ToString on each element of the list.

    var result = string.Join(",", list.ToArray());
    
    Answered on December 20, 2018.
    Add Comment

    It is lightly complicated then use LINQ

    var result = myList.Aggregate((total, part) => total + "(" + part.ToLower() + ")");
    

    It will take [“A”, “B”, “C”] and produce “(a)(b)(c)”

    Answered on December 20, 2018.
    Add Comment

    String.Join(" ", myList) or String.Join(" ", myList.ToArray()). The first argument is the separator between the substrings.

    var myList = new List<String> { "foo","bar","baz"};
    Console.WriteLine(String.Join("-", myList)); // prints "foo-bar-baz"
    

    Depending on your version of .NET you might need to use ToArray() on the list first..

    Answered on January 15, 2019.
    Add Comment

    You could use string.Join:

    List<string> list = new List<string>()
    {
        "Red",
        "Blue",
        "Green"
    };
    
    string output = string.Join(Environment.NewLine, list.ToArray());    
    Console.Write(output);

    The result would be:

    Red    
    Blue    
    Green

    As an alternative to Environment.NewLine, you can replace it with a string based line-separator of your choosing.

    Answered on January 15, 2019.
    Add Comment
    List<string> employees = new List<string>();
    employees.Add("e1");
    employees.Add("e2");
    employees.Add("e3");
    
    string employeesString = "'" + employees.Aggregate((x, y) => x + "','" + y) + "'";
    Console.WriteLine(employeesString);
    Console.ReadLine();
    Answered on January 28, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.