1. The Comma Separated Value (CSV) file contains data that (as the name suggests) are separated by commas.  In C# they can easily be split into an array by using Data.Split(',').  The problem occurs when data contains a comma.  To overcome this problem, any data that contains comma is enclosed with double quotes ("").

    The following function can split a csv data into array and preserve the double quoted fields.  This is a sample function, you pass a single comma separated line of data.  By default the separator is a comma.

    I have used this in a project and seems to be working.  Please do your own testing before using it and if you encounter any problem or issues let me know, so I can fix it.

            private string[] csvParser(string csv, char separator = ',')
            {
                List <stringparsed = new <string>();
                string[] temp = csv.Split(separator);
                int counter = 0;
                string data = string.Empty;
                while (counter < temp.Length)
                {
                    data = temp[counter].Trim();
                    if (data.Trim().StartsWith("\""))
                    {
                        bool isLast = false;
                        while (!isLast && counter < temp.Length)
                        {
                            data += separator.ToString() + temp[counter + 1];
                            counter++;
                            isLast = (temp[counter].Trim().EndsWith("\""));
                        }
                    }
                    parsed.Add(data);
                    counter++;
                }

                return parsed.ToArray();

            }

    Test data:
    Only One Field
    First, Second
    "First-A, First-B", Second, Third, Fourth, Fifth
    First, "Second-A, Second-B", Third, Fourth, Fifth
    First, "Second-A, Second-B, Second-C", "Third-A, Third-B", Fourth, Fifth
    First, Second, Third, Fourth, "Fifth-A, Fifth-B"
    "First-A, First-B, First-C", "Second-A, Second-B, Second-C", Third, Fourth, "Fifth-A, Fifth-B, Fifth-C"
    "First-A, First-B, First-C", "Second-A, Second-B, Second-C", "Third-A, Third-B, Third-C, Third-D", "Fourth-A, Fourth-B, Fourth-C, Fourth-D, Fourth-E", "Fifth-A, Fifth-B, Fifth-C"
    2

    View comments



  2. For a project I needed to create a string wrapper function.

    The wrapper function had to follow these rules.

    1: Each line has a prefix.

    2: Each line may not exceed 80 charachters, including the prefix.

    3: If the data on a line is less than 80 charachters (including prefix), then buffer it with space characters.

    4: Total number of lines may not exceed 3, so there could only be three lines. We drop the rest of the characters if it exceeds three lines.



    Here is the function I came up with:



    private List <string> DataWrapper(string data, string prefix, int maxLineSize, int numberOfLines, char buffer)

    {

    List <string> result = new List <string> ();

    int dataSize = maxLineSize - prefix.Length ;

    int line = 0;

    int counter = 0;



    //If number of lines is less or equal to zero then just make it same size as the data

    if (numberOfLines <= 0)

    numberOfLines = data.Length;



    if (String.IsNullOrEmpty(data))

    result = null;

    else if (maxLineSize <= 0)

    result = null;

    else

    {

    string temp = data;

    while ((counter < data.Length) && (line < numberOfLines))

    {

    temp = data.Substring(counter);

    if (temp.Length < maxLineSize)

    {

    result.Add(prefix + temp.PadRight(dataSize, buffer));

    }

    else

    {

    temp = (prefix + temp.PadRight(dataSize, buffer).Substring(0, dataSize));

    result.Add(temp);

    }

    line++;

    counter+=dataSize;

    }

    }



    return result;

    }





    //Calling function:

    string[] input = new string [5];

    string prefix = "Lines:";



    input[0] = String.Empty;

    input[1] = "0123456789"; //10;

    input[2] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789012345678901234567890123456789"; //92

    input[3] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789012345678901234567890123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789012345678901234567890123456789"; //184

    input[4] = "TO BE, OR NOT TO BE, THAT IS THE QUESTION: WHETHER 'TIS NOBLER IN THE MIND TO SUFFER THE SLINGS AND ARROWS OF OUTRAGEOUS FORTUNE, OR TO TAKE ARMS AGAINST A SEA OF TROUBLES, AND BY OPPOSING END THEM: TO DIE, TO SLEEP NO MORE; AND BY A SLEEP, TO SAY WE END THE HEART-ACHE, AND THE THOUSAND NATURAL SHOCKS THAT FLESH IS HEIR TO? 'TIS A CONSUMMATION DEVOUTLY TO BE WISHED. TO DIE TO SLEEP, TO SLEEP, PERCHANCE TO DREAM; AYE, THERE'S THE RUB, FOR IN THAT SLEEP OF DEATH, WHAT DREAMS MAY COME, WHEN WE HAVE SHUFFLED OFF THIS MORTAL COIL, MUST GIVE US PAUSE. THERE'S THE RESPECT THAT MAKES CALAMITY OF SO LONG LIFE: FOR WHO WOULD BEAR THE WHIPS AND SCORNS OF TIME, THE OPPRESSOR'S WRONG, THE PROUD MAN'S CONTUMELY, THE PANGS OF DESPISED LOVE, THE LAW’S DELAY, THE INSOLENCE OF OFFICE, AND THE SPURNS THAT PATIENT MERIT OF THE UNWORTHY TAKES, WHEN HE HIMSELF MIGHT HIS QUIETUS MAKE WITH A BARE BODKIN? WHO WOULD FARDELS BEAR, TO GRUNT AND SWEAT UNDER A WEARY LIFE, BUT THAT THE DREAD OF SOMETHING AFTER DEATH, THE UNDISCOVERED COUNTRY, FROM WHOSE BOURN NO TRAVELLER RETURNS, PUZZLES THE WILL, AND MAKES US RATHER BEAR THOSE ILLS WE HAVE, THAN FLY TO OTHERS THAT WE KNOW NOT OF. THUS CONSCIENCE DOES MAKE COWARDS OF US ALL, AND THUS THE NATIVE HUE OF RESOLUTION IS SICKLIED O'ER, WITH THE PALE CAST OF THOUGHT, AND ENTERPRISES OF GREAT PITH AND MOMENT, WITH THIS REGARD THEIR CURRENTS TURN AWRY, AND LOSE THE NAME OF ACTION. SOFT YOU NOW, THE FAIR OPHELIA? NYMPH, IN THY ORISONS BE ALL MY SINS REMEMBERED."; // 1495



    foreach (string line in input)

    {

    string msg = line.ToUpper();

    List <string> result= this.DataWrapper(msg, prefix, 80, 3, ' ');

    if (results != null)

    {

    foreach (string result in results)

    {

    //Handle the result.

    }

    }

    }
    0

    Add a comment

Blog Archive
Topics
Topics
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.