0

within my Csv File i have a column which holds 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 too.

After doing some research i noticed that i could use the string.split function which will add a comma after the three character which are TIP and then push the date in a new column. however i do not know to code this process

class Program
{

    static void Main(string[] args)
    {

        string currentDirectory = Directory.GetCurrentDirectory();
        DirectoryInfo directory = new DirectoryInfo(currentDirectory);
        var fileName = Path.Combine(directory.FullName, "Climate 02_08_2016.csv");
        var fileContents = ReadFile(fileName);
        string[] fileLines = fileContents.Split(new char[] { 'r', 'n' }, StringSplitOptions.RemoveEmptyEntries);
        foreach (var line in fileLines)
        {
            Console.WriteLine(line);

        }





    }

            public static string ReadFile(string fileName)
         {
             var column5 = new List<string>();
            using (var reader = new StreamReader(fileName))
               {



                 while (reader.EndOfStream)
                {
                    var splits = reader.ReadLine().Split(';');
                    column5.Add(splits[4]);
                }

                 return reader.ReadToEnd();
               }


        }



    }
user5813072
  • 51
  • 4
  • 15

1 Answers1

1

String.Split splits a string into array of strings based on a delimiter specified. MSDN Reference.

You could use String.Replace method to replace space with a comma.

var result = yourStringVar.Replace(' ', ',')

Here is the full code (slightly simplified).

class Program
{
    static void Main(string[] args)
    {
        var filePath = Path.Combine(Directory.GetCurrentDirectory(), "Climate 02_08_2016.csv");
        var fileContents = ReadFile(filePath);
        foreach (var line in fileContents)
        {
            Console.WriteLine(line);
        }

        Console.WriteLine("Press any key to exit...");
        Console.ReadKey();
    }

    public static IList<string> ReadFile(string fileName)
    {
        var results = new List<string>();

        var lines = File.ReadAllLines(fileName);
        for (var i = 0; i < lines.Length; i++)
        {
            // Skip the line with column names
            if (i == 0)
            {
                continue;
            }

            // Replace space with a comma
            var replace = lines[i].Replace(' ', ',');

            results.Add(replace);
        }

        return results;
    }
}

I get following output:

TUI,01/01/20
Press any key to exit...

Please let me know if this is enough for you to implement your own solution.

Ignas
  • 4,092
  • 17
  • 28
  • Thank you this is taking me towards the solution, however instead of splitting them by space id like to split them with a comma in between which will then separate them and place them in two different column. please let me know if you have any idea. – user5813072 Aug 08 '16 at 10:09