StringReader, StringBuilder, and StringWriter

This section talks about string I/O. This concept at first might sound strange—after all, we’ve been talking about file I/O until this point. But it is often very convenient to treat strings as files and write data to them just as you write data to a file.

This section covers three important classes: StringReader, StringBuilder, and StringWriter. The StringReader class implements a TextReader that reads from a string.

The StringBuilder class represents a custom constructor for strings and is used in conjunction with the String class to carry out modifications upon strings. It is convenient for situations in which it is desirable to modify a string, perhaps by removing, replacing, or inserting characters, without creating a new string subsequent to each modification. The methods contained within this class do not return a new StringBuilder object unless specified otherwise.

Equals has not been overridden in StringBuilder (as it is in String) because the contract with Equals() states that if a.Equals(b) is true it will always be true. Because the string of characters contained in a StringBuilder can change, it is not possible to implement Equals() based on the content of a StringBuilder.

The StringWriter class writes characters to a string.

The example I created to demonstrate these three classes isn’t necessarily practical, but it clearly shows how to use these classes. The example enables users to enter two sets of string information by providing two editable text boxes. These sets of text data are then read one character at a time to produce a resultant string. To show the difference between the two strings (in addition to the distinct possibility that the data will be different), the font color alternates.

Figure 5.10 shows the example in action. The two sets of text data have been read and combined into one string. Listing 5.11 shows the code that makes it work.

Figure 5.10. Each text box’s contents are read one character at a time and written out.


Listing 5.11. Combining Two Strings into One
Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Dim reader1 As New StringReader(TextBox1.Text) 
    Dim reader2 As New StringReader(TxtBox2.Text) 

    Label1.Text = “”  
    Dim builder As New StringBuilder(“”) 

    Try 
        Dim strDataOut As New String(“”) 

        While reader1.Peek() <> -1 Or reader2.Peek() <> -1 
            strDataOut = “” 

            If reader1.Peek() <> -1 Then 
                strDataOut = strDataOut + Chr(reader1.Read()) 
            End If 

            If reader2.Peek() <> -1 Then 
                strDataOut = strDataOut + (“<font color=red>” + 
                    Chr(reader2.Read()) + “</font>”) 
            End If 

            builder.Append(strDataOut) 

        End While 

        builder.Append(“ File was written successfully.”) 

        Dim i As Integer 
        For i = 0 To builder.Length - 1 
            Label1.Text = Label1.Text + builder.Chars(i) 
        Next 

    Catch ex As Exception 
        Label1.Text = ex.Message 
    End Try 

End Sub 
Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) 
    Dim reader1 As New StringReader(TextBox1.Text) 
    Dim reader2 As New StringReader(TextBox2.Text) 

    Label2.Text = “” 
    Dim builder As New StringBuilder(“”) 
    Dim writer As New StringWriter(builder) 

    Try 
        Dim strDataOut As New String(“”) 

        While reader1.Peek() <> -1 Or reader2.Peek() <> -1 
            strDataOut = “” 

            If reader1.Peek() <> -1 Then 
                strDataOut = strDataOut + Chr(reader1.Read()) 
            End If 

            If reader2.Peek() <> -1 Then 
                strDataOut = strDataOut + (“<font color=red>” + 
                     Chr(reader2.Read()) + “</font>”) 
            End If  

            writer.Write(strDataOut) 

        End While 

        writer.Write(“ File was written successfully.”) 

        Dim i As Integer 
        For i = 0 To builder.Length - 1 
            Label2.Text = Label2.Text + builder.Chars(i) 
        Next 

    Catch ex As Exception 
        Label2.Text = ex.Message 
    End Try 

End Sub
					

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

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