Visual Basic (VB) StringBuilder

In visual basic, StringBuilder is a class and it is useful to represent a mutable string of characters and it is an object of System.Text namespace.

 

Like string in vb, we can use StringBuilder to create variables to hold any kind of text which is a sequential collection of characters based on our requirements.

 

In visual basic, both string and StringBuilder will represent a sequence of characters and perform the same kind of operations but the only difference is strings are immutable and StringBuilder is mutable.

 

Generally, in visual basic the string object cannot be modified once it created. If any changes made to the string object like add or modify an existing value, then it will simply discard the old instance in memory and create a new instance to hold the new value. In case, if we are doing repeated modifications on the string object, then it will effect the performance of the application. To know more about strings, check strings in vb with examples.

 

To solve this problem, visual basic introduced an alternative called StringBuilder, which is a mutable string class. Mutability means once an instance of the class is created, the same instance will be used to perform any operations like inserting, appending, removing or replacing the characters instead of creating a new instance for every time.

 

In visual basic, the StringBuilder is a dynamic object which will expand a memory dynamically to accommodate the modifications of string instead of creating a new instance in the memory.

 

Following is the pictorial representation of memory allocation for StringBuilder object in visual basic programming language.

 

Visual Basic (VB) StringBuilder Memory Representation Diagram

Visual Basic StringBuilder Declaration and Initialization

As discussed, the StringBuilder class is an object of System.Text namespace so to use StringBuilder in our application, we need to import System.Text namespace.

 

In visual basic, the StringBuilder declaration and initialization will be same like class. Following are the different ways of declaring and initializing the stringbuilder in visual basic programming language.

 

Dim sb As StringBuilder = New StringBuilder()

 

//or

 

Dim sb As StringBuilder = New StringBuilder("Welcome to Tutlane")

If you observe the above code snippet, we created an instance of the StringBuilder class by defining our variable with overloaded constructor methods.

Visual Basic StringBuilder Capacity

As discussed, the StringBuilder is a dynamic object and it will expand dynamically to accommodate the number of characters based on the string modifications. We can also specify an initial capacity of characters, the StringBuilder can hold by passing an int value using one of the overloaded constructors or by using StringBuilder Capacity property.

 

For example, we created a StringBuilder by specifying capacity of 25 characters and appending a string whose length is greater than the capacity of 25 characters. In this case, the new space will be allocated automatically and the capacity of StringBuilder will be doubled.

 

Following is the example of specifying the initial capacity of characters the StringBuilder can hold in visual basic programming language.

 

Dim sb As StringBuilder = New StringBuilder(25)

 

//or

 

Dim sb As StringBuilder = New StringBuilder("Welcome to Tutlane", 25)

 

//or

 

sb.Capacity = 25

In visual basic, whenever the defined capacity of StringBuilder is lesser than the appended string value, then the current capacity of StringBuilder automatically will increase to match the appended string value.

 

The default capacity of StringBuilder is 16 characters and its maximum capacity is more than 12 billion characters.

Visual Basic StringBuilder Methods

Following table lists the important methods of StringBuilder which we can use to modify the contents of StringBuilder.

 

MethodDescription
StringBuilder.Append This method will append the given string value to the end of current StringBuilder.
StringBuilder.AppendFormat It will replaces a format specifier passed in a string with formatted text.
StringBuilder.Insert It inserts a string at the specified index of current StringBuilder.
StringBuilder.Remove It removes a specified number of characters from the current StringBuilder.
StringBuilder.Replace It replaces a specified character at a specified index.

Visual Basic StringBuilder Append Method

The Append method is useful to add or append a string objects to the end of string represented by the StringBuilder.

 

Following is the example of initializing a StringBuilder with some text and appending the required text at the end of string object.

 

Dim sb As StringBuilder = New StringBuilder("Suresh")

sb.Append(", Rohini")

sb.Append(", Trishika")

Console.WriteLine(sb);

// Output: Suresh, Rohini, Trishika

Visual Basic StringBuilder AppendFormat Method

The AppendFormat method is useful to add or append a string objects by formatting into a specified format at the end of the string represented by the StringBuilder.

 

Following is the example of initializing a StringBuilder with some text and appending a formatted text at the end of the string object.

 

Dim amount As Integer = 146

Dim sb As StringBuilder = New StringBuilder("Total")

