Using Streams with Strings

Although not often utilized, you can use 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:

Dim sBuilder As New Text.StringBuilderDim sWriter As New StringWriter(sBuilder)For Each name As String In Directory.GetFiles("C:\")    sWriter.WriteLine(name)NextsWriter.Close()Console.WriteLine(sBuilder.ToString)

To read strings, you can use the StringReader object, whose constructor ...

Get Visual Basic 2015 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.