Reading a MultiLine string one line at a time
Posted on 7/27/2007 3:09:42 AM
in #Dot Net Framework
Hi,/P>
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./P>
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./P>
System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();/P> MemoryStream MStream = new MemoryStream(encoding.GetBytes(stringTobeConverted));/o:p>/SPAN>/PRE>To read the stream one line at a time we need to convert the stream to type StreamReader/o:p>/SPAN>/PRE>StreamReader sr1 = new StreamReader(MStream);/o:p>/SPAN>/PRE>Now we can use the do while loop to read one line at a time from the stream./o:p>/SPAN>/PRE>
do/P>
{/P>
/SPAN>line = sr1.ReadLine();/P>
/SPAN>if (line != null)/P>
/SPAN>{/P>
/SPAN>//// do something here./P>
/SPAN>}/P>
}/P>
while (!(line == null));/P> sr.Close();/o:p>/SPAN>/PRE>That’s we have to do :)/o:p>/SPAN>/PRE>Thanks Vikram
|