Reading a MultiLine string one line at a time

Hi,

Yesterday I was working on a small page for helping one of my friends with a utility (I will update the small bit as a new Utility section in the site very soon). In the small bit of code I had to take a string convert it to a stream, and then read the stream one line each.

I was working with the stream class conversion of the stream and thought of posting the code. Here is the code for converting a string to a stream.

System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

MemoryStream MStream = new MemoryStream(encoding.GetBytes(stringTobeConverted));
To read the stream one line at a time we need to convert the stream to type StreamReader
StreamReader sr1 = new StreamReader(MStream);
Now we can use the do while loop to read one line at a time from the stream.

do

{

   line = sr1.ReadLine();

   if (line != null)

   {

      // do something here.

   }

}

while (!(line == null));

sr.Close();
That’s we have to do :)
Thanks
Vikram

Share this post   Email it |  digg it! |  reddit! |  bookmark it!

Feedback

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 
Copyright © 2006 - 2008 Vikram Lakhotia