VB.Net/Language Basics/StringBuilder

Материал из VB Эксперт
Перейти к: навигация, поиск

Append String to StringBuilder

Imports System
Imports System.Text 
Public Class MainClass
    Shared Sub Main(ByVal args As String())
             Dim s1 As String = "One,Two,Three"
             Const Space As Char = " "c
             Const Comma As Char = ","c
             Dim delimiters( ) As Char = {Space, Comma}
             Dim output As New StringBuilder( )
             Dim ctr As Integer = 0
             Dim resultArray As String( ) = s1.Split(delimiters)
             Dim subString As String
             For Each subString In resultArray
                 ctr = ctr + 1
                 output.AppendFormat("{0} : {1}" & _
                    Environment.NewLine, ctr, subString)
             Next subString
             Console.WriteLine(output)
    End Sub
End Class


Demonstrating StringBuilder class constructors

Imports System
Imports System.Text
Public Class MainClass
   Shared Sub Main()

      Dim buffer1, buffer2, buffer3 As StringBuilder
      Dim quotes As Char = ChrW(34)
      buffer1 = New StringBuilder()
      buffer2 = New StringBuilder(10)
      buffer3 = New StringBuilder("hello")
      Console.WriteLine("buffer1 = " & quotes & buffer1.ToString() & _
         quotes)
      Console.WriteLine("buffer2 = " & quotes & _
         buffer2.ToString() & quotes)
      Console.WriteLine("buffer3 = " & quotes & _
         buffer3.ToString() & quotes)
   End Sub " Main
End Class


Insert and Remove of the StringBuilder class

Imports System
Imports System.Text
Public Class MainClass
   Shared Sub Main()
      Dim objectValue As Object = "hello"
      Dim stringValue As String = "good bye"
      Dim characterArray As Char() = {"a"c, "b"c, "c"c, _
         "d"c, "e"c, "f"c}
      Dim booleanValue As Boolean = True
      Dim characterValue As Char = "K"c
      Dim integerValue As Integer = 7
      Dim longValue As Long = 1123456
      Dim singleValue As Single = 2234.4325
      Dim doubleValue As Double = 33.3
      Dim buffer As StringBuilder = New StringBuilder()
      " insert values into buffer
      buffer.Insert(0, objectValue)
      buffer.Insert(0, "  ")
      buffer.Insert(0, stringValue)
      buffer.Insert(0, "  ")
      buffer.Insert(0, characterArray)
      buffer.Insert(0, "  ")
      buffer.Insert(0, booleanValue)
      buffer.Insert(0, "  ")
      buffer.Insert(0, characterValue)
      buffer.Insert(0, "  ")
      buffer.Insert(0, integerValue)
      buffer.Insert(0, "  ")
      buffer.Insert(0, longValue)
      buffer.Insert(0, "  ")
      buffer.Insert(0, singleValue)
      buffer.Insert(0, "  ")
      buffer.Insert(0, doubleValue)
      buffer.Insert(0, "  ")
      Console.WriteLine("buffer after inserts:" & vbCrLf & _
         buffer.ToString() )
      buffer.Remove(12, 1)
      buffer.Remove(2, 4) 
      Console.WriteLine("buffer after Removes:" & vbCrLf & _
         buffer.ToString())
  End Sub " Main
End Class


StringBuilder Append methods

Imports System
Imports System.Text
Public Class MainClass
   Shared Sub Main()
      Dim objectValue As Object = "hello"
      Dim stringValue As String = "good bye"
      Dim characterArray As Char() = {"a"c, "b"c, "c"c, _
         "d"c, "e"c, "f"c}
      Dim booleanValue As Boolean = True
      Dim characterValue As Char = "Z"c
      Dim integerValue As Integer = 2347
      Dim longValue As Long = 100
      Dim singleValue As Single = 2.52
      Dim doubleValue As Double = 3.3
      Dim buffer As StringBuilder = New StringBuilder()
      " use method Append to append values to buffer
      buffer.Append(objectValue)
      buffer.Append("  ")
      buffer.Append(stringValue)
      buffer.Append("  ")
      buffer.Append(characterArray)
      buffer.Append("  ")
      buffer.Append(characterArray, 0, 3)
      buffer.Append("  ")
      buffer.Append(booleanValue)
      buffer.Append("  ")
      buffer.Append(characterValue)
      buffer.Append("  ")
      buffer.Append(integerValue)
      buffer.Append("  ")
      buffer.Append(longValue)
      buffer.Append("  ")
      buffer.Append(singleValue)
      buffer.Append("  ")
      buffer.Append(doubleValue)
      Console.WriteLine("buffer = " & buffer.ToString())   
  End Sub " Main
End Class


StringBuilder: Char and Replace

Imports System.Text

Module Module1
    Sub Main()
        Dim SB As StringBuilder = New StringBuilder(26)
        SB.Append("ABCDEFGHIJKLMNOPQRSTUVWYZ")
        SB.Chars(4) = "e"
        SB.Chars(0) = "a"
        SB.Replace("aBCDe", "ABCDE")

        Console.WriteLine("Ending StringBuilder: " & SB.ToString())
    End Sub
End Module


StringBuilder Length and Capacity

Imports System
Imports System.Text
Public Class MainClass
   Shared Sub Main()
      Dim buffer As StringBuilder = _
         New StringBuilder("Hello, how are you?")
      Dim output As String = "buffer = " & buffer.ToString & _
         vbCrLf & "Length = " & buffer.Length & vbCrLf & _
         "Capacity = " & buffer.Capacity
      buffer.EnsureCapacity(75)
      Console.WriteLine("New capacity = " & _
         buffer.Capacity)
      buffer.Length = 10
      
      Console.WriteLine("New Length = " & _
         buffer.Length & vbCrLf & "buffer = ")
   End Sub " Main
End Class


StringBuilder method AppendFormat

Imports System
Imports System.Text
Public Class MainClass
   Shared Sub Main()
      Dim buffer As StringBuilder = New StringBuilder()
      Dim string1, string2 As String
      string1 = "This {0} costs: {1:C}." & vbCrLf
      Dim objectArray As Object() = New Object(1) {}
      objectArray(0) = "car"
      objectArray(1) = 1234.56
      buffer.AppendFormat(string1, objectArray)
      string2 = "Number:{0:D3}. " & vbCrLf & _
         "Number right aligned with spaces:{0, 4}." & vbCrLf & _
         "Number left aligned with spaces:{0, -4}."
      buffer.AppendFormat(string2, 5)
      Console.WriteLine(buffer.ToString())
  End Sub
End Class


StringBuilder Performance Test

Imports System
Public Class MainClass
  Shared Sub Main()
        Dim i As Integer
        Dim StartTime As New DateTime()
        Dim EndTime As New DateTime()
        StartTime = DateTime.Now()
        Dim theText As New System.Text.StringBuilder()
        For i = 1 To 50000
            theText = theText.Append("A")
        Next
        EndTime = DateTime.Now
        Dim answer1, answer2 As Long
        answer1 = EndTime.Ticks() - StartTime.Ticks()
        StartTime = DateTime.Now()
        Dim aString As String = ""
        For i = 1 To 50000
            aString = aString & "A"
        Next
        EndTime = DateTime.Now
        answer2 = (EndTime.Ticks() - StartTime.Ticks()) 
        Console.WriteLine("StringBuilder was " & answer2 / answer1 & " times faster.")
  End Sub

End Class


StringBuilder Replace method

Imports System
Imports System.Text
Public Class MainClass
   Shared Sub Main()
      Dim builder1 As StringBuilder = _
         New StringBuilder("Happy Birthday Jane")
      Dim builder2 As StringBuilder = _
         New StringBuilder("good bye greg")
      Console.WriteLine("Before Replacements:" & vbCrLf & _
         builder1.ToString() & vbCrLf & builder2.ToString())
      builder1.Replace("Jane", "Greg")
      builder2.Replace("g"c, "G"c, 0, 5)
      Console.WriteLine("After Replacements:" & _
         vbCrLf & builder1.ToString() & vbCrLf & _
         builder2.ToString())
  End Sub " Main
End Class


Use StringBuilder for fast processing strings

Imports System
Imports System.Text
Public Class MainClass
    
    Shared Sub Main()
             Dim s1 As String = "One,Two,Three www . vbex . com"
             Const Space As Char = " "c
             Const Comma As Char = ","c
             Dim delimiters(  ) As Char = {Space, Comma}
             Dim ctr As Integer = 0
             Dim resultArray As String(  ) = s1.Split(delimiters)
             Dim output As New StringBuilder(  )
             Dim subString As String
             For Each subString In resultArray
                 ctr = ctr + 1
                 output.AppendFormat("{0} : {1}" & _
                    Environment.NewLine, ctr, subString)
             Next subString
             Console.WriteLine(output.ToString(  ))
            
   End Sub
End Class


Use StringBuilder Indexer

Imports System
Imports System.Text
Public Class MainClass
   Shared Sub Main()
      Dim i As Integer
      Dim buffer As StringBuilder = _
         New StringBuilder("Hello, how are you?")
      For i = 0 To buffer.Length - 1
         Console.WriteLine(buffer(i))
      Next
   End Sub " Main
End Class