Using Streams with Strings

Although not often utilized, you can take advantage of StringReader and StringWriter for manipulating strings. The following example generates a new StringBuilder and associates it to a new StringWriter. Then it retrieves the list of filenames in the C:\ directory and puts each string into the writer. You notice that, because of the association between the two objects, changes are reflected to the StringBuilder. Try this:

image

To read strings you can use the StringReader object, whose constructor requires a string to be read. To continue with the example, we can read the previously created StringBuilder line by line:

You ...

Get Visual Basic® 2010 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.