Create comma separated strings C#?

前端 未结 5 1514
走了就别回头了
走了就别回头了 2020-12-09 01:22

I have an object which holds many values, some of them (not all values from the object) need to be put in a csv string. My approach was this:

string csvStrin         


        
5条回答
  •  粉色の甜心
    2020-12-09 01:43

    Another approach is to use the CommaDelimitedStringCollection class from System.Configuration namespace/assembly. It behaves like a list plus it has an overriden ToString method that returns a comma-separated string.

    Pros - More flexible than an array.

    Cons - You can't pass a string containing a comma.

    CommaDelimitedStringCollection list = new CommaDelimitedStringCollection();
    
    list.AddRange(new string[] { "Huey", "Dewey" });
    list.Add("Louie");
    //list.Add(",");
    
    string s = list.ToString(); //Huey,Dewey,Louie
    

提交回复
热议问题