1. Strings are immutable - meaning the space allocated in memory is fixed. If two strings are concatenated, .net internally creates another string. The string builder is muatble, meaning the space allocated in memory is dynamic.

    If two stringbuilders are concatenated, no new object is created. However, if the capacity is reached, then the stringBuilder automatically doubles its capacity (huge).



    Example:

    Dim szText As System.Text.StringBuilder = New _
    System.Text.StringBuilder("First String")

    MessageBox.Show("Default Capacity : " & szText.Capacity) ''Returns 16
    MessageBox.Show("Max Capacity : " & szText.MaxCapacity)
    ‘'Returns 2147348647

    MessageBox.Show("Default Length : " & szText.Length) ''Returns 12

    szText.Append("Second String")
    MessageBox.Show("Default Capacity : " & szText.Capacity) ''Returns 32

    MessageBox.Show("Max Capacity : " & szText.MaxCapacity)
    ‘'Returns 2147348647
    MessageBox.Show("Default Length : " & szText.Length) ''Returns 25

    We can check the capacity
    If szText.Capacity < 1024 then
    szText.Capacity = 1024
    End If

    Or

    szText.EnsureCapacity (1024)

    We can do string manipulation

    szText.Insert ( 5, "Middle" ) ''Will insert at the position 5
    szText.Replace ( "First", "Begin")

    0

    Add a comment

Blog Archive
Topics
Topics
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.