Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reading a text file separated by dotted lines in C#

The text in my application’s log file is separated by —- dotted lines.
How can I read each paragraph as a string ? For example : Audit paragraph as string[0], Transaction paragraph as string 1 and so on ….till the end paragraph where there is no dotted line at last.

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There you go:

IEnumerable<string> ReadParagraphs(IEnumerable<string> source)
{
    var output = new List<string>();
    foreach (var line in source)
    {
        if (line != "-------") // you count your `'`
        {
            if (output.Count > 0)
            {
                yield return String.Join(Environment.NewLine, output);
                output.Clear();
            }
        }
        else
        {
            output.Add(line);
        }
    }
    if (output.Count > 0)
    {
        yield return String.Join(Environment.NewLine, output);
    }
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading