StringBuilder.AppendFormat Methode
Definition
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines entsprechenden Objektarguments ersetzt.Each format item is replaced by the string representation of a corresponding object argument.
Überlädt
AppendFormat(IFormatProvider, String, Object, Object, Object) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines der drei Argumente mit einem angegebenen Formatanbieter ersetzt.Each format item is replaced by the string representation of either of three arguments using a specified format provider. |
AppendFormat(String, Object) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines einzelnen Arguments ersetzt.Each format item is replaced by the string representation of a single argument. |
AppendFormat(String, Object[]) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines entsprechenden Arguments in einem Parameterarray ersetzt.Each format item is replaced by the string representation of a corresponding argument in a parameter array. |
AppendFormat(IFormatProvider, String, Object) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines einzigen Arguments mit einem angegebenen Formatanbieter ersetzt.Each format item is replaced by the string representation of a single argument using a specified format provider. |
AppendFormat(IFormatProvider, String, Object[]) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird von der Zeichenfolgendarstellung eines entsprechenden Arguments in einem Parameterarray ersetzt, das einen angegebenen Formatanbieter verwendet.Each format item is replaced by the string representation of a corresponding argument in a parameter array using a specified format provider. |
AppendFormat(String, Object, Object) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung für eines von zwei Argumenten ersetzt.Each format item is replaced by the string representation of either of two arguments. |
AppendFormat(IFormatProvider, String, Object, Object) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines der zwei Argumente mit einem angegebenen Formatanbieter ersetzt.Each format item is replaced by the string representation of either of two arguments using a specified format provider. |
AppendFormat(String, Object, Object, Object) |
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung für eines von drei Argumenten ersetzt.Each format item is replaced by the string representation of either of three arguments. |
AppendFormat(IFormatProvider, String, Object, Object, Object)
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines der drei Argumente mit einem angegebenen Formatanbieter ersetzt.Each format item is replaced by the string representation of either of three arguments using a specified format provider.
public:
System::Text::StringBuilder ^ AppendFormat(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public System.Text.StringBuilder AppendFormat (IFormatProvider provider, string format, object arg0, object arg1, object arg2);
public System.Text.StringBuilder? AppendFormat (IFormatProvider provider, string? format, object arg0, object arg1, object arg2);
member this.AppendFormat : IFormatProvider * string * obj * obj * obj -> System.Text.StringBuilder
Public Function AppendFormat (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object, arg2 As Object) As StringBuilder
Parameter
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that supplies culture-specific formatting information.
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- arg0
- Object
Das erste zu formatierende Objekt.The first object to format.
- arg1
- Object
Das zweite zu formatierende Objekt.The second object to format.
- arg2
- Object
Das dritte zu formatierende Objekt.The third object to format.
Gibt zurück
Ein Verweis auf diese Instanz, nachdem der Anfügevorgang abgeschlossen wurde.A reference to this instance after the append operation has completed. Nach dem Anfügevorgang enthält diese Instanz alle Daten, die auch vor dem Vorgang vorhanden waren. Ihnen folgt eine Kopie von format
, wobei die Formatangabe durch die Zeichenfolgendarstellung des entsprechenden Objektarguments ersetzt wurde.After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format
where any format specification is replaced by the string representation of the corresponding object argument.
Ausnahmen
format
ist null
.format
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer oder gleich 3 (drei).The index of a format item is less than 0 (zero), or greater than or equal to 3 (three).
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Im folgenden Beispiel wird die- AppendFormat(IFormatProvider, String, Object, Object, Object) Methode verwendet, um das Ergebnis einer booleschen And
Operation mit ganzzahligen Werten zu veranschaulichen.The following example uses the AppendFormat(IFormatProvider, String, Object, Object, Object) method to illustrate the result of a Boolean And
operation with integer values. Beachten Sie, dass die Format Zeichenfolge sechs Format Elemente enthält, die-Methode jedoch nur drei Elemente in der Argumentliste aufweist, da jedes Element auf zwei verschiedene Arten formatiert ist.Note that the format string includes six format items, but the method has only three items in its argument list, because each item is formatted in two different ways.
using System;
using System.Globalization;
using System.Text;
public class Example
{
public static void Main()
{
Random rnd = new Random();
CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
StringBuilder sb = new StringBuilder();
string formatString = " {0,12:N0} ({0,8:X8})\n" +
"And {1,12:N0} ({1,8:X8})\n" +
" = {2,12:N0} ({2,8:X8})\n";
for (int ctr = 0; ctr <= 2; ctr++) {
int value1 = rnd.Next();
int value2 = rnd.Next();
sb.AppendFormat(culture, formatString,
value1, value2, value1 & value2).
AppendLine();
}
Console.WriteLine(sb.ToString());
}
}
// The example displays output like the following:
// 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)
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
' The example displays the following output:
' 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)
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung der .NET Framework, um den Wert eines Objekts in seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.
Der- format
Parameter besteht aus null oder mehr Text Ausführungen mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, die Objekten in der Parameterliste dieser Methode entsprechen.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung des entsprechenden Objekts.The formatting process replaces each format item with the string representation of the corresponding object.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Wenn an der Index Position kein Parameter vorhanden ist, wird eine ausgelöst FormatException .If there is no parameter in the index position, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
Der provider
-Parameter gibt eine IFormatProvider -Implementierung an, die Formatierungsinformationen für und bereitstellen kann arg0
arg1
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for arg0
and arg1
. provider
kann eine der folgenden Formen haben:provider
can be any of the following:
Ein- CultureInfo Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.A CultureInfo object that provides culture-specific formatting information.
Ein- NumberFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für bereitstellt,
arg0
oder,arg1
Wenn es sich um numerische Werte handelt.A NumberFormatInfo object that provides culture-specific formatting information forarg0
orarg1
if they are numeric values.Ein- DateTimeFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für, oder bereitstellt,
arg0
arg1
arg2
Wenn diese Datums-und Uhrzeitwerte sind.A DateTimeFormatInfo object that provides culture-specific formatting information forarg0
,arg1
, orarg2
if they are date and time values.Eine benutzerdefinierte- IFormatProvider Implementierung, die Formatierungsinformationen für
arg0
, und bereitstelltarg1
arg2
.A custom IFormatProvider implementation that provides formatting information forarg0
,arg1
, andarg2
. In der Regel implementiert eine solche Implementierung auch die- ICustomFormatter Schnittstelle.Typically, such an implementation also implements the ICustomFormatter interface.
Wenn der- provider
Parameter ist null
, werden Format Anbieter Informationen aus der aktuellen Kultur abgerufen.If the provider
parameter is null
, format provider information is obtained from the current culture.
arg0
, arg1
und arg2
stellen die zu formatierenden Objekte dar.arg0
, arg1
, and arg2
represent the objects to be formatted. Jedes Format Element in format
wird durch die Zeichen folgen Darstellung des Objekts ersetzt, das über den entsprechenden Index verfügt.Each format item in format
is replaced with the string representation of the object that has the corresponding index. Wenn das Format Element enthält formatString
und das entsprechende Argument die IFormattable -Schnittstelle implementiert, definiert die-Methode des Arguments ToString(formatString, provider)
die Formatierung.If the format item includes formatString
and the corresponding argument implements the IFormattable interface, then the argument's ToString(formatString, provider)
method defines the formatting. Andernfalls definiert die-Methode des Arguments ToString()
die Formatierung.Otherwise, the argument's ToString()
method defines the formatting.
Hinweise für Aufrufer
Wenn Sie in .net Core und in den .NET Framework 4,0 und höheren Versionen das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der zugehörigen-Eigenschaft hinaus zunehmen MaxCapacity .In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Vorgehensweise: Definieren und Verwenden von benutzerdefinierten numerischen FormatanbieternHow to: Define and Use Custom Numeric Format Providers
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings
Gilt für:
AppendFormat(String, Object)
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines einzelnen Arguments ersetzt.Each format item is replaced by the string representation of a single argument.
public:
System::Text::StringBuilder ^ AppendFormat(System::String ^ format, System::Object ^ arg0);
public System.Text.StringBuilder AppendFormat (string format, object arg0);
public System.Text.StringBuilder AppendFormat (string format, object? arg0);
member this.AppendFormat : string * obj -> System.Text.StringBuilder
Public Function AppendFormat (format As String, arg0 As Object) As StringBuilder
Parameter
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- arg0
- Object
Ein zu formatierendes Objekt.An object to format.
Gibt zurück
Ein Verweis auf diese Instanz mit angefügtem format
.A reference to this instance with format
appended. Die einzelnen Elemente in format
werden jeweils durch eine Zeichenfolgendarstellung von arg0
ersetzt.Each format item in format
is replaced by the string representation of arg0
.
Ausnahmen
format
ist null
.format
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer gleich 1.The index of a format item is less than 0 (zero), or greater than or equal to 1.
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Das folgende Beispiel veranschaulicht die AppendFormat Methode.The following example demonstrates the AppendFormat method.
using namespace System;
using namespace System::Text;
using namespace System::Globalization;
void Show( StringBuilder^ sbs )
{
Console::WriteLine( sbs );
sbs->Length = 0;
}
int main()
{
StringBuilder^ sb = gcnew StringBuilder;
int var1 = 111;
float var2 = 2.22F;
String^ var3 = "abcd";
array<Object^>^var4 = {3,4.4,(Char)'X'};
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 );
CultureInfo^ ci = gcnew CultureInfo( "es-ES",true );
array<Object^>^temp1 = {var2};
sb->AppendFormat( ci, "5) {0}", temp1 );
Show( sb );
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
using System;
using System.Text;
using System.Globalization;
class Sample
{
static StringBuilder sb = new StringBuilder();
public static void Main()
{
int var1 = 111;
float var2 = 2.22F;
string var3 = "abcd";
object[] var4 = {3, 4.4, 'X'};
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);
CultureInfo ci = new CultureInfo("es-ES", true);
sb.AppendFormat(ci, "5) {0}", var2);
Show(sb);
}
public static void Show(StringBuilder sbs)
{
Console.WriteLine(sbs.ToString());
sb.Length = 0;
}
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
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
'
'This example produces the following results:
'
'StringBuilder.AppendFormat method:
'1) 111
'2) 111, 2.22
'3) 111, 2.22, abcd
'4) 3, 4.4, X
'5) 2,22
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung der .NET Framework, um den Wert eines Objekts in seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.
Der- format
Parameter besteht aus null oder mehr Text Läufen, die mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, gemischt werden.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items. Der Index der Format Elemente muss 0 (null) sein, arg0
damit er dem einzelnen Objekt in der Parameterliste dieser Methode entspricht.The index of the format items must be 0, to correspond to arg0
, the single object in the parameter list of this method. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung von arg0
.The formatting process replaces each format item with the string representation of arg0
.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Wenn an der Index Position kein Parameter vorhanden ist, wird eine ausgelöst FormatException .If there is no parameter in the index position, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
arg0
stellt das zu formatierende Objekt dar.arg0
represents the object to be formatted. Jedes Format Element in format
wird durch die Zeichen folgen Darstellung von ersetzt arg0
.Each format item in format
is replaced with the string representation of arg0
. Wenn das Format Element formatString
die- arg0
Schnittstelle enthält und implementiert IFormattable , dann arg0.ToString(formatString, null)
definiert die Formatierung.If the format item includes formatString
and arg0
implements the IFormattable interface, then arg0.ToString(formatString, null)
defines the formatting. Andernfalls wird arg0.ToString()
die Formatierung definiert.Otherwise, arg0.ToString()
defines the formatting.
Wenn die zugewiesene Zeichenfolge format
"vielen Dank für Ihre Spende von {0: # # # #} Lebensmitteldosen an unsere gemeinnützige Organisation ist".If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." und arg0
ist eine ganze Zahl mit dem Wert 10. der Rückgabewert ist "vielen Dank für Ihre Spende von 10 Nahrungsmitteln an unsere gemeinnützige Organisation".and arg0
is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."
Hinweise für Aufrufer
Wenn Sie in .net Core und in den .NET Framework 4,0 und höheren Versionen das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der zugehörigen-Eigenschaft hinaus zunehmen MaxCapacity .In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings
Gilt für:
AppendFormat(String, Object[])
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines entsprechenden Arguments in einem Parameterarray ersetzt.Each format item is replaced by the string representation of a corresponding argument in a parameter array.
public:
System::Text::StringBuilder ^ AppendFormat(System::String ^ format, ... cli::array <System::Object ^> ^ args);
public System.Text.StringBuilder AppendFormat (string format, params object[] args);
public System.Text.StringBuilder AppendFormat (string format, params object[]? args);
member this.AppendFormat : string * obj[] -> System.Text.StringBuilder
Public Function AppendFormat (format As String, ParamArray args As Object()) As StringBuilder
Parameter
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- args
- Object[]
Ein Array zu formatierender Objekte.An array of objects to format.
Gibt zurück
Ein Verweis auf diese Instanz mit angefügtem format
.A reference to this instance with format
appended. Jedes Formatelement in format
wird durch die Zeichenfolgendarstellung des entsprechenden Objektarguments ersetzt.Each format item in format
is replaced by the string representation of the corresponding object argument.
Ausnahmen
format
oder args
ist null
.format
or args
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer oder gleich der Länge des args
-Arrays.The index of a format item is less than 0 (zero), or greater than or equal to the length of the args
array.
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Das folgende Beispiel veranschaulicht die AppendFormat Methode.The following example demonstrates the AppendFormat method.
using namespace System;
using namespace System::Text;
using namespace System::Globalization;
void Show( StringBuilder^ sbs )
{
Console::WriteLine( sbs );
sbs->Length = 0;
}
int main()
{
StringBuilder^ sb = gcnew StringBuilder;
int var1 = 111;
float var2 = 2.22F;
String^ var3 = "abcd";
array<Object^>^var4 = {3,4.4,(Char)'X'};
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 );
CultureInfo^ ci = gcnew CultureInfo( "es-ES",true );
array<Object^>^temp1 = {var2};
sb->AppendFormat( ci, "5) {0}", temp1 );
Show( sb );
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
using System;
using System.Text;
using System.Globalization;
class Sample
{
static StringBuilder sb = new StringBuilder();
public static void Main()
{
int var1 = 111;
float var2 = 2.22F;
string var3 = "abcd";
object[] var4 = {3, 4.4, 'X'};
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);
CultureInfo ci = new CultureInfo("es-ES", true);
sb.AppendFormat(ci, "5) {0}", var2);
Show(sb);
}
public static void Show(StringBuilder sbs)
{
Console.WriteLine(sbs.ToString());
sb.Length = 0;
}
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
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
'
'This example produces the following results:
'
'StringBuilder.AppendFormat method:
'1) 111
'2) 111, 2.22
'3) 111, 2.22, abcd
'4) 3, 4.4, X
'5) 2,22
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung der .NET Framework, um den Wert eines Objekts in seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.
Der- format
Parameter besteht aus null oder mehr Text Ausführungen mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, die Objekten in der Parameterliste dieser Methode entsprechen.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung des entsprechenden Objekts.The formatting process replaces each format item with the string representation of the corresponding object.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Wenn an der Index Position kein Parameter vorhanden ist, wird eine ausgelöst FormatException .If there is no parameter in the index position, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
args
stellt die zu formatierenden Objekte dar.args
represents the objects to be formatted. Jedes Format Element in format
wird durch die Zeichen folgen Darstellung des entsprechenden Objekts in ersetzt args
.Each format item in format
is replaced with the string representation of the corresponding object in args
. Wenn das Format Element enthält formatString
und das entsprechende Objekt in args
die- IFormattable Schnittstelle implementiert, dann args[index].ToString(formatString, provider)
definiert die Formatierung.If the format item includes formatString
and the corresponding object in args
implements the IFormattable interface, then args[index].ToString(formatString, provider)
defines the formatting. Andernfalls wird args[index].ToString()
die Formatierung definiert.Otherwise, args[index].ToString()
defines the formatting.
Wenn die zugewiesene Zeichenfolge format
"vielen Dank für Ihre Spende von {0: # # # #} Lebensmitteldosen an unsere gemeinnützige Organisation ist".If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." und arg0
ist eine ganze Zahl mit dem Wert 10. der Rückgabewert ist "vielen Dank für Ihre Spende von 10 Nahrungsmitteln an unsere gemeinnützige Organisation".and arg0
is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."
Hinweise für Aufrufer
Wenn Sie in .net Core und in den .NET Framework 4,0 und höheren Versionen das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der zugehörigen-Eigenschaft hinaus zunehmen MaxCapacity .In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings
Gilt für:
AppendFormat(IFormatProvider, String, Object)
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines einzigen Arguments mit einem angegebenen Formatanbieter ersetzt.Each format item is replaced by the string representation of a single argument using a specified format provider.
public:
System::Text::StringBuilder ^ AppendFormat(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0);
public System.Text.StringBuilder AppendFormat (IFormatProvider provider, string format, object arg0);
public System.Text.StringBuilder AppendFormat (IFormatProvider? provider, string format, object? arg0);
member this.AppendFormat : IFormatProvider * string * obj -> System.Text.StringBuilder
Public Function AppendFormat (provider As IFormatProvider, format As String, arg0 As Object) As StringBuilder
Parameter
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that supplies culture-specific formatting information.
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- arg0
- Object
Das zu formatierende Objekt.The object to format.
Gibt zurück
Ein Verweis auf diese Instanz, nachdem der Anfügevorgang abgeschlossen wurde.A reference to this instance after the append operation has completed. Nach dem Anfügevorgang enthält diese Instanz alle Daten, die auch vor dem Vorgang vorhanden waren. Ihnen folgt eine Kopie von format
, wobei die Formatangabe durch die Zeichenfolgendarstellung von arg0
ersetzt wurde.After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format
in which any format specification is replaced by the string representation of arg0
.
Ausnahmen
format
ist null
.format
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer oder gleich 1.The index of a format item is less than 0 (zero), or greater than or equal to one (1).
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Folgendes umfasst zwei Aufrufe der- AppendFormat(IFormatProvider, String, Object) Methode.The following includes two calls to the AppendFormat(IFormatProvider, String, Object) method. Beide verwenden die Formatierungs Konventionen der Kultur English-Great Britain (en-GB).Both use the formatting conventions of the English-Great Britain (en-GB) culture. Der erste fügt die Zeichen folgen Darstellung einer Decimal Wert Währung in eine Ergebnis Zeichenfolge ein.The first inserts the string representation of a Decimal value currency in a result string. Die zweite fügt einen DateTime Wert an zwei Stellen in einer Ergebnis Zeichenfolge ein, wobei der erste nur die kurze Datums Zeichenfolge und die zweite die kurze Zeit Zeichenfolge enthält.The second inserts a DateTime value in two places in a result string, the first including only the short date string and the second the short time string.
using System;
using System.Globalization;
using System.Text;
public class Example
{
public static void Main()
{
StringBuilder sb = new StringBuilder();
Decimal value = 16.95m;
CultureInfo enGB = CultureInfo.CreateSpecificCulture("en-GB");
DateTime dateToday = DateTime.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());
}
}
// The example displays the following output:
// Final Price: £16.95
// Date and Time: 01/10/2014 at 10:22
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
' The example displays output like the following:
' Final Price: £16.95
' Date and Time: 01/10/2014 at 10:22
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung des .NET Framework, um den Wert von in arg0
seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of arg0
to its text representation and embed that representation in the current StringBuilder object.
Der- format
Parameter besteht aus null oder mehr Text Läufen, die mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, gemischt werden.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items. Der Index jedes Format Elements muss NULL (0) sein, da diese Methode eine Argumentliste mit einem einzelnen Argument enthält.The index of each format item must be zero (0) since this method includes an argument list with a single argument. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung von arg0
.The formatting process replaces each format item with the string representation of arg0
.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Da die Methode in diesem Fall AppendFormat(IFormatProvider, String, Object) ein einzelnes Argument in der Argumentliste aufweist, muss der Wert des Indexes immer 0 sein.In this case, since the AppendFormat(IFormatProvider, String, Object) method has a single argument in the argument list, the value of index must always be 0. Wenn dies nicht der Fall ist, wird eine ausgelöst FormatException .If it is not, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
Der- provider
Parameter gibt eine- IFormatProvider Implementierung an, die Formatierungsinformationen für die Objekte in bereitstellen kann args
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for the objects in args
. provider
kann eine der folgenden Formen haben:provider
can be any of the following:
Ein- CultureInfo Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.A CultureInfo object that provides culture-specific formatting information.
Ein- NumberFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für bereitstellt,
arg0
Wenn es sich um einen numerischen Wert handelt.A NumberFormatInfo object that provides culture-specific formatting information forarg0
if it is a numeric value.Ein- DateTimeFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für bereitstellt,
arg0
Wenn es sich um einen Datums-und Uhrzeitwert handelt.A DateTimeFormatInfo object that provides culture-specific formatting information forarg0
if it is a date and time value.Eine benutzerdefinierte- IFormatProvider Implementierung, die Formatierungsinformationen für bereitstellt
arg0
.A custom IFormatProvider implementation that provides formatting information forarg0
. In der Regel implementiert eine solche Implementierung auch die- ICustomFormatter Schnittstelle.Typically, such an implementation also implements the ICustomFormatter interface.
Wenn der- provider
Parameter ist null
, werden Formatierungsinformationen aus der aktuellen Kultur abgerufen.If the provider
parameter is null
, formatting information is obtained from the current culture.
arg0
stellt das zu formatierende Objekt dar.arg0
represents the object to be formatted. Jedes Format Element in format
wird durch die Zeichen folgen Darstellung von ersetzt arg0
.Each format item in format
is replaced with the string representation of arg0
. Wenn das Format Element formatString
die- arg0
Schnittstelle enthält und implementiert IFormattable , dann arg0.ToString(formatString, provider)
definiert die Formatierung.If the format item includes formatString
and arg0
implements the IFormattable interface, then arg0.ToString(formatString, provider)
defines the formatting. Andernfalls wird arg0.ToString()
die Formatierung definiert.Otherwise, arg0.ToString()
defines the formatting.
Hinweise für Aufrufer
Wenn Sie in .net Core und in den .NET Framework 4,0 und höheren Versionen das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der zugehörigen-Eigenschaft hinaus zunehmen MaxCapacity .In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Vorgehensweise: Definieren und Verwenden von benutzerdefinierten numerischen FormatanbieternHow to: Define and Use Custom Numeric Format Providers
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings
Gilt für:
AppendFormat(IFormatProvider, String, Object[])
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird von der Zeichenfolgendarstellung eines entsprechenden Arguments in einem Parameterarray ersetzt, das einen angegebenen Formatanbieter verwendet.Each format item is replaced by the string representation of a corresponding argument in a parameter array using a specified format provider.
public:
System::Text::StringBuilder ^ AppendFormat(IFormatProvider ^ provider, System::String ^ format, ... cli::array <System::Object ^> ^ args);
public System.Text.StringBuilder AppendFormat (IFormatProvider provider, string format, params object[] args);
public System.Text.StringBuilder AppendFormat (IFormatProvider? provider, string format, params object[]? args);
member this.AppendFormat : IFormatProvider * string * obj[] -> System.Text.StringBuilder
Public Function AppendFormat (provider As IFormatProvider, format As String, ParamArray args As Object()) As StringBuilder
Parameter
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that supplies culture-specific formatting information.
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- args
- Object[]
Ein Array zu formatierender Objekte.An array of objects to format.
Gibt zurück
Ein Verweis auf diese Instanz, nachdem der Anfügevorgang abgeschlossen wurde.A reference to this instance after the append operation has completed. Nach dem Anfügevorgang enthält diese Instanz alle Daten, die auch vor dem Vorgang vorhanden waren. Ihnen folgt eine Kopie von format
, wobei die Formatangabe durch die Zeichenfolgendarstellung des entsprechenden Objektarguments ersetzt wurde.After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format
where any format specification is replaced by the string representation of the corresponding object argument.
Ausnahmen
format
ist null
.format
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer oder gleich der Länge des args
-Arrays.The index of a format item is less than 0 (zero), or greater than or equal to the length of the args
array.
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Das folgende Beispiel veranschaulicht die AppendFormat Methode.The following example demonstrates the AppendFormat method.
using namespace System;
using namespace System::Text;
using namespace System::Globalization;
void Show( StringBuilder^ sbs )
{
Console::WriteLine( sbs );
sbs->Length = 0;
}
int main()
{
StringBuilder^ sb = gcnew StringBuilder;
int var1 = 111;
float var2 = 2.22F;
String^ var3 = "abcd";
array<Object^>^var4 = {3,4.4,(Char)'X'};
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 );
CultureInfo^ ci = gcnew CultureInfo( "es-ES",true );
array<Object^>^temp1 = {var2};
sb->AppendFormat( ci, "5) {0}", temp1 );
Show( sb );
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
using System;
using System.Text;
using System.Globalization;
class Sample
{
static StringBuilder sb = new StringBuilder();
public static void Main()
{
int var1 = 111;
float var2 = 2.22F;
string var3 = "abcd";
object[] var4 = {3, 4.4, 'X'};
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);
CultureInfo ci = new CultureInfo("es-ES", true);
sb.AppendFormat(ci, "5) {0}", var2);
Show(sb);
}
public static void Show(StringBuilder sbs)
{
Console.WriteLine(sbs.ToString());
sb.Length = 0;
}
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
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
'
'This example produces the following results:
'
'StringBuilder.AppendFormat method:
'1) 111
'2) 111, 2.22
'3) 111, 2.22, abcd
'4) 3, 4.4, X
'5) 2,22
Im folgenden Beispiel wird eine benutzerdefinierte IFormatProvider Implementierung mit dem Namen definiert CustomerFormatter
, die eine 10-stellige Kundennummer mit Bindestrichen nach den vierten und siebten Ziffern formatiert.The following example defines a custom IFormatProvider implementation named CustomerFormatter
that formats a 10-digit customer number with hyphens after the fourth and seventh digits. Es wird an die- StringBuilder.AppendFormat(IFormatProvider, String, Object[]) Methode übermittelt, um eine Zeichenfolge zu erstellen, die die formatierte Kundennummer und den Kundennamen enthält.It is passed to the StringBuilder.AppendFormat(IFormatProvider, String, Object[]) method to create a string that includes the formatted customer number and customer name.
using System;
using System.Text;
public class Customer
{
private string custName;
private int custNumber;
public Customer(string name, int number)
{
this.custName = name;
this.custNumber = number;
}
public string Name
{
get { return this.custName; }
}
public int CustomerNumber
{
get { return this.custNumber; }
}
}
public class CustomerNumberFormatter : IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
return null;
}
public string Format(string format, object arg, IFormatProvider provider)
{
if (arg is Int32)
{
string custNumber = ((int) arg).ToString("D10");
return custNumber.Substring(0, 4) + "-" + custNumber.Substring(4, 3) +
"-" + custNumber.Substring(7, 3);
}
else
{
return null;
}
}
}
public class Example
{
public static void Main()
{
Customer customer = new Customer("A Plus Software", 903654);
StringBuilder sb = new StringBuilder();
sb.AppendFormat(new CustomerNumberFormatter(), "{0}: {1}",
customer.CustomerNumber, customer.Name);
Console.WriteLine(sb.ToString());
}
}
// The example displays the following output:
// 0000-903-654: A Plus Software
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
' The example displays the following output:
' 0000-903-654: A Plus Software
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung der .NET Framework, um den Wert eines Objekts in seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.
Der- format
Parameter besteht aus null oder mehr Text Ausführungen mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, die Objekten in der Parameterliste dieser Methode entsprechen.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung des entsprechenden Objekts.The formatting process replaces each format item with the string representation of the corresponding object.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Wenn an der Index Position kein Parameter vorhanden ist, wird eine ausgelöst FormatException .If there is no parameter in the index position, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
Der- provider
Parameter gibt eine- IFormatProvider Implementierung an, die Formatierungsinformationen für die Objekte in bereitstellen kann args
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for the objects in args
. provider
kann eine der folgenden Formen haben:provider
can be any of the following:
Ein- CultureInfo Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.A CultureInfo object that provides culture-specific formatting information.
Ein- NumberFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für numerische Werte in bereitstellt
args
.A NumberFormatInfo object that provides culture-specific formatting information for numeric values inargs
.Ein- DateTimeFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für Datums-und Uhrzeitwerte in bereitstellt
args
.A DateTimeFormatInfo object that provides culture-specific formatting information for date and time values inargs
.Eine benutzerdefinierte- IFormatProvider Implementierung, die Formatierungsinformationen für ein oder mehrere-Objekte in bereitstellt
args
.A custom IFormatProvider implementation that provides formatting information for one or more of the objects inargs
. In der Regel implementiert eine solche Implementierung auch die- ICustomFormatter Schnittstelle.Typically, such an implementation also implements the ICustomFormatter interface. Das zweite Beispiel im nächsten Abschnitt veranschaulicht einen StringBuilder.AppendFormat(IFormatProvider, String, Object[]) Methoden aufrufmit einer benutzerdefinierten- IFormatProvider Implementierung.The second example in the next section illustrates an StringBuilder.AppendFormat(IFormatProvider, String, Object[]) method call with a custom IFormatProvider implementation.
Wenn der- provider
Parameter ist null
, werden Format Anbieter Informationen aus der aktuellen Kultur abgerufen.If the provider
parameter is null
, format provider information is obtained from the current culture.
args
stellt die zu formatierenden Objekte dar.args
represents the objects to be formatted. Jedes Format Element in format
wird durch die Zeichen folgen Darstellung des entsprechenden Objekts in ersetzt args
.Each format item in format
is replaced with the string representation of the corresponding object in args
. Wenn das Format Element enthält formatString
und das entsprechende Objekt in args
die- IFormattable Schnittstelle implementiert, dann args[index].ToString(formatString, provider)
definiert die Formatierung.If the format item includes formatString
and the corresponding object in args
implements the IFormattable interface, then args[index].ToString(formatString, provider)
defines the formatting. Andernfalls wird args[index].ToString()
die Formatierung definiert.Otherwise, args[index].ToString()
defines the formatting.
Hinweise für Aufrufer
Wenn Sie in .net Core und in den .NET Framework 4,0 und höheren Versionen das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der zugehörigen-Eigenschaft hinaus zunehmen MaxCapacity .In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Vorgehensweise: Definieren und Verwenden von benutzerdefinierten numerischen FormatanbieternHow to: Define and Use Custom Numeric Format Providers
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings
Gilt für:
AppendFormat(String, Object, Object)
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung für eines von zwei Argumenten ersetzt.Each format item is replaced by the string representation of either of two arguments.
public:
System::Text::StringBuilder ^ AppendFormat(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public System.Text.StringBuilder AppendFormat (string format, object arg0, object arg1);
public System.Text.StringBuilder AppendFormat (string format, object? arg0, object? arg1);
member this.AppendFormat : string * obj * obj -> System.Text.StringBuilder
Public Function AppendFormat (format As String, arg0 As Object, arg1 As Object) As StringBuilder
Parameter
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- arg0
- Object
Das erste zu formatierende Objekt.The first object to format.
- arg1
- Object
Das zweite zu formatierende Objekt.The second object to format.
Gibt zurück
Ein Verweis auf diese Instanz mit angefügtem format
.A reference to this instance with format
appended. Jedes Formatelement in format
wird durch die Zeichenfolgendarstellung des entsprechenden Objektarguments ersetzt.Each format item in format
is replaced by the string representation of the corresponding object argument.
Ausnahmen
format
ist null
.format
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer gleich 2.The index of a format item is less than 0 (zero), or greater than or equal to 2.
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Das folgende Beispiel veranschaulicht die AppendFormat Methode.The following example demonstrates the AppendFormat method.
using namespace System;
using namespace System::Text;
using namespace System::Globalization;
void Show( StringBuilder^ sbs )
{
Console::WriteLine( sbs );
sbs->Length = 0;
}
int main()
{
StringBuilder^ sb = gcnew StringBuilder;
int var1 = 111;
float var2 = 2.22F;
String^ var3 = "abcd";
array<Object^>^var4 = {3,4.4,(Char)'X'};
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 );
CultureInfo^ ci = gcnew CultureInfo( "es-ES",true );
array<Object^>^temp1 = {var2};
sb->AppendFormat( ci, "5) {0}", temp1 );
Show( sb );
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
using System;
using System.Text;
using System.Globalization;
class Sample
{
static StringBuilder sb = new StringBuilder();
public static void Main()
{
int var1 = 111;
float var2 = 2.22F;
string var3 = "abcd";
object[] var4 = {3, 4.4, 'X'};
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);
CultureInfo ci = new CultureInfo("es-ES", true);
sb.AppendFormat(ci, "5) {0}", var2);
Show(sb);
}
public static void Show(StringBuilder sbs)
{
Console.WriteLine(sbs.ToString());
sb.Length = 0;
}
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
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
'
'This example produces the following results:
'
'StringBuilder.AppendFormat method:
'1) 111
'2) 111, 2.22
'3) 111, 2.22, abcd
'4) 3, 4.4, X
'5) 2,22
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung der .NET Framework, um den Wert eines Objekts in seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.
Der format
-Parameter besteht aus null oder mehr Text Läufen mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, die mit arg0
und übereinstimmen arg1
, den beiden-Objekten in der Parameterliste dieser Methode.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to arg0
and arg1
, the two objects in the parameter list of this method. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung des entsprechenden Objekts.The formatting process replaces each format item with the string representation of the corresponding object.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Wenn an der Index Position kein Parameter vorhanden ist, wird eine ausgelöst FormatException .If there is no parameter in the index position, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
arg0
und arg1
stellen die zu formatierenden Objekte dar.arg0
and arg1
represent the objects to be formatted. Jedes Format Element in format
wird durch die Zeichen folgen Darstellung von arg0
oder ersetzt arg1
.Each format item in format
is replaced with the string representation of either arg0
or arg1
. Wenn das Format Element enthält formatString
und das entsprechende-Objekt die- IFormattable Schnittstelle implementiert, dann arg
definiert x .ToString(formatString, provider)
die Formatierung, wobei x für den Index des Arguments steht.If the format item includes formatString
and the corresponding object implements the IFormattable interface, then arg
x.ToString(formatString, provider)
defines the formatting, where x is the index of the argument. Andernfalls definiert arg
x .ToString()
die Formatierung.Otherwise, arg
x.ToString()
defines the formatting.
Wenn die zugewiesene Zeichenfolge format
"vielen Dank für Ihre Spende von {0: # # # #} Lebensmitteldosen an unsere gemeinnützige Organisation ist".If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." und arg0
ist eine ganze Zahl mit dem Wert 10. der Rückgabewert ist "vielen Dank für Ihre Spende von 10 Nahrungsmitteln an unsere gemeinnützige Organisation".and arg0
is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."
Hinweise für Aufrufer
Wenn Sie in .net Core und in den .NET Framework 4,0 und höheren Versionen das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der zugehörigen-Eigenschaft hinaus zunehmen MaxCapacity .In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings
Gilt für:
AppendFormat(IFormatProvider, String, Object, Object)
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung eines der zwei Argumente mit einem angegebenen Formatanbieter ersetzt.Each format item is replaced by the string representation of either of two arguments using a specified format provider.
public:
System::Text::StringBuilder ^ AppendFormat(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public System.Text.StringBuilder AppendFormat (IFormatProvider provider, string format, object arg0, object arg1);
public System.Text.StringBuilder? AppendFormat (IFormatProvider provider, string? format, object arg0, object arg1);
member this.AppendFormat : IFormatProvider * string * obj * obj -> System.Text.StringBuilder
Public Function AppendFormat (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object) As StringBuilder
Parameter
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.An object that supplies culture-specific formatting information.
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- arg0
- Object
Das erste zu formatierende Objekt.The first object to format.
- arg1
- Object
Das zweite zu formatierende Objekt.The second object to format.
Gibt zurück
Ein Verweis auf diese Instanz, nachdem der Anfügevorgang abgeschlossen wurde.A reference to this instance after the append operation has completed. Nach dem Anfügevorgang enthält diese Instanz alle Daten, die auch vor dem Vorgang vorhanden waren. Ihnen folgt eine Kopie von format
, wobei die Formatangabe durch die Zeichenfolgendarstellung des entsprechenden Objektarguments ersetzt wurde.After the append operation, this instance contains any data that existed before the operation, suffixed by a copy of format
where any format specification is replaced by the string representation of the corresponding object argument.
Ausnahmen
format
ist null
.format
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer oder gleich 2.The index of a format item is less than 0 (zero), or greater than or equal to 2 (two).
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Im folgenden Beispiel wird die AppendFormat(IFormatProvider, String, Object, Object) -Methode verwendet, um in einem generischen-Objekt gespeicherte Zeit-und Temperaturdaten anzuzeigen Dictionary<TKey,TValue> .The following example uses the AppendFormat(IFormatProvider, String, Object, Object) method to display time and temperature data stored in a generic Dictionary<TKey,TValue> object. Beachten Sie, dass die Format Zeichenfolge drei Format Elemente enthält, obwohl nur zu formatierende Objekte vorhanden sind.Note that the format string has three format items, although there are only to objects to format. Dies liegt daran, dass das erste Objekt in der Liste (ein Datums-und Uhrzeitwert) von zwei Format Elementen verwendet wird: das erste Format Element zeigt die Uhrzeit an, während die zweite das Datum anzeigt.This is because the first object in the list (a date and time value) is used by two format items: The first format item displays the time, and the second displays the date.
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
public class Example
{
public static void Main()
{
CultureInfo culture = new CultureInfo("en-US");
StringBuilder sb = new StringBuilder();
Dictionary<DateTime, Double> temperatureInfo = new Dictionary<DateTime, Double>();
temperatureInfo.Add(new DateTime(2010, 6, 1, 14, 0, 0), 87.46);
temperatureInfo.Add(new DateTime(2010, 12, 1, 10, 0, 0), 36.81);
sb.AppendLine("Temperature Information:\n");
foreach (var item in temperatureInfo)
{
sb.AppendFormat(culture,
"Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F\n",
item.Key, item.Value);
}
Console.WriteLine(sb.ToString());
}
}
// The example displays the following output:
// 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
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
' The example displays the following output:
' 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
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung der .NET Framework, um den Wert eines Objekts in seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.
Der- format
Parameter besteht aus null oder mehr Text Ausführungen mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, die Objekten in der Parameterliste dieser Methode entsprechen.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to objects in the parameter list of this method. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung des entsprechenden Objekts.The formatting process replaces each format item with the string representation of the corresponding object.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Wenn an der Index Position kein Parameter vorhanden ist, wird eine ausgelöst FormatException .If there is no parameter in the index position, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
Der provider
-Parameter gibt eine IFormatProvider -Implementierung an, die Formatierungsinformationen für und bereitstellen kann arg0
arg1
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for arg0
and arg1
. provider
kann eine der folgenden Formen haben:provider
can be any of the following:
Ein- CultureInfo Objekt, das kulturspezifische Formatierungsinformationen bereitstellt.A CultureInfo object that provides culture-specific formatting information.
Ein- NumberFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für bereitstellt,
arg0
oder,arg1
Wenn es sich um numerische Werte handelt.A NumberFormatInfo object that provides culture-specific formatting information forarg0
orarg1
if they are numeric values.Ein- DateTimeFormatInfo Objekt, das kulturspezifische Formatierungsinformationen für oder enthält,
arg0
arg1
Wenn es sich um Datums-und Uhrzeitwerte handelt.A DateTimeFormatInfo object that provides culture-specific formatting information forarg0
orarg1
if they are date and time values.Eine benutzerdefinierte IFormatProvider -Implementierung, die Formatierungsinformationen für und bereitstellt
arg0
arg1
.A custom IFormatProvider implementation that provides formatting information forarg0
andarg1
. In der Regel implementiert eine solche Implementierung auch die- ICustomFormatter Schnittstelle.Typically, such an implementation also implements the ICustomFormatter interface.
Wenn der- provider
Parameter ist null
, werden Format Anbieter Informationen aus der aktuellen Kultur abgerufen.If the provider
parameter is null
, format provider information is obtained from the current culture.
arg0
und arg1
stellen die zu formatierenden Objekte dar.arg0
and arg1
represent the objects to be formatted. Jedes Format Element in format
wird durch die Zeichen folgen Darstellung des Objekts ersetzt, das über den entsprechenden Index verfügt.Each format item in format
is replaced with the string representation of the object that has the corresponding index. Wenn das Format Element enthält formatString
und das entsprechende Argument die IFormattable -Schnittstelle implementiert, definiert die-Methode des Arguments ToString(formatString, provider)
die Formatierung.If the format item includes formatString
and the corresponding argument implements the IFormattable interface, then the argument's ToString(formatString, provider)
method defines the formatting. Andernfalls definiert die-Methode des Arguments ToString()
die Formatierung.Otherwise, the argument's ToString()
method defines the formatting.
Hinweise für Aufrufer
IIn .net Core und in den .NET Framework 4,0 und höheren Versionen, wenn Sie das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der-Eigenschaft hinaus zunehmen MaxCapacity .IIn .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Vorgehensweise: Definieren und Verwenden von benutzerdefinierten numerischen FormatanbieternHow to: Define and Use Custom Numeric Format Providers
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings
Gilt für:
AppendFormat(String, Object, Object, Object)
Fügt die Zeichenfolge, die durch Verarbeitung einer kombinierten Formatzeichenfolge mit 0 (null) oder mehr Formatelementen zurückgegeben wurde, an diese Instanz an.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. Jedes Formatelement wird durch die Zeichenfolgendarstellung für eines von drei Argumenten ersetzt.Each format item is replaced by the string representation of either of three arguments.
public:
System::Text::StringBuilder ^ AppendFormat(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public System.Text.StringBuilder AppendFormat (string format, object arg0, object arg1, object arg2);
public System.Text.StringBuilder? AppendFormat (string? format, object arg0, object arg1, object arg2);
member this.AppendFormat : string * obj * obj * obj -> System.Text.StringBuilder
Public Function AppendFormat (format As String, arg0 As Object, arg1 As Object, arg2 As Object) As StringBuilder
Parameter
- format
- String
Eine kombinierte Formatzeichenfolge.A composite format string.
- arg0
- Object
Das erste zu formatierende Objekt.The first object to format.
- arg1
- Object
Das zweite zu formatierende Objekt.The second object to format.
- arg2
- Object
Das dritte zu formatierende Objekt.The third object to format.
Gibt zurück
Ein Verweis auf diese Instanz mit angefügtem format
.A reference to this instance with format
appended. Jedes Formatelement in format
wird durch die Zeichenfolgendarstellung des entsprechenden Objektarguments ersetzt.Each format item in format
is replaced by the string representation of the corresponding object argument.
Ausnahmen
format
ist null
.format
is null
.
format
ist ungültig.format
is invalid.
- oder --or-
Der Index eines Formatelements ist kleiner als 0 (null) oder größer oder gleich 3.The index of a format item is less than 0 (zero), or greater than or equal to 3.
Die Länge der erweiterten Zeichenfolge würde MaxCapacity überschreiten.The length of the expanded string would exceed MaxCapacity.
Beispiele
Das folgende Beispiel veranschaulicht die AppendFormat Methode.The following example demonstrates the AppendFormat method.
using namespace System;
using namespace System::Text;
using namespace System::Globalization;
void Show( StringBuilder^ sbs )
{
Console::WriteLine( sbs );
sbs->Length = 0;
}
int main()
{
StringBuilder^ sb = gcnew StringBuilder;
int var1 = 111;
float var2 = 2.22F;
String^ var3 = "abcd";
array<Object^>^var4 = {3,4.4,(Char)'X'};
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 );
CultureInfo^ ci = gcnew CultureInfo( "es-ES",true );
array<Object^>^temp1 = {var2};
sb->AppendFormat( ci, "5) {0}", temp1 );
Show( sb );
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
using System;
using System.Text;
using System.Globalization;
class Sample
{
static StringBuilder sb = new StringBuilder();
public static void Main()
{
int var1 = 111;
float var2 = 2.22F;
string var3 = "abcd";
object[] var4 = {3, 4.4, 'X'};
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);
CultureInfo ci = new CultureInfo("es-ES", true);
sb.AppendFormat(ci, "5) {0}", var2);
Show(sb);
}
public static void Show(StringBuilder sbs)
{
Console.WriteLine(sbs.ToString());
sb.Length = 0;
}
}
/*
This example produces the following results:
StringBuilder.AppendFormat method:
1) 111
2) 111, 2.22
3) 111, 2.22, abcd
4) 3, 4.4, X
5) 2,22
*/
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
'
'This example produces the following results:
'
'StringBuilder.AppendFormat method:
'1) 111
'2) 111, 2.22
'3) 111, 2.22, abcd
'4) 3, 4.4, X
'5) 2,22
Hinweise
Diese Methode verwendet die Funktion für die kombinierte Formatierung der .NET Framework, um den Wert eines Objekts in seine Textdarstellung zu konvertieren und diese Darstellung in das aktuelle-Objekt einzubetten StringBuilder .This method uses the composite formatting feature of the .NET Framework to convert the value of an object to its text representation and embed that representation in the current StringBuilder object.
Der- format
Parameter besteht aus null oder mehr Text Ausführungen mit NULL oder mehr indizierten Platzhaltern, so genannten Format Elementen, arg0
arg2
die durch, die Objekte in der Parameterliste dieser Methode entsprechen.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items, that correspond to arg0
through arg2
, the objects in the parameter list of this method. Der Formatierungsprozess ersetzt jedes Format Element durch die Zeichen folgen Darstellung des entsprechenden Objekts.The formatting process replaces each format item with the string representation of the corresponding object.
Die Syntax eines Format Elements lautet wie folgt:The syntax of a format item is as follows:
{Index[,length] [:FormatString]}{index[,length][:formatString]}
Elemente in eckigen Klammern sind optional.Elements in square brackets are optional. In der folgenden Tabelle wird jedes Element beschrieben.The following table describes each element.
ElementElement | BESCHREIBUNGDescription |
---|---|
Indexindex | Die null basierte Position in der Parameterliste des-Objekts, das formatiert werden soll.The zero-based position in the parameter list of the object to be formatted. Wenn das durch den Index angegebene Objekt ist null , wird das Format Element durch ersetzt String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. Wenn an der Index Position kein Parameter vorhanden ist, wird eine ausgelöst FormatException .If there is no parameter in the index position, a FormatException is thrown. |
,Länge,length | Die Mindestanzahl von Zeichen in der Zeichen folgen Darstellung des-Parameters.The minimum number of characters in the string representation of the parameter. Wenn dies positiv ist, wird der-Parameter rechtsbündig ausgerichtet. Wenn der Wert negativ ist, wird er linksbündig ausgerichtet.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:Format Zeichenfolge:formatString | Eine standardmäßige oder benutzerdefinierte Format Zeichenfolge, die vom-Parameter unterstützt wird.A standard or custom format string that is supported by the parameter. |
Hinweis
Informationen zu den standardmäßigen und benutzerdefinierten Format Zeichenfolgen, die mit Datums-und Uhrzeitwerten verwendet werden, finden Sie unter Standardformat Zeichenfolgen für Datum und Uhrzeit und benutzerdefinierteFor the standard and custom format strings used with date and time values, see Standard Date and Time Format Strings and Custom Date and Time Format Strings. Informationen zu den standardmäßigen und benutzerdefiniertenFormatierungs Zeichenfolgen, die mit numerischen Werten verwendet werden, finden Sie unter standardmäßige Zahlenformat Zeichenfolgen undFor the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. Informationen zu den Standardformat Zeichenfolgen für-Enumerationen finden Sie unter Enumerationsformatzeichenfolgen.For the standard format strings used with enumerations, see Enumeration Format Strings.
arg0
, arg1
und arg2
stellen die zu formatierenden Objekte dar.arg0
, arg1
, and arg2
represent the objects to be formatted. Jedes Format Element in format
wird arg0
arg1
arg2
abhängig vom Index des Format Elements durch die Zeichen folgen Darstellung von, oder ersetzt.Each format item in format
is replaced with the string representation of either arg0
, arg1
, or arg2
, depending on the index of the format item. Wenn das Format Element enthält formatString
und das entsprechende Objekt in args
die- IFormattable Schnittstelle implementiert, dann arg
.ToString(formatString, null)
definiert x die Formatierung, wobei x für den Index des Arguments steht.If the format item includes formatString
and the corresponding object in args
implements the IFormattable interface, then arg
x.ToString(formatString, null)
defines the formatting, where x is the index of the argument. Andernfalls definiert arg
x .ToString()
die Formatierung.Otherwise, arg
x.ToString()
defines the formatting.
Wenn die zugewiesene Zeichenfolge format
"vielen Dank für Ihre Spende von {0: # # # #} Lebensmitteldosen an unsere gemeinnützige Organisation ist".If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." und arg0
ist eine ganze Zahl mit dem Wert 10. der Rückgabewert ist "vielen Dank für Ihre Spende von 10 Nahrungsmitteln an unsere gemeinnützige Organisation".and arg0
is an integer with the value 10, the return value will be "Thank you for your donation of 10 cans of food to our charitable organization."
Hinweise für Aufrufer
Wenn Sie in .net Core und in den .NET Framework 4,0 und höheren Versionen das StringBuilder Objekt durch Aufrufen des-Konstruktors instanziieren StringBuilder(Int32, Int32) , können sowohl die Länge als auch die Kapazität der- StringBuilder Instanz über den Wert der zugehörigen-Eigenschaft hinaus zunehmen MaxCapacity .In .NET Core and in the .NET Framework 4.0 and later versions, when you instantiate the StringBuilder object by calling the StringBuilder(Int32, Int32) constructor, both the length and the capacity of the StringBuilder instance can grow beyond the value of its MaxCapacity property. Dies kann besonders dann der Fall sein, wenn Sie die Append(String) AppendFormat(String, Object) Methoden und zum Anfügen kleiner Zeichen folgen aufzurufen.This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
Weitere Informationen
- Formatieren von Typen in .NETFormatting Types in .NET
- Kombinierte FormatierungComposite Formatting
- Standardmäßige ZahlenformatzeichenfolgenStandard Numeric Format Strings
- Benutzerdefinierte ZahlenformatzeichenfolgenCustom Numeric Format Strings
- Standard-Formatzeichenfolgen für Datum und UhrzeitStandard Date and Time Format Strings
- Benutzerdefinierte Formatzeichenfolgen für Datum und UhrzeitCustom Date and Time Format Strings
- TimeSpan-StandardformatzeichenfolgenStandard TimeSpan Format Strings
- Benutzerdefinierte TimeSpan-FormatzeichenfolgenCustom TimeSpan Format Strings
- EnumerationsformatzeichenfolgenEnumeration Format Strings