BlackWaspTM
C# Programming
.NET 1.1+

Convert Comma-Separated Strings to String Arrays

Integrating with legacy systems often includes receiving comma-separated lists of string values. To process these values, you may want to convert the comma-separated information to a simple array containing one element for each of the items.

Tabular Data

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

*/
15 February 2008