Create comma separated strings C#?

前端 未结 5 1511
走了就别回头了
走了就别回头了 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:39

    If you're using .Net 4 you can use the overload for string.Join that takes an IEnumerable if you have them in a List, too:

    string.Join(", ", strings);
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-09 01:49

    You can use the string.Join method to do something like string.Join(",", o.Number, o.Id, o.whatever, ...).

    edit: As digEmAll said, string.Join is faster than StringBuilder. They use an external implementation for the string.Join.

    Profiling code (of course run in release without debug symbols):

    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch sw = new Stopwatch();
            string r;
            int iter = 10000;
    
            string[] values = { "a", "b", "c", "d", "a little bit longer please", "one more time" };
    
            sw.Restart();
            for (int i = 0; i < iter; i++)
                r = Program.StringJoin(",", values);
            sw.Stop();
            Console.WriteLine("string.Join ({0} times): {1}ms", iter, sw.ElapsedMilliseconds);
    
            sw.Restart();
            for (int i = 0; i < iter; i++)
                r = Program.StringBuilderAppend(",", values);
            sw.Stop();
            Console.WriteLine("StringBuilder.Append ({0} times): {1}ms", iter, sw.ElapsedMilliseconds);
            Console.ReadLine();
        }
    
        static string StringJoin(string seperator, params string[] values)
        {
            return string.Join(seperator, values);
        }
    
        static string StringBuilderAppend(string seperator, params string[] values)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(values[0]);
            for (int i = 1; i < values.Length; i++)
            {
                builder.Append(seperator);
                builder.Append(values[i]);
            }
            return builder.ToString();
        }
    }
    

    string.Join took 2ms on my machine and StringBuilder.Append 5ms. So there is noteworthy difference. Thanks to digAmAll for the hint.

    0 讨论(0)
  • 2020-12-09 01:52

    If you put all your values in an array, at least you can use string.Join.

    string[] myValues = new string[] { ... };
    string csvString = string.Join(",", myValues);
    

    You can also use the overload of string.Join that takes params string as the second parameter like this:

    string csvString = string.Join(",", value1, value2, value3, ...);
    
    0 讨论(0)
  • 2020-12-09 02:01

    You could override your object's ToString() method:

    public override string ToString ()
    {
        return string.Format ("{0},{1},{2}", this.number, this.id, this.whatever);
    }
    
    0 讨论(0)
提交回复
热议问题