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 trialCedric Kehi
1,078 Pointshow 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
231,236 PointsSince 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
Cedric Kehi
1,078 PointsCedric Kehi
1,078 PointsThank 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 {
Steven Parker
231,236 PointsSteven Parker
231,236 PointsIt 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.