Write Line by Line to StringWriter - CSharp File IO

CSharp examples for File IO:String Reader Writer

Description

Write Line by Line to StringWriter

Demo Code


using System;//from ww  w  .  j a  v a  2 s . c  o m
using System.IO;
using System.Text;

public class Program
{
    public static void Main(string[] args)
    {
        string emily = "this is a test\nthis is a test\nthis is a test\n";
        StringBuilder builder = new StringBuilder();
        StringWriter writer2 = new StringWriter(builder);
        foreach (string item in emily.Split(new char[] { '\n' }))
        {
            writer2.WriteLine(item);
        }

        Console.WriteLine("\nContents of a new writer after writing:\n{0}", writer2.ToString());

        Console.WriteLine("\nContents of internal StringBuilder after using StringWriter to write to it:\n{0}", builder.ToString());
    }
}

Result


Related Tutorials