StringBuilder class


System.Text.StringBuilder

The StringBuilder allows you to manipulate an string within the existing string.  You can add, delete and remove strings and characters within a string.

Unlike the methods of the String Class, the StringBuilder lets you perform these actions on the existing string.

Forms of String Builder usage:



Append(string)
Adds the string in the parameter to the existing string.

Dim fullName As new StringBuilder = "Joe Smith"
fullName.Append(" Jr.")



fullName is now Joe Smith Jr.  The original string variable was changed.

Insert(index, string)
Inserts a string into an existing string at the index location.  In this example, we add a middle initial.

fullName.Insert(5, "M. ")

fullName is now Joe M. Smith Jr.

Remove(startIndex, count)
Remove a specified number of characters, as specified by the count parameter.  The startIndex determines where to start removing characters.

fullName.Remove(5, 3)

This removes the middle initial.
fullName is now Joe Smith Jr.

Replace(oldString, newString)

Replace an existing string or substring, within a string, with a new string.


Chars(index)
Get the character at the index location.

Length
Returns the number of characters in the string.

Capacity
Get or set the number of characters for the string.

If capacity is not set, the default is 16 characters.  The capacity is increased when more characters are added to the string.


Example:
Here is an example of using the StringBuilder to format a telephone number.


Dim tel1 As New StringBuilder
tel1.Append("8471234567")
tel1 = 8471235467
tel1.Insert(3, "-")
tel1=847-1234567
tel1.Insert(7, "-")
tel1=847-123-4567

Dim tel2 As New StringBulder
tel2.Append("(847) 123-4567")
tel2.Remove(1,1)

tel2=847) 123-4567
tel2.Remove(4,1)
tel2=847 123-4567

Dim address1 As New StringBuilder
address1.Append("123 Main Avenue Suite 199")
address1.Replace("Avenue", "AVE")
address1.Replace("Suite", "STE")



Use ToString to convert the StringBuilder to a string.

txtPhone.text = tel1.ToString
lblAddress.text = address1.ToString











Copyright © 2006-2019, LQSystems,Inc. All rights reserved.


   Printer Friendly Page