sb.AppendFormat(": {0:c}", amount)

Console.WriteLine(sb);

// Output is Total: $146.00

Visual Basic StringBuilder Insert Method

The Insert method is useful to insert a string at the specified index position of the current StringBuilder object.

 

Following is the example of initializing a StringBuilder with some text and inserting a string at the specified index position of the StringBuilder object.

 

Dim sb As StringBuilder = New StringBuilder("Welcome Tutlane")

sb.Insert(8, "to ")

Console.WriteLine(sb);

// Output: Welcome to Tutlane

Visual Basic StringBuilder Remove Method

The Remove method is useful to remove a specified number of characters from the current StringBuilder object, starting from the specified index position.

 

Following is the example of removing a specified number of characters from the StringBuilder object, starting from the specified index position.

 

Dim sb As StringBuilder = New StringBuilder("Welcome to Tutlane")

sb.Remove(8, 3)

Console.WriteLine(sb);

// Output: Welcome Tutlane

Visual Basic StringBuilder Replace Method

The Replace method is useful to replace all occurrences of specified string characters in current StringBuilder object with a specified replacement string characters.

 

Following is the example of replacing a specified number of characters from the StringBuilder object, with specified replace characters.

 

Dim sb As StringBuilder = New StringBuilder("Welcome to Tutlane")

sb.Replace("Tutlane", "C#")

Console.WriteLine(sb);

// Output: Welcome to C#

In visual basic, the StringBuilder is having another method called AppendLine() which is useful to add a newline at the end of string.

Visual Basic StringBuilder Example

Following is the example of using StringBuilder to insert or append or replace or to remove a particular string text in visual basic programming language.

 

Imports System.Text

 

Module Module1

    Sub Main()

        Dim sb As StringBuilder = New StringBuilder("Suresh")

        sb.Append(", Rohini")

        sb.Append(", Trishika")

        sb.AppendLine()

        sb.Append("Welcome to Tutlane")

        Console.WriteLine(sb)

        Dim sb1 As StringBuilder = New StringBuilder("Welcome World")

        sb1.Insert(8, "to Tutlane ")

        Console.WriteLine(sb1)

        Dim sb2 As StringBuilder = New StringBuilder("Welcome to Tutlane")

        sb2.Remove(8, 3)

        Console.WriteLine(sb2)

        Dim sb3 As StringBuilder = New StringBuilder("Welcome to Tutlane World")

        sb3.Replace("Tutlane", "C#")

        Console.WriteLine(sb3)

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we imported a System.Text namespace to use StringBuilder in our application and we used a different methods of StringBuilder to make required modifications based on our requirements.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic (VB) StringBuilder Example Result

 

This is how we can use StringBuilder in our applications to make required modifications to the string objects based on our requirements.

Visual Basic Convert StringBuilder to String

In visual basic, we can convert a StringBuilder object to string by calling by StringBuilder.ToString() method.

 

Following is the example of converting a StringBuilder object to string using ToString() method in visual basic programming language.

 

Imports System.Text

 

Module Module1

    Sub Main()

        Dim sb As StringBuilder = New StringBuilder("Suresh")

        sb.Append(", Rohini")

        sb.Append(", Trishika")

        sb.AppendLine()

        sb.Append("Welcome to Tutlane")

        Console.WriteLine(sb.ToString())

        Console.ReadLine()

    End Sub

End Module

If you observe the above example, we are converting StringBuilder object (sb) to string object using sb.ToString() method.

 

When we execute the above visual basic program, we will get the result like as shown below.

 

Visual Basic Convert StringBuilder to String Example Result

 

This is how we can convert StringBuilder object to string based on our requirements.

 

In visual basic, the StringBuilder will offer a better performance than string but we should not automatically replaces a string with StringBuilder whenever we want to manipulate a strings.

Use StringBuilder in Visual Basic

Following are the important points which we needs to remember while using a StringBuilder in visual basic programming language.

 

  • We need to consider using StringBuilder only when we are unknown about the number of changes which will make to a string at design time otherwise StringBuilder will offer a negligible or no performance improvement over string.
  • When we are performing a fixed number of concatenation operations on string, then it’s better to avoid using StringBuilder because it will offer negligible performance.
  • In another case when we expect our application will make a significant number of changes to a string, then we need to use StringBuilder instead of string.

Topics Covered