StringBuilder class in vb.net
StringBuilder is mostly used when you want to modify string without creating a new object. It is mutable object.
StringBuilder class is located in System.Text namespace and it's allow you to expand the string without creating a new object. StringBuilder object is mutable. It is reference type. It is mostly used when you want to modify string without creating a new object because in many situation you need to perform repeated modification if you do not use string builder class then you need to create a new object. String is also reference type but it acts like a value type.
The important members of StringBuilder are listed below which can be used to modify the contents of a StringBuilder are listed below.
Method |
Explanation |
Append( ) |
Append method that appends a typed object add to the end of the current StringBuilder. |
AppendFormat( ) |
Appends a formatted string, which contains zero or more format specifies, to this instance. This method add string to the end of String Builder and it specify the standard format for string. |
Insert( ) |
This method use to specify the position where you want to add a string. |
Remove( ) |
This method used to removes the specified characters from the StringBuilder. It's position start from zero based index. |
Replace( ) |
This method use to replace a string with another string. |
Code
Imports System.Text
Module Module1
Sub Main()
Dim SBuilder1 As Text.StringBuilder = Nothing
Dim SBuilder2 As New Text.StringBuilder("My name is Clark")
SBuilder1 = New Text.StringBuilder("Hello... ")
Console.WriteLine(SBuilder1.ToString())
SBuilder1.Append("my name is ")
SBuilder1.Append("Clark")
Console.WriteLine(SBuilder1.ToString())
SBuilder1.AppendFormat(" on {0:d}", DateTime.Today)
Console.WriteLine(SBuilder1.ToString())
SBuilder2.Replace("Clark", "Husey")
Console.WriteLine(SBuilder2.ToString())
SBuilder2.Insert(11, "MR.")
Console.WriteLine(SBuilder2.ToString())
Console.ReadLine()
End Sub
End Module
Output
