StringWriter implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder. : StringWriter « File Stream « C# / C Sharp






StringWriter implements a TextWriter for writing information to a string. The information is stored in an underlying StringBuilder.

 

using System;
using System.IO;

class StringRW
{
    static void Main()
    {
        string textReaderText = "this is a test";

        Console.WriteLine("Original text:\n\n{0}", textReaderText);

        string aLine, aParagraph = null;
        StringReader strReader = new StringReader(textReaderText);
        while(true)
        {
            aLine = strReader.ReadLine();
            if(aLine != null)
            {
                aParagraph = aParagraph + aLine + " ";
            }
            else
            {
                aParagraph = aParagraph + "\n";
                break;
            }
        }
        Console.WriteLine("Modified text:\n\n{0}", aParagraph);

        int intCharacter;
        char convertedCharacter;
        StringWriter strWriter = new StringWriter();
        strReader = new StringReader(aParagraph);
        while(true)
        {
            intCharacter = strReader.Read();

            if(intCharacter == -1) break;

            convertedCharacter = Convert.ToChar(intCharacter);
            if(convertedCharacter == '.')
            {
                strWriter.Write(".\n\n");

                strReader.Read();
                strReader.Read();
            }
            else
            {
                strWriter.Write(convertedCharacter);
            }
        }
        Console.WriteLine("\nOriginal text:\n\n{0}", strWriter.ToString());
    }
}

   
  








Related examples in the same category

1.Use StringWriter to write string
2.The hex dump program.
3.Initializes a new instance of the StringWriter class with the specified format control.
4.Initializes a new instance of the StringWriter class that writes to the specified StringBuilder.
5.Initializes a new instance of the StringWriter class.