The System.Text Namespace
Just about every business application you create will eventually require you
to work with strings in some manner. String manipulation is a very common
activity in most applications, but creating, adding to, and deleting from
strings are expensive in terms of processor time and memory.
If you create a variable of type String, you are actually creating a String
object; all base data types in .NET inherit from the base Object class, so
all data types are actually objects. The contents of a String object cannot
be changed once you've placed text into the string梚n order to change its
contents, you must place the return value of a method call into a new
String object. For example, the String class provides a Remove method,
which allows you to remove a portion of a string. However, because the
contents of a String object are immutable, the Remove method doesn't do its
work "in place." Instead, it returns a new string object as its return
value, as shown here:
Dim strNew As String
Dim strOld As String = "This is a test"
' Remove the first four characters of the string.
strNew = strOld.Remove(0, 4)
If you perform many string operations that return strings, you're consuming
memory and processor cycles that you might be able to avoid using a handy
alternative: the StringBuilder class. This class manages a memory buffer
containing your text for you, and it provides methods to modify text "in
place," without requiring you to assign the result to a new string each
time you make a modification to a string. If you are performing a few
simple assignments or concatenations, don't worry about the overhead.
However, if you are building a string inside a loop, the StringBuilder
provides better performance with less overhead. The System.Text namespace
also contains classes for encoding characters into bytes and decoding bytes
into characters. Also, encoders and decoders are available for converting
to and from ASCII and Unicode.
A System.Text Example
This example creates a System.Text.StringBuilder object and provides a
string in the object's constructor. The code retrieves the seventh
character in the string and displays it in a Label control on the page. The
code invokes the Replace method to replace a substring within the string
with another string. Finally, the example inserts a new value into a
specified location within the string. (In this case, the code adds this new
value to the end of the original string.) The example in Listing 4.2 uses
the ToString method of the StringBuilder object to retrieve its contents:
Listing 4.2 Use the StringBuilder for Efficient String Handling
Private Sub StringBuilderExample()
Dim sb As New _
System.Text.StringBuilder(".NET is cool")
' Display the 7th character
lblMsg.Text = sb.Chars(6)
' Replace .NET with Microsoft.NET
sb.Replace(".NET", "Microsoft .NET")
lblMsg.Text = sb.ToString()
sb.Insert(sb.Length, " and fun.")
lblMsg.Text = sb.ToString()
End Sub
What makes this more efficient than using a normal String object is that
when you create a new StringBuilder object, it automatically manages memory
for you by creating a buffer with extra room for growth. If the buffer
needs to grow, the StringBuilder object takes care of the memory management
for you. If you want, you can pass the initial size of the buffer to the
constructor to specify how much space to allocate when a new StringBuilder
object is created.