StringBuilder Class in VB.NET: Part 1

In this article I will explain you about the StringBuilder Class in VB.NET.
  • 2123

A StringBuilder object is not a string but rather an auxiliary object used for manipulating characters. It contains a buffer, typically initialized with a string but usually larger than that string. This buffer can be manipulated in place without creating a new string: You can insert, append, remove, and replace characters. When you're done manipulating the characters, use StringBuilder's ToString method to extract the finished string from it.

Both String and StringBuilder contain Unicode characters of type Char and support an indexer that returns Char. Because the String class is immutable, its indexer is read-only, but the StringBuilder indexer is readable/writeable. Example given below illustrates how to manipulate characters with the StringBuilder class and then place the characters into a String object.

Example of StringBuilder Class Manipulation

    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Namespace StringBuilderClass

        Class Program
            Private Shared Sub Main(ByVal args As String())
                Dim MyName As [String]
                Dim MyOtherName As New StringBuilder("Hello")
                MyOtherName.Remove(2, 3)
                ' produces "He"
                MyOtherName.Insert(2, "lp")
                ' produces "Help"
                MyOtherName.Replace("l"c, "a"c)
                ' produces "Heap"
                MyName = MyOtherName.ToString()
            End Sub

You can use either String or StringBuilder for string operations. Deciding which to use depends on the frequency of the string modification. If you modify the string frequently-in operations like reassigning, appending, removing, and replacing some characters-you should choose the StringBuilder class. However, if your methods do not change the string value much, registering the String class to the .NET Framework's internal string table will save space in memory for duplicate strings. The framework also provides you faster access to string literal values stored in variables. The .NET Framework automatically handles these operations behind the scenes for you.

For example, whenever you modify a string in the String class, the methods of the String class returns a new string as the result. Creating many String objects might degrade the performance of your program. You can avoid creating a new instance of a string by using the StringBuilder class.

Let's say you want to concatenate two strings. Here is the traditional way using the System.String class:

    Dim str1 As String = "I like "
    Dim str2 As String = "Soccer"
    Dim strConcat As String = String.Concat(str1, str2)

The value of strConcat is I like Soccer. You can use the StringBuilder class and its Append method to do the same thing:

MyStrBuilder As New StringBuilder("I like ")
    Dim newStr As [String] = "Soccer"

The value of MyStrBuilder is I like Soccer.

You can use the String and StringBuilder classes whenever required and also write auxilliary functions for both classes by providing static helper functions. For example, by default StringBuilder does not provide the IndexOf member function, which does a linear search for a character in a string. But the example given below shows how you can create your own custom IndexOf function.

Example of StringBuilder IndexOf

    // Example IndexOf function for StringBuilder class
    Imports System.Text
        Public Class App
            Public Shared Function sbIndexOf(ByVal sb As StringBuilder, ByVal ch As Char) As Integer
intVal1 As Int32 = -1
                While System.Threading.Interlocked.Increment(intVal1) < sb.Length
                    If sb(intVal1) = ch Then
                    End If
            End Function

            ' string is an alias for System.String in the .NET Framework.
            Public Shared Sub Main(ByVal args As String())
                Dim sb1 As New StringBuilder("Hello There")
                Console.Write("{0}", App.sbIndexOf(sb1, "o"c))
            End Sub


Hope this article would have helped you in understanding the StringBuilder Class in VB.NET. Remaining part of this article is on my next article.


More Articles

© 2020 DotNetHeaven. All rights reserved.