VB.Net by API/System.Text/StringBuilder

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

StringBuilder.Append

  
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.AppendFormat

  
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


StringBuilder.Chars

  
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.EnsureCapacity

  
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.Insert

  
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.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