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 preceding section “TextReader and TextWriter” for details.

The StringReader provides methods for reading lines, characters, or blocks of characters from a 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 sequence. 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 than building a string by concatenating a series of values onto a String variable. 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 add 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.

Example program StringWriterReader uses the following code to demonstrate the StringWriter and StringReader classes:

' 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

This code 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, which 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.39.152