當前位置: 首頁>>代碼示例>>VB.NET>>正文


VB.NET StringBuilder.AppendFormat方法代碼示例

本文整理匯總了VB.NET中System.Text.StringBuilder.AppendFormat方法的典型用法代碼示例。如果您正苦於以下問題:VB.NET StringBuilder.AppendFormat方法的具體用法?VB.NET StringBuilder.AppendFormat怎麽用?VB.NET StringBuilder.AppendFormat使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Text.StringBuilder的用法示例。


在下文中一共展示了StringBuilder.AppendFormat方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的VB.NET代碼示例。

示例1: Example

' 導入命名空間
Imports System.Globalization
Imports System.Text

Public Module Example
   Public Sub Main()
      Dim rnd As New Random()
      Dim culture As CultureInfo = CultureInfo.CreateSpecificCulture("fr-FR")
      Dim sb As New StringBuilder()
      Dim formatString As String = "    {0,12:N0} ({0,8:X8})" + vbCrLf +
                                   "And {1,12:N0} ({1,8:X8})" + vbCrLf +
                                   "  = {2,12:N0} ({2,8:X8})" + vbCrLf
      For ctr As Integer = 0 To 2
         Dim value1 As Integer = rnd.Next()
         Dim value2 As Integer = rnd.Next()
         sb.AppendFormat(culture, formatString,
                         value1, value2, value1 And value2).AppendLine()
      Next
      Console.WriteLine(sb.ToString())
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System.Text,代碼行數:21,代碼來源:StringBuilder.AppendFormat

輸出:

1 984 112 195 (76432643)
And 1 179 778 511 (4651FDCF)
= 1 178 674 243 (46412443)

2 034 813 710 (7948CB0E)
And  569 333 976 (21EF58D8)
=  558 385 160 (21484808)

126 717 735 (078D8F27)
And 1 830 715 973 (6D1E8245)
=   84 705 797 (050C8205)

示例2: Sample

' 導入命名空間
Imports System.Text
Imports System.Globalization

Class Sample
   Private Shared sb As New StringBuilder()

   Public Shared Sub Main()
      Dim var1 As Integer = 111
      Dim var2 As Single = 2.22F
      Dim var3 As String = "abcd"
      Dim var4 As Object() =  {3, 4.4, "X"c}
      
      Console.WriteLine()
      Console.WriteLine("StringBuilder.AppendFormat method:")
      sb.AppendFormat("1) {0}", var1)
      Show(sb)
      sb.AppendFormat("2) {0}, {1}", var1, var2)
      Show(sb)
      sb.AppendFormat("3) {0}, {1}, {2}", var1, var2, var3)
      Show(sb)
      sb.AppendFormat("4) {0}, {1}, {2}", var4)
      Show(sb)
      Dim ci As New CultureInfo("es-ES", True)
      sb.AppendFormat(ci, "5) {0}", var2)
      Show(sb)
   End Sub
   
   Public Shared Sub Show(sbs As StringBuilder)
      Console.WriteLine(sbs.ToString())
      sb.Length = 0
   End Sub
End Class
開發者ID:VB.NET開發者,項目名稱:System.Text,代碼行數:33,代碼來源:StringBuilder.AppendFormat

輸出:

StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22

示例3: Example

' 導入命名空間
Imports System.Globalization
Imports System.Text

Module Example
   Public Sub Main()
      Dim sb As New StringBuilder()
      Dim value As Decimal = 16.95d
      Dim enGB As CultureInfo = CultureInfo.CreateSpecificCulture("en-GB")
      Dim dateToday As DateTime = Date.Now
      sb.AppendFormat(enGB, "Final Price: {0:C2}", value)
      sb.AppendLine()
      sb.AppendFormat(enGB, "Date and Time: {0:d} at {0:t}", dateToday)
      Console.WriteLine(sb.ToString())
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System.Text,代碼行數:16,代碼來源:StringBuilder.AppendFormat

輸出:

Final Price: £16.95
Date and Time: 01/10/2014 at 10:22

示例4: Customer

' 導入命名空間
Imports System.Text

Public Class Customer
   Private custName As String
   Private custNumber As Integer
   
   Public Sub New(name As String, number As Integer)
      custName = name
      custNumber = number
   End Sub
   
   Public ReadOnly Property Name As String
      Get
         Return Me.custName
      End Get
   End Property
   
   Public ReadOnly Property CustomerNumber As Integer
      Get
         Return Me.custNumber
      End Get
   End Property
End Class

Public Class CustomerNumberFormatter 
   Implements IFormatProvider, ICustomFormatter
   
   Public Function GetFormat(formatType As Type) As Object _
                   Implements IFormatProvider.GetFormat
      If formatType Is GetType(ICustomFormatter) Then
         Return Me
      End If
      Return Nothing
   End Function
   
   Public Function Format(fmt As String, arg As Object, provider As IFormatProvider) As String _
                   Implements ICustomFormatter.Format
      If typeof arg Is Int32 Then
         Dim custNumber As String = CInt(arg).ToString("D10")
         Return custNumber.Substring(0, 4) + "-" + custNumber.SubString(4, 3) + _
                "-" + custNumber.Substring(7, 3)
      Else
         Return Nothing
      End If
   End Function                   
End Class

Module Example
   Public Sub Main()
      Dim customer As New Customer("A Plus Software", 903654)
      Dim sb As New StringBuilder()
      sb.AppendFormat(New CustomerNumberFormatter, "{0}: {1}", _
                      customer.CustomerNumber, customer.Name)
      Console.WriteLine(sb.ToString())
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System.Text,代碼行數:57,代碼來源:StringBuilder.AppendFormat

輸出:

0000-903-654: A Plus Software

示例5: Example

' 導入命名空間
Imports System.Collections.Generic
Imports System.Globalization
Imports System.Text

Module Example
   Public Sub Main()
      Dim culture As New CultureInfo("en-US")
      Dim sb As New StringBuilder()
      Dim temperatureInfo As New Dictionary(Of Date, Double) 
      temperatureInfo.Add(#6/1/2010 2:00PM#, 87.46)
      temperatureInfo.Add(#12/1/2010 10:00AM#, 36.81)
      
      sb.AppendLine("Temperature Information:").AppendLine()
      For Each item In temperatureInfo
         sb.AppendFormat(culture,
                         "Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F",
                         item.Key, item.Value).AppendLine()
      Next
      Console.WriteLine(sb.ToString())
   End Sub
End Module
開發者ID:VB.NET開發者,項目名稱:System.Text,代碼行數:22,代碼來源:StringBuilder.AppendFormat

輸出:

Temperature Information:

Temperature at  2:00 PM on  6/1/2010:  87.5°F
Temperature at 10:00 AM on 12/1/2010:  36.8°F

示例6: MainClass

' 導入命名空間
Imports System
Imports System.Text

Public Class MainClass
    
    Shared Sub Main()
             Dim s1 As String = "One,Two,Three www . java2s . 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
開發者ID:VB程序員,項目名稱:System.Text,代碼行數:30,代碼來源:StringBuilder.AppendFormat


注:本文中的System.Text.StringBuilder.AppendFormat方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。