StringBuilder.AppendFormat 메서드
정의
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목은 해당 개체 인수의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of a corresponding object argument.
오버로드
AppendFormat(IFormatProvider, String, Object, Object, Object) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 지정된 형식 공급자를 사용하여 각 형식 항목이 세 인수 중 하나의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of either of three arguments using a specified format provider. |
AppendFormat(String, Object) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목이 단일 인수의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of a single argument. |
AppendFormat(String, Object[]) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 형식 항목은 매개 변수 배열에 있는 해당 인수의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of a corresponding argument in a parameter array. |
AppendFormat(IFormatProvider, String, Object) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 지정된 형식 공급자를 사용하여 각 서식 항목이 단일 인수의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of a single argument using a specified format provider. |
AppendFormat(IFormatProvider, String, Object[]) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목은 지정된 서식 공급자를 사용하여 매개 변수 배열에 있는 해당 인수의 문자열 표현으로 바뀝니다.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) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목이 두 인수 중 하나의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of either of two arguments. |
AppendFormat(IFormatProvider, String, Object, Object) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 지정된 형식 공급자를 사용하여 각 형식 항목이 두 인수 중 하나의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of either of two arguments using a specified format provider. |
AppendFormat(String, Object, Object, Object) |
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목이 세 인수 중 하나의 문자열 표현으로 바뀝니다.Each format item is replaced by the string representation of either of three arguments. |
AppendFormat(IFormatProvider, String, Object, Object, Object)
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 지정된 형식 공급자를 사용하여 각 형식 항목이 세 인수 중 하나의 문자열 표현으로 바뀝니다.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
매개 변수
- provider
- IFormatProvider
문화권별 형식 정보를 제공하는 개체입니다.An object that supplies culture-specific formatting information.
- format
- String
복합 형식 문자열입니다.A composite format string.
- arg0
- Object
서식을 지정할 첫 번째 개체입니다.The first object to format.
- arg1
- Object
서식을 지정할 두 번째 개체입니다.The second object to format.
- arg2
- Object
서식을 지정할 세 번째 개체입니다.The third object to format.
반환
추가 작업이 완료된 후 이 인스턴스에 대한 참조입니다.A reference to this instance after the append operation has completed. 추가 작업 후, 이 인스턴스에는 추가 작업 이전부터 있던 데이터가 포함되고 그 뒤에 형식 사양이 해당하는 개체 인수의 문자열 표현으로 바뀐 format
의 복사본이 포함됩니다.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.
예외
format
이(가) null
인 경우format
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스가 0보다 작거나 3보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to 3 (three).
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음 예제에서는 메서드를 사용 하 여 AppendFormat(IFormatProvider, String, Object, Object, Object) And
정수 값을 사용 하는 부울 연산의 결과를 보여 줍니다.The following example uses the AppendFormat(IFormatProvider, String, Object, Object, Object) method to illustrate the result of a Boolean And
operation with integer values. 서식 문자열에는 6 개의 형식 항목이 포함 되지만, 각 항목은 두 가지 방법으로 서식이 지정 되기 때문에 메서드의 인수 목록에는 세 개의 항목만 있습니다.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)
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여 개체의 값을 해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
format
매개 변수는이 메서드의 매개 변수 목록에 있는 개체에 해당 하는 0 개 이상의 인덱싱된 자리 표시자 (서식 항목 이라고 함)와 결합 된 0 개 이상의 텍스트 실행으로 구성 됩니다.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. 서식 지정 프로세스에서는 각 형식 항목을 해당 개체의 문자열 표현으로 바꿉니다.The formatting process replaces each format item with the string representation of the corresponding object.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 인덱스 위치에 매개 변수가 없으면 FormatException 이 throw 됩니다.If there is no parameter in the index position, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
provider
매개 변수는 IFormatProvider 및에 대 한 형식 정보를 제공할 수 있는 구현을 지정 합니다 arg0
arg1
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for arg0
and arg1
. provider
다음 중 하나일 수 있습니다.provider
can be any of the following:
CultureInfo문화권별 형식 지정 정보를 제공 하는 개체입니다.A CultureInfo object that provides culture-specific formatting information.
NumberFormatInfo에 대 한 문화권별 서식 지정 정보를 제공 하는 개체입니다 .이 개체는
arg0
arg1
숫자 값입니다.A NumberFormatInfo object that provides culture-specific formatting information forarg0
orarg1
if they are numeric values.DateTimeFormatInfo
arg0
arg1
arg2
날짜 및 시간 값인 경우, 또는에 대 한 문화권별 서식 지정 정보를 제공 하는 개체입니다.A DateTimeFormatInfo object that provides culture-specific formatting information forarg0
,arg1
, orarg2
if they are date and time values.IFormatProvider, 및에 대 한 서식 정보를 제공 하는 사용자 지정 구현입니다
arg0
arg1
arg2
.A custom IFormatProvider implementation that provides formatting information forarg0
,arg1
, andarg2
. 일반적으로 이러한 구현은 인터페이스도 구현 ICustomFormatter 합니다.Typically, such an implementation also implements the ICustomFormatter interface.
provider
매개 변수가 이면 null
현재 문화권에서 형식 공급자 정보를 가져옵니다.If the provider
parameter is null
, format provider information is obtained from the current culture.
arg0
, arg1
및 arg2
는 형식을 지정할 개체를 나타냅니다.arg0
, arg1
, and arg2
represent the objects to be formatted. 의 각 서식 항목 format
은 해당 인덱스를 포함 하는 개체의 문자열 표현으로 바뀝니다.Each format item in format
is replaced with the string representation of the object that has the corresponding index. 형식 항목에가 포함 formatString
되 고 해당 인수가 인터페이스를 구현 하는 경우 IFormattable 인수의 ToString(formatString, provider)
메서드는 형식을 정의 합니다.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. 그렇지 않으면 인수의 ToString()
메서드가 서식 지정을 정의 합니다.Otherwise, the argument's ToString()
method defines the formatting.
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
적용 대상
AppendFormat(String, Object)
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목이 단일 인수의 문자열 표현으로 바뀝니다.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
매개 변수
- format
- String
복합 형식 문자열입니다.A composite format string.
- arg0
- Object
서식을 지정할 개체입니다.An object to format.
반환
format
이 추가된 이 인스턴스에 대한 참조입니다.A reference to this instance with format
appended. format
의 각 서식 항목이 arg0
의 문자열 표현으로 바뀝니다.Each format item in format
is replaced by the string representation of arg0
.
예외
format
은 null
입니다.format
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스는 0(영)보다 작고, 또는 1보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to 1.
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음 예제는 AppendFormat 메서드.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
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여 개체의 값을 해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
매개 변수는 0 개 이상의 format
인덱싱된 자리 표시자 (서식 항목 이라고 함)와 결합 된 0 개 이상의 텍스트 실행으로 구성 됩니다.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items. 형식 항목의 인덱스는 arg0
이 메서드의 매개 변수 목록에 있는 단일 개체에 해당 하는 0 이어야 합니다.The index of the format items must be 0, to correspond to arg0
, the single object in the parameter list of this method. 서식 지정 프로세스에서는 각 서식 항목을의 문자열 표현으로 바꿉니다 arg0
.The formatting process replaces each format item with the string representation of arg0
.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 인덱스 위치에 매개 변수가 없으면 FormatException 이 throw 됩니다.If there is no parameter in the index position, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
arg0
형식을 지정할 개체를 나타냅니다.arg0
represents the object to be formatted. 의 각 서식 항목이 format
의 문자열 표현으로 바뀝니다 arg0
.Each format item in format
is replaced with the string representation of arg0
. 형식 항목이 formatString
인터페이스를 포함 하 고 구현 하는 경우는 형식을 arg0
정의 합니다 IFormattable arg0.ToString(formatString, null)
.If the format item includes formatString
and arg0
implements the IFormattable interface, then arg0.ToString(formatString, null)
defines the formatting. 그렇지 않으면 arg0.ToString()
는 형식을 정의 합니다.Otherwise, arg0.ToString()
defines the formatting.
에 할당 된 문자열이 format
"자선 기구 조직의 {0: # # # #} 캔의 기부에 참여 해 주셔서 감사 합니다."If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." 및 arg0
는 값이 10 인 정수 이며, 반환 값은 "자선 기구 조직에 식량 10 캔의 기부에 대해 감사 합니다."가 됩니다.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."
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
적용 대상
AppendFormat(String, Object[])
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 형식 항목은 매개 변수 배열에 있는 해당 인수의 문자열 표현으로 바뀝니다.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
매개 변수
- format
- String
복합 형식 문자열입니다.A composite format string.
- args
- Object[]
형식을 지정할 개체의 배열입니다.An array of objects to format.
반환
format
이 추가된 이 인스턴스에 대한 참조입니다.A reference to this instance with format
appended. format
의 각 서식 항목은 해당 개체 인수의 문자열 표현으로 바뀝니다.Each format item in format
is replaced by the string representation of the corresponding object argument.
예외
format
또는 args
가 null
인 경우format
or args
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스가 0보다 작거나 args
배열의 길이보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to the length of the args
array.
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음 예제는 AppendFormat 메서드.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
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여 개체의 값을 해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
format
매개 변수는이 메서드의 매개 변수 목록에 있는 개체에 해당 하는 0 개 이상의 인덱싱된 자리 표시자 (서식 항목 이라고 함)와 결합 된 0 개 이상의 텍스트 실행으로 구성 됩니다.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. 서식 지정 프로세스에서는 각 형식 항목을 해당 개체의 문자열 표현으로 바꿉니다.The formatting process replaces each format item with the string representation of the corresponding object.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 인덱스 위치에 매개 변수가 없으면 FormatException 이 throw 됩니다.If there is no parameter in the index position, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
args
형식을 지정할 개체를 나타냅니다.args
represents the objects to be formatted. 의 각 서식 항목 format
은의 해당 개체에 대 한 문자열 표현으로 바뀝니다 args
.Each format item in format
is replaced with the string representation of the corresponding object in args
. 형식 항목에가 포함 되어 formatString
있고의 해당 개체가 인터페이스를 구현 하는 경우는 args
IFormattable args[index].ToString(formatString, provider)
형식을 정의 합니다.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. 그렇지 않으면 args[index].ToString()
는 형식을 정의 합니다.Otherwise, args[index].ToString()
defines the formatting.
에 할당 된 문자열이 format
"자선 기구 조직의 {0: # # # #} 캔의 기부에 참여 해 주셔서 감사 합니다."If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." 및 arg0
는 값이 10 인 정수 이며, 반환 값은 "자선 기구 조직에 식량 10 캔의 기부에 대해 감사 합니다."가 됩니다.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."
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
적용 대상
AppendFormat(IFormatProvider, String, Object)
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 지정된 형식 공급자를 사용하여 각 서식 항목이 단일 인수의 문자열 표현으로 바뀝니다.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
매개 변수
- provider
- IFormatProvider
문화권별 형식 정보를 제공하는 개체입니다.An object that supplies culture-specific formatting information.
- format
- String
복합 형식 문자열입니다.A composite format string.
- arg0
- Object
형식을 지정할 개체입니다.The object to format.
반환
추가 작업이 완료된 후 이 인스턴스에 대한 참조입니다.A reference to this instance after the append operation has completed. 추가 작업 후, 이 인스턴스에는 추가 작업 이전부터 있던 데이터가 포함되고 그 뒤에 형식 사양이 format
의 문자열 표현으로 바뀐 arg0
의 복사본이 포함됩니다.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
.
예외
format
이(가) null
인 경우format
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스가 0보다 작거나, 1보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to one (1).
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음에는 메서드에 대 한 두 개의 호출이 포함 되어 있습니다 AppendFormat(IFormatProvider, String, Object) .The following includes two calls to the AppendFormat(IFormatProvider, String, Object) method. 둘 다 English-Great Britain (en-us) 문화권의 서식 규칙을 사용 합니다.Both use the formatting conventions of the English-Great Britain (en-GB) culture. 첫 번째는 Decimal 결과 문자열의 값 통화에 대 한 문자열 표현을 삽입 합니다.The first inserts the string representation of a Decimal value currency in a result string. 두 번째는 DateTime 결과 문자열의 두 위치에 값을 삽입 하 고, 첫 번째는 간단한 날짜 문자열만 포함 하 고 두 번째는 짧은 시간 문자열을 포함 합니다.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
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여의 값을 arg0
해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
매개 변수는 0 개 이상의 format
인덱싱된 자리 표시자 (서식 항목 이라고 함)와 결합 된 0 개 이상의 텍스트 실행으로 구성 됩니다.The format
parameter consists of zero or more runs of text intermixed with zero or more indexed placeholders, called format items. 이 메서드에는 단일 인수를 사용 하는 인수 목록이 포함 되므로 각 형식 항목의 인덱스는 0 이어야 합니다.The index of each format item must be zero (0) since this method includes an argument list with a single argument. 서식 지정 프로세스에서는 각 서식 항목을의 문자열 표현으로 바꿉니다 arg0
.The formatting process replaces each format item with the string representation of arg0
.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 이 경우 AppendFormat(IFormatProvider, String, Object) 메서드의 인수 목록에 단일 인수가 있으므로 인덱스 값은 항상 0 이어야 합니다.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. 그렇지 않으면 FormatException 이 throw 됩니다.If it is not, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
provider
매개 변수는 IFormatProvider 의 개체에 대 한 형식 지정 정보를 제공할 수 있는 구현을 지정 합니다 args
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for the objects in args
. provider
다음 중 하나일 수 있습니다.provider
can be any of the following:
CultureInfo문화권별 형식 지정 정보를 제공 하는 개체입니다.A CultureInfo object that provides culture-specific formatting information.
NumberFormatInfo숫자 값 인 경우에 대 한 문화권별 서식 지정 정보를 제공 하는 개체입니다
arg0
.A NumberFormatInfo object that provides culture-specific formatting information forarg0
if it is a numeric value.DateTimeFormatInfo
arg0
날짜 및 시간 값인 경우에 대 한 문화권별 서식 지정 정보를 제공 하는 개체입니다.A DateTimeFormatInfo object that provides culture-specific formatting information forarg0
if it is a date and time value.IFormatProvider에 대 한 서식 정보를 제공 하는 사용자 지정 구현입니다
arg0
.A custom IFormatProvider implementation that provides formatting information forarg0
. 일반적으로 이러한 구현은 인터페이스도 구현 ICustomFormatter 합니다.Typically, such an implementation also implements the ICustomFormatter interface.
provider
매개 변수가 이면 null
현재 문화권에서 형식 지정 정보를 가져옵니다.If the provider
parameter is null
, formatting information is obtained from the current culture.
arg0
형식을 지정할 개체를 나타냅니다.arg0
represents the object to be formatted. 의 각 서식 항목이 format
의 문자열 표현으로 바뀝니다 arg0
.Each format item in format
is replaced with the string representation of arg0
. 형식 항목이 formatString
인터페이스를 포함 하 고 구현 하는 경우는 형식을 arg0
정의 합니다 IFormattable arg0.ToString(formatString, provider)
.If the format item includes formatString
and arg0
implements the IFormattable interface, then arg0.ToString(formatString, provider)
defines the formatting. 그렇지 않으면 arg0.ToString()
는 형식을 정의 합니다.Otherwise, arg0.ToString()
defines the formatting.
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
적용 대상
AppendFormat(IFormatProvider, String, Object[])
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목은 지정된 서식 공급자를 사용하여 매개 변수 배열에 있는 해당 인수의 문자열 표현으로 바뀝니다.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
매개 변수
- provider
- IFormatProvider
문화권별 형식 정보를 제공하는 개체입니다.An object that supplies culture-specific formatting information.
- format
- String
복합 형식 문자열입니다.A composite format string.
- args
- Object[]
형식을 지정할 개체의 배열입니다.An array of objects to format.
반환
추가 작업이 완료된 후 이 인스턴스에 대한 참조입니다.A reference to this instance after the append operation has completed. 추가 작업 후, 이 인스턴스에는 추가 작업 이전부터 있던 데이터가 포함되고 그 뒤에 형식 사양이 해당하는 개체 인수의 문자열 표현으로 바뀐 format
의 복사본이 포함됩니다.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.
예외
format
이(가) null
인 경우format
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스가 0보다 작거나 args
배열의 길이보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to the length of the args
array.
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음 예제는 AppendFormat 메서드.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
다음 예제에서는 IFormatProvider CustomerFormatter
네 자리 숫자의 네 자리 숫자 뒤에 하이픈을 지정 하는 이라는 사용자 지정 구현을 정의 합니다.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. StringBuilder.AppendFormat(IFormatProvider, String, Object[])형식이 지정 된 고객 번호와 고객 이름을 포함 하는 문자열을 만들기 위해 메서드에 전달 됩니다.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
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여 개체의 값을 해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
format
매개 변수는이 메서드의 매개 변수 목록에 있는 개체에 해당 하는 0 개 이상의 인덱싱된 자리 표시자 (서식 항목 이라고 함)와 결합 된 0 개 이상의 텍스트 실행으로 구성 됩니다.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. 서식 지정 프로세스에서는 각 형식 항목을 해당 개체의 문자열 표현으로 바꿉니다.The formatting process replaces each format item with the string representation of the corresponding object.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 인덱스 위치에 매개 변수가 없으면 FormatException 이 throw 됩니다.If there is no parameter in the index position, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
provider
매개 변수는 IFormatProvider 의 개체에 대 한 형식 지정 정보를 제공할 수 있는 구현을 지정 합니다 args
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for the objects in args
. provider
다음 중 하나일 수 있습니다.provider
can be any of the following:
CultureInfo문화권별 형식 지정 정보를 제공 하는 개체입니다.A CultureInfo object that provides culture-specific formatting information.
NumberFormatInfo의 숫자 값에 대 한 문화권별 형식 지정 정보를 제공 하는 개체입니다
args
.A NumberFormatInfo object that provides culture-specific formatting information for numeric values inargs
.DateTimeFormatInfo의 날짜 및 시간 값에 대 한 문화권별 형식 지정 정보를 제공 하는 개체입니다
args
.A DateTimeFormatInfo object that provides culture-specific formatting information for date and time values inargs
.IFormatProvider에서 하나 이상의 개체에 대 한 형식 지정 정보를 제공 하는 사용자 지정 구현입니다
args
.A custom IFormatProvider implementation that provides formatting information for one or more of the objects inargs
. 일반적으로 이러한 구현은 인터페이스도 구현 ICustomFormatter 합니다.Typically, such an implementation also implements the ICustomFormatter interface. 다음 섹션의 두 번째 예제에서는 StringBuilder.AppendFormat(IFormatProvider, String, Object[]) 사용자 지정 구현을 사용한 메서드 호출을 보여 줍니다 IFormatProvider .The second example in the next section illustrates an StringBuilder.AppendFormat(IFormatProvider, String, Object[]) method call with a custom IFormatProvider implementation.
provider
매개 변수가 이면 null
현재 문화권에서 형식 공급자 정보를 가져옵니다.If the provider
parameter is null
, format provider information is obtained from the current culture.
args
형식을 지정할 개체를 나타냅니다.args
represents the objects to be formatted. 의 각 서식 항목 format
은의 해당 개체에 대 한 문자열 표현으로 바뀝니다 args
.Each format item in format
is replaced with the string representation of the corresponding object in args
. 형식 항목에가 포함 되어 formatString
있고의 해당 개체가 인터페이스를 구현 하는 경우는 args
IFormattable args[index].ToString(formatString, provider)
형식을 정의 합니다.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. 그렇지 않으면 args[index].ToString()
는 형식을 정의 합니다.Otherwise, args[index].ToString()
defines the formatting.
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
적용 대상
AppendFormat(String, Object, Object)
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목이 두 인수 중 하나의 문자열 표현으로 바뀝니다.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
매개 변수
- format
- String
복합 형식 문자열입니다.A composite format string.
- arg0
- Object
서식을 지정할 첫 번째 개체입니다.The first object to format.
- arg1
- Object
서식을 지정할 두 번째 개체입니다.The second object to format.
반환
format
이 추가된 이 인스턴스에 대한 참조입니다.A reference to this instance with format
appended. format
의 각 서식 항목은 해당 개체 인수의 문자열 표현으로 바뀝니다.Each format item in format
is replaced by the string representation of the corresponding object argument.
예외
format
은 null
입니다.format
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스가 0보다 작거나 2보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to 2.
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음 예제는 AppendFormat 메서드.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
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여 개체의 값을 해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
format
매개 변수는 0 개 이상의 인덱싱된 자리 표시자 (형식 항목 이라고 함)와 결합 된 0 개 이상의 텍스트 실행으로 구성 되며, arg0
arg1
이 메서드의 매개 변수 목록에 있는 두 개체는 및에 해당 합니다.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. 서식 지정 프로세스에서는 각 형식 항목을 해당 개체의 문자열 표현으로 바꿉니다.The formatting process replaces each format item with the string representation of the corresponding object.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 인덱스 위치에 매개 변수가 없으면 FormatException 이 throw 됩니다.If there is no parameter in the index position, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
arg0
및 arg1
는 형식을 지정할 개체를 나타냅니다.arg0
and arg1
represent the objects to be formatted. 의 각 서식 항목 format
은 또는의 문자열 표현으로 바뀝니다 arg0
arg1
.Each format item in format
is replaced with the string representation of either arg0
or arg1
. 형식 항목에가 포함 되 formatString
고 해당 개체가 인터페이스를 구현 하는 경우 IFormattable arg
x .ToString(formatString, provider)
는 형식을 정의 합니다. 여기서 x 는 인수의 인덱스입니다.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. 그렇지 않으면 arg
x .ToString()
는 형식을 정의 합니다.Otherwise, arg
x.ToString()
defines the formatting.
에 할당 된 문자열이 format
"자선 기구 조직의 {0: # # # #} 캔의 기부에 참여 해 주셔서 감사 합니다."If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." 및 arg0
는 값이 10 인 정수 이며, 반환 값은 "자선 기구 조직에 식량 10 캔의 기부에 대해 감사 합니다."가 됩니다.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."
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
적용 대상
AppendFormat(IFormatProvider, String, Object, Object)
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 지정된 형식 공급자를 사용하여 각 형식 항목이 두 인수 중 하나의 문자열 표현으로 바뀝니다.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
매개 변수
- provider
- IFormatProvider
문화권별 형식 정보를 제공하는 개체입니다.An object that supplies culture-specific formatting information.
- format
- String
복합 형식 문자열입니다.A composite format string.
- arg0
- Object
서식을 지정할 첫 번째 개체입니다.The first object to format.
- arg1
- Object
서식을 지정할 두 번째 개체입니다.The second object to format.
반환
추가 작업이 완료된 후 이 인스턴스에 대한 참조입니다.A reference to this instance after the append operation has completed. 추가 작업 후, 이 인스턴스에는 추가 작업 이전부터 있던 데이터가 포함되고 그 뒤에 형식 사양이 해당하는 개체 인수의 문자열 표현으로 바뀐 format
의 복사본이 포함됩니다.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.
예외
format
은 null
입니다.format
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스가 0보다 작거나 2보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to 2 (two).
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음 예제에서는 메서드를 사용 하 여 AppendFormat(IFormatProvider, String, Object, Object) 제네릭 개체에 저장 된 시간 및 온도 데이터를 표시 합니다 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. 서식 지정을 위한 개체만 있지만 형식 문자열에는 세 가지 형식 항목이 있습니다.Note that the format string has three format items, although there are only to objects to format. 이는 목록의 첫 번째 개체 (날짜 및 시간 값)가 두 개의 형식 항목에 사용 되기 때문입니다. 첫 번째 형식 항목은 시간을 표시 하 고 두 번째 개체는 날짜를 표시 합니다.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
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여 개체의 값을 해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
format
매개 변수는이 메서드의 매개 변수 목록에 있는 개체에 해당 하는 0 개 이상의 인덱싱된 자리 표시자 (서식 항목 이라고 함)와 결합 된 0 개 이상의 텍스트 실행으로 구성 됩니다.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. 서식 지정 프로세스에서는 각 형식 항목을 해당 개체의 문자열 표현으로 바꿉니다.The formatting process replaces each format item with the string representation of the corresponding object.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 인덱스 위치에 매개 변수가 없으면 FormatException 이 throw 됩니다.If there is no parameter in the index position, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
provider
매개 변수는 IFormatProvider 및에 대 한 형식 정보를 제공할 수 있는 구현을 지정 합니다 arg0
arg1
.The provider
parameter specifies an IFormatProvider implementation that can provide formatting information for arg0
and arg1
. provider
다음 중 하나일 수 있습니다.provider
can be any of the following:
CultureInfo문화권별 형식 지정 정보를 제공 하는 개체입니다.A CultureInfo object that provides culture-specific formatting information.
NumberFormatInfo에 대 한 문화권별 서식 지정 정보를 제공 하는 개체입니다 .이 개체는
arg0
arg1
숫자 값입니다.A NumberFormatInfo object that provides culture-specific formatting information forarg0
orarg1
if they are numeric values.DateTimeFormatInfo에 대 한 문화권별 서식 지정 정보를 제공 하는 개체입니다 .이 개체는
arg0
arg1
날짜 및 시간 값입니다.A DateTimeFormatInfo object that provides culture-specific formatting information forarg0
orarg1
if they are date and time values.IFormatProvider및에 대 한 형식 정보를 제공 하는 사용자 지정 구현입니다
arg0
arg1
.A custom IFormatProvider implementation that provides formatting information forarg0
andarg1
. 일반적으로 이러한 구현은 인터페이스도 구현 ICustomFormatter 합니다.Typically, such an implementation also implements the ICustomFormatter interface.
provider
매개 변수가 이면 null
현재 문화권에서 형식 공급자 정보를 가져옵니다.If the provider
parameter is null
, format provider information is obtained from the current culture.
arg0
및 arg1
는 형식을 지정할 개체를 나타냅니다.arg0
and arg1
represent the objects to be formatted. 의 각 서식 항목 format
은 해당 인덱스를 포함 하는 개체의 문자열 표현으로 바뀝니다.Each format item in format
is replaced with the string representation of the object that has the corresponding index. 형식 항목에가 포함 formatString
되 고 해당 인수가 인터페이스를 구현 하는 경우 IFormattable 인수의 ToString(formatString, provider)
메서드는 형식을 정의 합니다.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. 그렇지 않으면 인수의 ToString()
메서드가 서식 지정을 정의 합니다.Otherwise, the argument's ToString()
method defines the formatting.
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.
적용 대상
AppendFormat(String, Object, Object, Object)
서식 항목이 0개 이상 포함된 복합 서식 문자열을 처리하여 반환된 문자열을 이 인스턴스에 추가합니다.Appends the string returned by processing a composite format string, which contains zero or more format items, to this instance. 각 서식 항목이 세 인수 중 하나의 문자열 표현으로 바뀝니다.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
매개 변수
- format
- String
복합 형식 문자열입니다.A composite format string.
- arg0
- Object
서식을 지정할 첫 번째 개체입니다.The first object to format.
- arg1
- Object
서식을 지정할 두 번째 개체입니다.The second object to format.
- arg2
- Object
서식을 지정할 세 번째 개체입니다.The third object to format.
반환
format
이 추가된 이 인스턴스에 대한 참조입니다.A reference to this instance with format
appended. format
의 각 서식 항목은 해당 개체 인수의 문자열 표현으로 바뀝니다.Each format item in format
is replaced by the string representation of the corresponding object argument.
예외
format
은 null
입니다.format
is null
.
format
이 잘못되었습니다.format
is invalid.
또는-or-
형식 항목의 인덱스가 0보다 작거나, 3보다 크거나 같습니다.The index of a format item is less than 0 (zero), or greater than or equal to 3.
확장된 문자열의 길이가 MaxCapacity를 초과합니다.The length of the expanded string would exceed MaxCapacity.
예제
다음 예제는 AppendFormat 메서드.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
설명
이 메서드는 .NET Framework의 복합 서식 지정 기능 을 사용 하 여 개체의 값을 해당 텍스트 표현으로 변환 하 고 현재 개체에 해당 표현을 포함 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.
format
매개 변수는 arg0
arg2
이 메서드의 매개 변수 목록에 있는 개체에 해당 하는 형식 항목 이라고 하는 0 개 이상의 인덱싱된 자리 표시자와 결합 된 0 개 이상의 텍스트 실행으로 구성 됩니다.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. 서식 지정 프로세스에서는 각 형식 항목을 해당 개체의 문자열 표현으로 바꿉니다.The formatting process replaces each format item with the string representation of the corresponding object.
서식 항목의 구문은 다음과 같습니다.The syntax of a format item is as follows:
{index[,length] [:formatString]}{index[,length][:formatString]}
대괄호 안의 요소는 선택 사항입니다.Elements in square brackets are optional. 다음 표에서는 각 요소에 대해 설명합니다.The following table describes each element.
요소Element | 설명Description |
---|---|
indexindex | 형식을 지정할 개체의 매개 변수 목록에서 0부터 시작 하는 위치입니다.The zero-based position in the parameter list of the object to be formatted. Index 로 지정 된 개체가 이면 null 형식 항목이로 바뀝니다 String.Empty .If the object specified by index is null , the format item is replaced by String.Empty. 인덱스 위치에 매개 변수가 없으면 FormatException 이 throw 됩니다.If there is no parameter in the index position, a FormatException is thrown. |
,길이,length | 매개 변수의 문자열 표현에 있는 최소 문자 수입니다.The minimum number of characters in the string representation of the parameter. 양수 이면 매개 변수가 오른쪽에 맞춰집니다. 음수 이면 왼쪽에 맞춰집니다.If positive, the parameter is right-aligned; if negative, it is left-aligned. |
:formatString:formatString | 매개 변수에서 지 원하는 표준 또는 사용자 지정 서식 문자열입니다.A standard or custom format string that is supported by the parameter. |
참고
날짜 및 시간 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 날짜 및 시간 형식 문자열 및 사용자 지정 날짜 및 시간 형식 문자열을 참조 하세요.For 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. 숫자 값에 사용 되는 표준 및 사용자 지정 서식 문자열은 표준 숫자 형식 문자열 및 사용자 지정 숫자 형식 문자열을 참조 하세요.For the standard and custom format strings used with numeric values, see Standard Numeric Format Strings and Custom Numeric Format Strings. 열거형에 사용 되는 표준 형식 문자열은 열거형 형식 문자열을 참조 하세요.For the standard format strings used with enumerations, see Enumeration Format Strings.
arg0
, arg1
및 arg2
는 형식을 지정할 개체를 나타냅니다.arg0
, arg1
, and arg2
represent the objects to be formatted. 의 각 서식 항목 format
은 arg0
arg1
arg2
형식 항목의 인덱스에 따라, 또는의 문자열 표현으로 바뀝니다.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. 형식 항목에가 포함 되어 formatString
있고의 해당 개체가 인터페이스를 구현 하는 경우 args
IFormattable arg
x .ToString(formatString, null)
는 형식을 정의 합니다. 여기서 x 는 인수의 인덱스입니다.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. 그렇지 않으면 arg
x .ToString()
는 형식을 정의 합니다.Otherwise, arg
x.ToString()
defines the formatting.
에 할당 된 문자열이 format
"자선 기구 조직의 {0: # # # #} 캔의 기부에 참여 해 주셔서 감사 합니다."If the string assigned to format
is "Thank you for your donation of {0:####} cans of food to our charitable organization." 및 arg0
는 값이 10 인 정수 이며, 반환 값은 "자선 기구 조직에 식량 10 캔의 기부에 대해 감사 합니다."가 됩니다.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."
호출자 참고
.NET Core 및 .NET Framework 4.0 이상 버전에서 StringBuilder 생성자를 호출 하 여 개체를 인스턴스화하면 StringBuilder(Int32, Int32) 인스턴스의 길이와 용량이 모두 StringBuilder 해당 속성의 값을 초과 하 여 커질 수 있습니다 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. 이는 특히 및 메서드를 호출 하 여 Append(String) 작은 문자열을 추가 하는 경우에 발생할 수 있습니다 AppendFormat(String, Object) .This can occur particularly when you call the Append(String) and AppendFormat(String, Object) methods to append small strings.