Delimited data - a pragmatic approach

In quite a few projects I have to process delimited data in some form, either from App.Config settings or via interfaces to other applications and systems.  Normally people use CSV formatting, separation by the comma character, however that makes life complicated if the data needs to contain commas as well.  Export a Microsoft Excel worksheet as CSV and look at the complex rules about using double quotes to retain commas, thereby introducing issues when a cell contains both a comma and double quotes already.  You end up with double, double quotes. 

Surely there is a simpler way?  YES!  Next time you define your delimited standard, make this simple statement;

the delimiter character is defined as the first character in the string of delimited data

If the actual data contains your standard delimiter, you can swap it for anoth character not contained in any of the data.  If both the 'sender' and 'receiver' adhere to the statement above, the change will be transparent.  Even better, the splitting of the data is reduced to just two of lines for C# or just five lines for VB.NET.  Here are the code samples in C# and VB.NET;

C#

private string[] SplitData(string originalDelimitedData)

{

    if ((originalDelimitedData == null) || (originalDelimitedData.Length < 2)) return new string[0];

    return originalDelimitedData.Substring(1).Split(originalDelimitedData[0]);

}


VB.NET

Private Function SplitData(ByVal originalDelimitedData As String) As String()

  If ((originalDelimitedData Is Nothing) OrElse (originalDelimitedData.Length < 2)) Then

    Dim ret(0) As String

    Return ret

  End If 

  Return originalDelimitedData.Substring(1).Split(originalDelimitedData.Chars(0))

End Function


That's pretty much it, I've created a couple of sample Windows Forms applications (C# and VB.NET for VS.NET 2003) which demo some extremely trivial examples of sample data and you can download it from here.

Print | posted on Friday, January 26, 2007 9:50 AM

Feedback

No comments posted yet.
Title  
Name
Email (never displayed)
Url
Comments   
Please add 1 and 6 and type the answer here: