VB.Net Tutorial/Data Type/StringBuilder

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

Append Char to StringBuilder

Option Strict On
Imports System.Text
Public Module modMain
   Public Sub Main()
      Dim chars() As Char = {"T"c,"h"c,"i"c,"s"c," "c,"i"c,"s"c, _
                             " "c,"a"c,"n"c," "c,"a"c,"r"c,"r"c,"a"c, _
                             "y"c," "c,"o"c,"f"c," "c,"C"c,"h"c, _
                             "a"c,"r"c,"s"c}
      Dim convertedChars As New StringBuilder
      For Each ch As Char in chars
         convertedChars.Append(ch)
      Next
      Dim displayString As String = convertedChars.ToString()
      Console.WriteLine(displayString)
   End Sub
End Module
This is an array of Chars

Demonstrating method Replace

Imports System.Text
Imports System.Windows.Forms
Module Tester
   Sub Main()
      Dim builder1 As StringBuilder = New StringBuilder("Happy Birthday")
      Dim builder2 As StringBuilder = New StringBuilder("good bye ")
      builder1.Replace("H", "G")
      builder2.Replace("g"c, "G"c, 0, 5)
      Console.WriteLine(builder1.ToString() & vbCrLf & _
         builder2.ToString())
   End Sub " Main
End Module
Gappy Birthday
Good bye

Demonstrating methods Insert and Remove of the StringBuilder class

Imports System.Text
Imports System.Windows.Forms
Module Tester
   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 = 12345677890
      Dim singleValue As Single = 2.5
      Dim doubleValue As Double = 33.333
      Dim buffer As StringBuilder = New StringBuilder()
      Dim output As String
      " 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, "  ")
      output = "buffer after inserts:" & vbCrLf & _
         buffer.ToString() & vbCrLf & vbCrLf
      buffer.Remove(12, 1) " delete 5 in 2.5
      buffer.Remove(2, 4) " delete 33.3 in 33.333
      output &= "buffer after Removes:" & vbCrLf & _
         buffer.ToString()
      Console.WriteLine(output)
   End Sub " Main 
End Module
buffer after inserts:
  33.333  2.5  12345677890  7  K  True  abcdef  good bye  hello
buffer after Removes:
  33  2.  12345677890  7  K  True  abcdef  good bye  hello

Demonstrating StringBuilder Append methods

Imports System.Text
Imports System.Windows.Forms
Module modBuilderAppend
   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 = 7
      Dim longValue As Long = 1000000
      Dim singleValue As Single = 2.5
      Dim doubleValue As Double = 33.333
      Dim buffer As StringBuilder = New StringBuilder()
      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
End Module
buffer = hello  good bye  abcdef  abc  True  Z  7  1000000  2.5  33.333

Demonstrating StringBuilder class constructors

Imports System.Text
Module Tester
   Sub Main()
      Dim buffer1, buffer2, buffer3 As StringBuilder
      buffer1 = New StringBuilder()
      buffer2 = New StringBuilder(10)
      buffer3 = New StringBuilder("hello")
      Console.WriteLine(buffer1.ToString())
      Console.WriteLine(buffer2.ToString())
      Console.WriteLine(buffer3.ToString())
   End Sub " Main
End Module
hello

Performance difference between String and StringBuilder

Imports System.Text
 
public class Test
   public Shared Sub Main
        Const ADD_STRING As String = "1234567890"
        Dim num_trials As Long = 1000
        Dim start_time As DateTime
        Dim stop_time As DateTime
        Dim elapsed_time As TimeSpan
        Dim txt As String
        Dim string_builder As New StringBuilder
        txt = ""
        start_time = Now
        For i As Long = 1 To num_trials
            txt = txt & ADD_STRING
        Next i
        stop_time = Now
        elapsed_time = stop_time.Subtract(start_time)
        Console.WriteLine(elapsed_time.TotalSeconds.ToString("0.000000"))
        txt = ""
        start_time = Now
        For i As Long = 1 To num_trials
            string_builder.Append(ADD_STRING)
        Next i
        txt = string_builder.ToString()
        stop_time = Now
        elapsed_time = stop_time.Subtract(start_time)
        Console.WriteLine(elapsed_time.TotalSeconds.ToString("0.000000"))
   End Sub
End class
0.015625
0.000000

StringBuilder AppendFormat

Imports System.Text
 Class Tester
     Public Shared Sub Main( )
         Dim s1 As String = "One,Two,Three Liberty Associates, Inc."
         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
Your balance as of May 16, 2002 is $ 19,950.40

StringBuilder.AppendFormat: {0:D} is ${1: #,###.00}

Imports System
public class Test
   public Shared Sub Main
        Dim objSB As New Text.StringBuilder
        objSB.AppendFormat("Your balance as of {0:D} is ${1: #,###.00}", _
            #5/16/2002#, 19950.4)
        Console.WriteLine(objSB.ToString)
   End Sub
   
End class
Your balance as of May 16, 2002 is $ 19,950.40

StringBuilder: Insert string, Append and Replace

Public Class Tester
    Public Shared Sub Main
        Dim result As New System.Text.StringBuilder("AA ")
        result.Append("CCC. ")
        result.Append("AAA").Append("rrr ").Append("eee")
        result.Insert(3, "note to stop ")
        result.Replace("CC", "rr")
        result.Insert(0,  vbNewLine)
        Console.WriteLine(result.ToString())
    End Sub
End Class
AA note to stop rrC. AAArrr eee

StringBuilder: Length, Capacity, EnsureCapacity

Imports System.Text
Module Tester
   Sub Main()
      Dim i As Integer
      Dim buffer As StringBuilder = New StringBuilder("Hello, how are you?")
      " use Length and Capacity properties
      Console.WriteLine("buffer = " & buffer.ToString & _
         vbCrLf & "Length = " & buffer.Length & vbCrLf & _
         "Capacity = " & buffer.Capacity)
      buffer.EnsureCapacity(75)
      Console.WriteLine("New capacity = " & buffer.Capacity)
      " truncate StringBuilder by setting Length property
      buffer.Length = 10
      
      Console.WriteLine("New Length = " & buffer.Length )
      
   End Sub 
End Module
buffer = Hello, how are you?
Length = 19
Capacity = 32
New capacity = 75
New Length = 10

StringBuilder.ToStrings

Imports System
public class Test
   public Shared Sub Main
        Dim objSB As New Text.StringBuilder("some text")
        Console.WriteLine(objSB.ToString)

   End Sub
   
End class
some text

Use StringBuilder Indexer

Imports System.Text
Module Tester
   Sub Main()
      Dim i As Integer
      Dim buffer As StringBuilder = New StringBuilder("Hello, how are you?")
      " use StringBuilder Indexer
      For i = 0 To buffer.Length - 1
         Console.WriteLine(buffer(i))
      Next
   End Sub 
End Module
H
e
l
l
o
,
h
o
w
a
r
e
y
o
u
?

Use StringBuilder to reverse a string

Imports System
public class Test
   public Shared Sub Main
        Dim data As String = "abcdefgh"

        Dim objSB As New Text.StringBuilder
        Dim intLength As Integer = Len(data)
        Dim intChar As Integer
        Dim chr As Char
        Dim objTS As TimeSpan
        Dim dteStart As Date
        objSB.Capacity = intLength
        objSB.Append(data)
        dteStart = Now()
        For intChar = 0 To CInt(objSB.Length / 2 - 1)
            chr = objSB.Chars(intChar)
            objSB.Chars(intChar) = objSB.Chars(intLength - intChar - 1)
            objSB.Chars(intLength - intChar - 1) = chr
        Next
        objTS = Now().Subtract(dteStart)
        Console.WriteLine(objTS.ToString)
        Console.WriteLine(objSB.ToString)
   End Sub
   
End class
00:00:00
hgfedcba