Friday, 1 April 2016

How to Reading from a string one line at a time in C sharp

public class TopClass
{
    public static void Main()
    {
   
   
        string contents = "My content\r\nHi there";
        using (StringReader reader = new StringReader(contents))
        {
            int lineNo = 0;
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                Console.WriteLine("Line#{0}: {1}", ++lineNo, line);
            }
        }
    }
}
---------------
OUTPUT:-
Line#1: My content
Line#2: Hi there
-----------------------------------

No comments:

Post a Comment