.NET Framework – Convert String Arrays to Comma-Separated Strings and vice versa


To totally unlock this section you need to Log-in


Login

Many systems provide the ability to import information from comma-separated value (CSV) files. These simple, small files of textual data are less sophisticated that modern databases and XML. However, they are often used by legacy systems for which the C# programmer must develop integration software.

When generating CSV information it is common to begin with an array of strings or a collection that can be converted into such an array.

Each element of the array will contain a single string that needs to be entered into the comma-separated file. The conversion is easy to achieve in C# and the .NET framework using the Join method of the String class.

String.Join Method

The static Join method requires two parameters. The first parameter holds a string containing the delimiter that will be placed between each item from the array. The second parameter is the array of strings to be joined. The method returns a string containing the joined items.

The following code shows the Join method in action:

string[] vegetables = new string[] { "Artichoke", "Bean", "Cabbage", "Carrot" };

string joined = String.Join(", ", vegetables);
Console.WriteLine(joined); // Outputs "Artichoke, Bean, Cabbage, Carrot"

Convert Comma-Separated Strings to String Arrays

Many systems provide the ability to store tabular information in comma-separated value (CSV) files. Although CSV files do not provide the level of descriptiveness of XML or the flexibility of database tables, they are small, transportable and of an accepted standard. This is why they are often used for integration between systems.

When processing CSV information it is common to transform the strings of comma-separated values into arrays. Such arrays contain one element for each value a CSV line. With the .NET framework we can use Split method of the String class for this purpose.

String.Split Method

The Split method requires a parameter containing a character array. Each item in the array is a potential delimiting character. Handily, the parameter is a Parameter Array. This allows multiple characters to be included directly in the method without initialising the array beforehand.

The following code splits a comma-separated list:

string fruit = "Apple,Banana,Orange,Strawberry";

string[] split = fruit.Split(',');
foreach (string item in split)
{
Console.WriteLine(item);
}

/* OUTPUT
Apple
Banana
Orange
Strawberry
*/

The following example is similar but this time the delimiting characters are the bar (|) and the comma. Both cause the generation of an additional result in the array.

string fruit = "Apple|Banana,Orange|Strawberry";

string[] split = fruit.Split('|', ',');
foreach (string item in split)
{
Console.WriteLine(item);
}

/* OUTPUT
Apple
Banana
Orange
Strawberry
*/

NOTE: The techniques in this article are not suitable when parsing CSV files where the individual data items can contain the delimiting characters. The article demonstrates the use of the Split method only and will give incorrect results for strings such as:

John Smith,"Jones, Bill",Mel Parker