StringReader and StringWriter


The StringReader and StringWriter classes let a program read and write text in a string.

These classes are derived from TextReader and TextWriter and inherit the definitions of most of their properties and methods from those classes. See the section “TextReader and TextWriter” earlier in this chapter for details.

The StringReader provides methods for reading lines, characters, or blocks of characters from the string. Its ReadToEnd method returns any of the string that has not already been read. The StringReader class’s constructor takes as a parameter the string that it should process.

The StringWriter class lets an application build a string. It provides methods to write text into the string with or without a new-line character. Its ToString method returns the StringWriter class’s string.

The StringWriter stores its string in an underlying StringBuilder class. StringBuilder is designed to make incrementally building a string more efficient. For example, if an application needs to build a very large string by concatenating a series of long substrings, it may be more efficient to use a StringBuilder rather than adding the strings to a normal String variable. StringWriter provides a simple interface to the StringBuilder class.

The most useful method provided by StringWriter that is not defined by the TextWriter parent class is GetStringBuilder. This method returns a reference to the underlying StringBuilder object that holds the class’s data.

The following code shows a simple example that uses the StringWriter and StringReader classes. It creates a StringWriter object and uses its WriteLine method to add two lines to the string. It then displays the result of the writer’s ToString method. This method returns the writer’s current contents. Next, the program creates a StringReader, passing its constructor the string from which it will read. It closes the StringWriter because it is no longer needed. The code displays the result of the StringReader class’s ReadLine method. Because the StringWriter created the string as two separate lines, this displays only the first line, “The quick brown fox.” Next, the code uses the StringReader class’s ReadToEnd method to read and display the rest of the text, “jumps over the lazy dog.” The code finishes by closing the StringReader.

  ' Use a StringWriter to write into a string. Using string_writer As New StringWriter()     string_writer.WriteLine("The quick brown fox")     string_writer.WriteLine("jumps over the lazy dog.")     MessageBox.Show(string_writer.ToString)     ' Use a StringReader to read from the string.     Using string_reader As New StringReader(string_writer.ToString)         string_writer.Close()         MessageBox.Show(string_reader.ReadLine())         MessageBox.Show(string_reader.ReadToEnd())         string_reader.Close()     End Using End Using 




Visual Basic 2005 with  .NET 3.0 Programmer's Reference
Visual Basic 2005 with .NET 3.0 Programmer's Reference
ISBN: 470137053
EAN: N/A
Year: 2007
Pages: 417

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net