Write to String using StringWriter - CSharp File IO

CSharp examples for File IO:String Reader Writer

Description

Write to String using StringWriter

Demo Code


using System;//from   w  w w  .  j av  a2s . c  o  m
using System.IO;
using System.Text;

public class Program
{
    public static void Main(string[] args)
    {
        string input = "this is a test";
        Console.WriteLine("Original text:\n{0}", input);

        StringReader reader = new StringReader(input);
        StringWriter writer = new StringWriter();

        Console.WriteLine("\nReading the next 5 chars:");
        char[] buffer = new char[] { 'a'};

        writer.Write(buffer);
        Console.WriteLine("\nWriting the chars to a StringWriter:\n{0}", writer.ToString());

        const char SPACE = ' ';
        Console.WriteLine("\nReading the next char:");
        char ch = (char)reader.Read();
        Console.WriteLine("\nWriting the char to a StringWriter:\n");
        if (ch == SPACE)
        {
            writer.Write("[SPACE]");
        }
        else
        {
            writer.Write(ch);
        }
        Console.WriteLine("StringWriter now contains:\n{0}", writer.ToString());


    }
}

Result


Related Tutorials