Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

how to split data in one column which holds a string and Datetime data in a new colum in the Csv file?

within my Csv File i have a column which a string and a datetime type. i would like to separate these data in two columns, one column which holds the string and one which holds the date but converts the datetime type as a string?

this is an example of the current cvs file

SecondaryBinLocation TUI 01/01/20 TUI 01/01/20

1 Answer

Steven Parker
Steven Parker
231,236 Points

Since you didn't post any code, I'll be doing a good bit of guesswork here. I'll start by guessing that since you posted this in the C# topic area that you have read this file into memory in a C# program. I'll also assume that your old format is implemented as a class oldformat, and you are creating an instance of newformat from it.

The value held in a single column can only be of one type, so I assume that this column is already a string that encompasses a code (like "TUI") followed by a date. So all you need is a simple string split:

  string[] parts = oldforamt.mixedcolumn.split();
  newformat.codestring = parts[0];
  newformat.datestring = parts[1];  // since the date was already a string

Thank you for you solution i will be trying these methods. however here is the code below as i have forgotten to insert it:

namespace CsvKP { class Program {

    static void Main(string[] args)
    {
        string currentDirectory = Directory.GetCurrentDirectory();
        DirectoryInfo directory = new DirectoryInfo(currentDirectory);
        var fileName = Path.Combine(directory.FullName, "InventoryReport 02_08_2016.csv");
        var fileContents = ReadFile(fileName);
        string[] fileLines = fileContents.Split(new char[] { 'r', 'n' }, StringSplitOptions.RemoveEmptyEntries);


            Console.WriteLine(fileContents);


    }

            public static string ReadFile(string fileName)
         {
            using (var reader = new StreamReader(fileName))
               {
                return reader.ReadToEnd();
               }
        }



    }
}
Steven Parker
Steven Parker
231,236 Points

It looks like your code was mangled a bit (you can prevent that using the info in the Markdown Cheatsheet pop-up).

But you have a few more steps before you get to what I suggested. You'll likely want to implement something like ReadRow and WriteRow functions that will convert incoming lines into oldformat instances, and output newformat ones. ReadRow will handle separating the lines into the individual column fields.

If you're not already familiar with constructing functions like this, you might enjoy the recently-released course on C# Streams and Data Processing.