Table of Contents

About

These functions convert from a key-value data string to a comma-separated value string.

Code

C#

///////////////////////////////////////////////////////////////////////////
//    Copyright (C) 2015 Wizardry and Steamworks - License: CC BY 2.0    //
///////////////////////////////////////////////////////////////////////////
/// <summary>
///     Converts key-value data to a comma-separated value string.
/// </summary>
/// <param name="kvp">a key-value data string</param>
/// <returns>a comma-separated value string</returns>
/// <remarks>compliant with RFC 4180</remarks>
public static string wasKeyValueDataToCSV(string kvp)
{
    List<string> csv = new List<string>();
    foreach (string n in kvp.Split('&'))
    {
        foreach (string s in n.Split('='))
        {
            List<char> cell = new List<char>();
            foreach (char i in s)
            {
                cell.Add(i);
                switch (!i.Equals('"'))
                {
                    case false:
                        cell.Add(i);
                        break;
                }
            }
            switch (!cell.Contains('"') && !cell.Contains(' ') && !cell.Contains(',') && !cell.Contains('\r') &&
                !cell.Contains('\n'))
            {
                case false:
                    cell.Insert(0, '"');
                    cell.Add('"');
                    break;
            }
            csv.Add(new string(cell.ToArray()));
        }
    }
    return string.Join(",", csv.ToArray());
}