String.Join()

How many times did you need to join a collection of strings (to create an CSV file, for instance) and ended up doing something like this:

string GetLines(string[] values){
    StringBuilder result = new StringBuilder();
    int i;
    for(i = 0; i < values.Length - 1; i++)
    {
        result.AppendFormat("{0},", values[i]);
    }
    result.Append(values[i]);
    return result.String();
}

There is a method for that! It is called Join and it is a static method in the String class:

String.Join(string separator, string[] values)

P.S: I know I should have read the documentation of the .NET Framework. But I haven’t done that before for the String class. I wonder how many times I have writen a method like this since I started programming.


About me


My name is Rafa Vargas. I'm an undergraduate student of Computer Science at University of Seville, Spain. I am mainly interested in computer security, usability and the business of software.

Click here to read the full story.

Twitter subscription

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 1,022 other followers

Archives


Follow

Get every new post delivered to your Inbox.

Join 1,022 other followers