Search code examples
c#arraystokenizedelimiterstreamreader

StreamReader row and line delimiters


I am trying to figure out how to tokenize a StreamReader of a text file. I have been able to separate the lines, but now I am trying to figure out how to break down those lines by a tab delimiter as well. This is what I have so far.

string readContents;
using (StreamReader streamReader = new StreamReader(@"File.txt"))
{
    readContents = streamReader.ReadToEnd();
    string[] lines = readContents.Split('\r');
    foreach (string s in lines)
    {
        Console.WriteLine(s);
    }
}
Console.ReadLine();

Solution

  • string readContents;
    using (StreamReader streamReader = new StreamReader(@"File.txt"))
    {
        readContents = streamReader.ReadToEnd();
        string[] lines = readContents.Split('\r');
        foreach (string s in lines)
        {
             string[] lines2 = s.Split('\t');
             foreach (string s2 in lines2)
             {
                 Console.WriteLine(s2);
             }
        }
    }
    Console.ReadLine();
    

    not really sure if that is what you want, but... it breaks (tab) the already broken (return) lines