다음을 통해 공유


String.Format 메서드

정의

지정된 형식에 따라 개체 값을 문자열로 변환하고 다른 문자열에 삽입합니다.

String.Format 메서드를 처음으로 사용하는 경우 빠른 개요는 String.Format 메서드 시작하기를 참조하세요.

오버로드

Format(IFormatProvider, String, Object[])

문자열의 서식 항목을 지정된 배열에 있는 해당 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

Format(IFormatProvider, String, Object, Object, Object)

문자열의 서식 항목을 지정된 세 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

Format(String, Object, Object, Object)

문자열의 서식 항목을 지정된 세 개체의 문자열 표현으로 바꿉니다.

Format(IFormatProvider, String, Object, Object)

문자열의 서식 항목을 지정된 두 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

Format(String, Object, Object)

문자열의 서식 항목을 지정된 두 개체의 문자열 표현으로 바꿉니다.

Format(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

Format(IFormatProvider, String, Object)

지정된 문자열의 서식 항목 또는 항목을 해당 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

Format(IFormatProvider, String, ReadOnlySpan<Object>)

문자열의 서식 항목을 지정된 범위에 있는 해당 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

Format(String, ReadOnlySpan<Object>)

지정된 문자열의 서식 항목을 지정된 범위에 있는 해당 개체의 문자열 표현으로 바꿉니다.

Format(String, Object[])

지정된 문자열의 서식 항목을 지정된 배열에 있는 해당 개체의 문자열 표현으로 바꿉니다.

Format(String, Object)

문자열에 있는 하나 이상의 서식 항목을 지정된 개체의 문자열 표현으로 바꿉니다.

Format(IFormatProvider, CompositeFormat, Object[])

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

Format<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

Format<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

Format<TArg0>(IFormatProvider, CompositeFormat, TArg0)

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

설명

이 API에 대한 자세한 내용은 String.Format대한 추가 API 비고를 참조하세요.

Format(IFormatProvider, String, Object[])

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

문자열의 서식 항목을 지정된 배열에 있는 해당 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format (IFormatProvider provider, string format, params object[] args);
public static string Format (IFormatProvider? provider, string format, params object?[] args);
static member Format : IFormatProvider * string * obj[] -> string
Public Shared Function Format (provider As IFormatProvider, format As String, ParamArray args As Object()) As String

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

args
Object[]

서식을 지정할 개체가 0개 이상 포함된 개체 배열입니다.

반환

서식 항목이 args해당 개체의 문자열 표현으로 대체된 format 복사본입니다.

예외

format 또는 argsnull.

format 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0보다 작거나 args 배열의 길이보다 크거나 같습니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 4개 이상의 식을 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다. 변환을 수행할 때 메서드는 문화권 구분 서식 또는 사용자 지정 포맷터를 사용합니다. 메서드는 ToString(IFormatProvider) 메서드를 호출하여 각 Object 인수를 문자열 표현으로 변환하거나 개체의 해당 형식 항목에 형식 문자열이 포함된 경우 ToString(String,IFormatProvider) 메서드를 호출합니다. 이러한 메서드가 없으면 개체의 매개 변수가 없는 ToString 메서드를 호출합니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 문화권 구분 또는 사용자 지정 서식을 제공하는 개체와 하나 이상의 서식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

예: 문화권 구분 서식 지정

이 예제에서는 Format(IFormatProvider, String, Object[]) 메서드를 사용하여 여러 다른 문화권을 사용하여 일부 날짜 및 시간 값과 숫자 값의 문자열 표현을 표시합니다.

string[] cultureNames = { "en-US", "fr-FR", "de-DE", "es-ES" };

DateTime dateToDisplay = new DateTime(2009, 9, 1, 18, 32, 0);
double value = 9164.32;

Console.WriteLine("Culture     Date                                Value\n");
foreach (string cultureName in cultureNames)
{
   System.Globalization.CultureInfo culture = new System.Globalization.CultureInfo(cultureName);
   string output = String.Format(culture, "{0,-11} {1,-35:D} {2:N}", 
                                 culture.Name, dateToDisplay, value);
   Console.WriteLine(output);
}    
// The example displays the following output:
//    Culture     Date                                Value
//    
//    en-US       Tuesday, September 01, 2009         9,164.32
//    fr-FR       mardi 1 septembre 2009              9 164,32
//    de-DE       Dienstag, 1. September 2009         9.164,32
//    es-ES       martes, 01 de septiembre de 2009    9.164,32
open System
open System.Globalization

let cultureNames = [| "en-US"; "fr-FR"; "de-DE"; "es-ES" |]

let dateToDisplay = DateTime(2009, 9, 1, 18, 32, 0)
let value = 9164.32

printfn "Culture     Date                                Value\n"
for cultureName in cultureNames do
    let culture = CultureInfo cultureName
    String.Format(culture, "{0,-11} {1,-35:D} {2:N}", culture.Name, dateToDisplay, value)
    |> printfn "%s"
// The example displays the following output:
//    Culture     Date                                Value
//    
//    en-US       Tuesday, September 01, 2009         9,164.32
//    fr-FR       mardi 1 septembre 2009              9 164,32
//    de-DE       Dienstag, 1. September 2009         9.164,32
//    es-ES       martes, 01 de septiembre de 2009    9.164,32
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR", "de-DE", "es-ES" }
      
      Dim dateToDisplay As Date = #9/1/2009 6:32PM#
      Dim value As Double = 9164.32

      Console.WriteLine("Culture     Date                                Value")
      Console.WriteLine()      
      For Each cultureName As String In cultureNames
         Dim culture As New CultureInfo(cultureName)
         Dim output As String = String.Format(culture, "{0,-11} {1,-35:D} {2:N}", _
                                              culture.Name, dateToDisplay, value)
         Console.WriteLine(output)
      Next    
   End Sub
End Module
' The example displays the following output:
'       Culture     Date                                Value
'       
'       en-US       Tuesday, September 01, 2009         9,164.32
'       fr-FR       mardi 1 septembre 2009              9 164,32
'       de-DE       Dienstag, 1. September 2009         9.164,32
'       es-ES       martes, 01 de septiembre de 2009    9.164,32

추가 정보

적용 대상

Format(IFormatProvider, String, Object, Object, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

문자열의 서식 항목을 지정된 세 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static string Format (IFormatProvider provider, string format, object arg0, object arg1, object arg2);
public static string Format (IFormatProvider? provider, string format, object? arg0, object? arg1, object? arg2);
static member Format : IFormatProvider * string * obj * obj * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object, arg2 As Object) As String

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

arg0
Object

서식을 지정할 첫 번째 개체입니다.

arg1
Object

서식을 지정할 두 번째 개체입니다.

arg2
Object

서식을 지정할 세 번째 개체입니다.

반환

서식 항목이 arg0, arg1arg2문자열 표현으로 대체된 format 복사본입니다.

예외

format null.

format 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0보다 작거나 2보다 큽니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 3개의 식을 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다. 변환을 수행할 때 메서드는 문화권 구분 서식 또는 사용자 지정 포맷터를 사용합니다. 메서드는 ToString(IFormatProvider) 메서드를 호출하여 각 Object 인수를 문자열 표현으로 변환하거나 개체의 해당 형식 항목에 형식 문자열이 포함된 경우 ToString(String,IFormatProvider) 메서드를 호출합니다. 이러한 메서드가 없으면 개체의 매개 변수가 없는 ToString 메서드를 호출합니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 문화권 구분 또는 사용자 지정 서식을 제공하는 개체와 하나 이상의 서식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

적용 대상

Format(String, Object, Object, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

문자열의 서식 항목을 지정된 세 개체의 문자열 표현으로 바꿉니다.

public:
 static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1, System::Object ^ arg2);
public static string Format (string format, object arg0, object arg1, object arg2);
public static string Format (string format, object? arg0, object? arg1, object? arg2);
static member Format : string * obj * obj * obj -> string
Public Shared Function Format (format As String, arg0 As Object, arg1 As Object, arg2 As Object) As String

매개 변수

arg0
Object

서식을 지정할 첫 번째 개체입니다.

arg1
Object

서식을 지정할 두 번째 개체입니다.

arg2
Object

서식을 지정할 세 번째 개체입니다.

반환

서식 항목이 arg0, arg1arg2문자열 표현으로 대체된 format 복사본입니다.

예외

format null.

format 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0보다 작거나 2보다 큽니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 세 식의 값을 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 하나 이상의 형식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

예: 세 개의 인수 서식 지정

이 예제에서는 Format(String, Object, Object, Object) 메서드를 사용하여 두 개의 정수 값이 있는 부울 And 작업의 결과를 보여 주는 문자열을 만듭니다. 형식 문자열에는 6개의 서식 항목이 포함되지만 각 항목의 형식은 서로 다른 두 가지 방법으로 지정되므로 메서드의 매개 변수 목록에는 3개의 항목만 있습니다.

using namespace System;

void main()
{
   String^ formatString = "    {0,10} ({0,8:X8})\n" + 
                           "And {1,10} ({1,8:X8})\n" + 
                           "  = {2,10} ({2,8:X8})";
   int value1 = 16932;
   int value2 = 15421;
   String^ result = String::Format(formatString, 
                                   value1, value2, value1 & value2);
   Console::WriteLine(result);
}
// The example displays the following output:
//                16932 (00004224)
//       And      15421 (00003C3D)
//         =         36 (00000024)
string formatString = "    {0,10} ({0,8:X8})\n" + 
                      "And {1,10} ({1,8:X8})\n" + 
                      "  = {2,10} ({2,8:X8})";
int value1 = 16932;
int value2 = 15421;
string result = String.Format(formatString, 
                              value1, value2, value1 & value2);
Console.WriteLine(result);
// The example displays the following output:
//                16932 (00004224)
//       And      15421 (00003C3D)
//         =         36 (00000024)
open System

let formatString = 
    "    {0,10} ({0,8:X8})\nAnd {1,10} ({1,8:X8})\n  = {2,10} ({2,8:X8})"

let value1 = 16932
let value2 = 15421
String.Format(formatString, value1, value2, value1 &&& value2)
|> printfn "%s"
// The example displays the following output:
//                16932 (00004224)
//       And      15421 (00003C3D)
//         =         36 (00000024)
Public Module Example
   Public Sub Main()
      Dim formatString As String = "    {0,10} ({0,8:X8})" + vbCrLf +  _
                                   "And {1,10} ({1,8:X8})" + vbCrLf + _
                                   "  = {2,10} ({2,8:X8})"
      Dim value1 As Integer = 16932
      Dim value2 As Integer = 15421
      Dim result As String = String.Format(formatString, _
                                           value1, value2, value1 And value2)
      Console.WriteLine(result)                          
   End Sub
End Module
' The example displays the following output:
'                16932 (00004224)
'       And      15421 (00003C3D)
'         =         36 (00000024)

추가 정보

  • .NET 형식
  • 복합 서식

적용 대상

Format(IFormatProvider, String, Object, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

문자열의 서식 항목을 지정된 두 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format (IFormatProvider provider, string format, object arg0, object arg1);
public static string Format (IFormatProvider? provider, string format, object? arg0, object? arg1);
static member Format : IFormatProvider * string * obj * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object, arg1 As Object) As String

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

arg0
Object

서식을 지정할 첫 번째 개체입니다.

arg1
Object

서식을 지정할 두 번째 개체입니다.

반환

서식 항목이 arg0arg1문자열 표현으로 대체되는 format 복사본입니다.

예외

format null.

format 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0 또는 1이 아닙니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 두 식을 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다. 변환을 수행할 때 메서드는 문화권 구분 서식 또는 사용자 지정 포맷터를 사용합니다. 메서드는 ToString(IFormatProvider) 메서드를 호출하여 각 Object 인수를 문자열 표현으로 변환하거나 개체의 해당 형식 항목에 형식 문자열이 포함된 경우 ToString(String,IFormatProvider) 메서드를 호출합니다. 이러한 메서드가 없으면 개체의 매개 변수가 없는 ToString 메서드를 호출합니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 문화권 구분 또는 사용자 지정 서식을 제공하는 개체와 하나 이상의 서식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

적용 대상

Format(String, Object, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

문자열의 서식 항목을 지정된 두 개체의 문자열 표현으로 바꿉니다.

public:
 static System::String ^ Format(System::String ^ format, System::Object ^ arg0, System::Object ^ arg1);
public static string Format (string format, object arg0, object arg1);
public static string Format (string format, object? arg0, object? arg1);
static member Format : string * obj * obj -> string
Public Shared Function Format (format As String, arg0 As Object, arg1 As Object) As String

매개 변수

arg0
Object

서식을 지정할 첫 번째 개체입니다.

arg1
Object

서식을 지정할 두 번째 개체입니다.

반환

서식 항목이 arg0arg1문자열 표현으로 대체되는 format 복사본입니다.

예외

format null.

format 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0 또는 1이 아닙니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 두 식의 값을 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 하나 이상의 형식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

예: 두 인수 서식 지정

이 예제에서는 Format(String, Object, Object) 메서드를 사용하여 제네릭 Dictionary<TKey,TValue> 개체에 저장된 시간 및 온도 데이터를 표시합니다. 서식 문자열에는 서식을 지정할 개체가 두 개뿐이지만 세 개의 서식 항목이 있습니다. 목록의 첫 번째 개체(날짜 및 시간 값)는 두 가지 형식 항목에서 사용되므로 첫 번째 서식 항목은 시간을 표시하고 두 번째 개체는 날짜를 표시합니다.

using namespace System;
using namespace System::Collections::Generic;

void main()
{
   Dictionary<DateTime, Double>^ temperatureInfo = gcnew Dictionary<DateTime, Double>(); 
   temperatureInfo->Add(DateTime(2010, 6, 1, 14, 0, 0), 87.46);
   temperatureInfo->Add(DateTime(2010, 12, 1, 10, 0, 0), 36.81);
      
   Console::WriteLine("Temperature Information:\n");
   String^ output;   
   for each (KeyValuePair<DateTime, Double>^ item in temperatureInfo)
   {
      output = String::Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}�F", 
                              item->Key, item->Value);
      Console::WriteLine(output);
   }
}
// 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
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);

Console.WriteLine("Temperature Information:\n");
string output;   
foreach (var item in temperatureInfo)
{
   output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", 
                          item.Key, item.Value);
   Console.WriteLine(output);
}
// The example displays output like the following:
//       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
open System
open System.Collections.Generic

let temperatureInfo = Dictionary<DateTime, float>() 
temperatureInfo.Add(DateTime(2010, 6, 1, 14, 0, 0), 87.46)
temperatureInfo.Add(DateTime(2010, 12, 1, 10, 0, 0), 36.81)

printfn $"Temperature Information:\n"
for item in temperatureInfo do
   String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", item.Key, item.Value)
   |> printfn "%s"
// The example displays output like the following:
//       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

Module Example
   Public Sub Main()
      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)
      
      Console.WriteLine("Temperature Information:")
      Console.WriteLine()
      Dim output As String   
      For Each item In temperatureInfo
         output = String.Format("Temperature at {0,8:t} on {0,9:d}: {1,5:N1}°F", _
                                item.Key, item.Value)
         Console.WriteLine(output)
      Next
   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 형식
  • 복합 서식
  • 표준 날짜 및 시간 형식 문자열
  • 사용자 지정 날짜 및 시간 형식 문자열
  • 표준 숫자 형식 문자열
  • 사용자 지정 숫자 형식 문자열
  • 표준 TimeSpan 형식 문자열
  • 사용자 지정 TimeSpan 형식 문자열
  • 열거형 형식 문자열

적용 대상

Format(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format (IFormatProvider? provider, System.Text.CompositeFormat format, ReadOnlySpan<object?> args);
public static string Format (IFormatProvider? provider, System.Text.CompositeFormat format, scoped ReadOnlySpan<object?> args);
static member Format : IFormatProvider * System.Text.CompositeFormat * ReadOnlySpan<obj> -> string
Public Shared Function Format (provider As IFormatProvider, format As CompositeFormat, args As ReadOnlySpan(Of Object)) As String

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

args
ReadOnlySpan<Object>

서식을 지정할 개체의 범위입니다.

반환

서식이 지정된 문자열입니다.

예외

format null.

형식 항목의 인덱스가 제공된 인수 수보다 크거나 같습니다.

적용 대상

Format(IFormatProvider, String, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

지정된 문자열의 서식 항목 또는 항목을 해당 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, System::Object ^ arg0);
public static string Format (IFormatProvider provider, string format, object arg0);
public static string Format (IFormatProvider? provider, string format, object? arg0);
static member Format : IFormatProvider * string * obj -> string
Public Shared Function Format (provider As IFormatProvider, format As String, arg0 As Object) As String

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

arg0
Object

서식을 지정할 개체입니다.

반환

서식 항목 또는 항목이 arg0문자열 표현으로 대체된 format 복사본입니다.

예외

format null.

format 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0이 아닙니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 식 값을 해당 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다. 변환을 수행할 때 메서드는 문화권 구분 서식 또는 사용자 지정 포맷터를 사용합니다. 메서드는 ToString(IFormatProvider) 메서드를 호출하여 arg0 문자열 표현으로 변환하거나, 개체의 해당 서식 항목에 형식 문자열이 포함된 경우 ToString(String,IFormatProvider) 메서드를 호출하여 문자열 표현으로 변환합니다. 이러한 메서드가 없으면 개체의 매개 변수가 없는 ToString 메서드를 호출합니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 문화권 구분 또는 사용자 지정 서식을 제공하는 개체와 하나 이상의 서식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

적용 대상

Format(IFormatProvider, String, ReadOnlySpan<Object>)

문자열의 서식 항목을 지정된 범위에 있는 해당 개체의 문자열 표현으로 바꿉니다. 매개 변수는 문화권별 서식 정보를 제공합니다.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::String ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format (IFormatProvider? provider, string format, scoped ReadOnlySpan<object?> args);
static member Format : IFormatProvider * string * ReadOnlySpan<obj> -> string
Public Shared Function Format (provider As IFormatProvider, format As String, args As ReadOnlySpan(Of Object)) As String

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

args
ReadOnlySpan<Object>

서식을 지정할 개체가 0개 이상 포함된 개체 범위입니다.

반환

서식 항목이 args해당 개체의 문자열 표현으로 대체된 format 복사본입니다.

적용 대상

Format(String, ReadOnlySpan<Object>)

지정된 문자열의 서식 항목을 지정된 범위에 있는 해당 개체의 문자열 표현으로 바꿉니다.

public:
 static System::String ^ Format(System::String ^ format, ReadOnlySpan<System::Object ^> args);
public static string Format (string format, scoped ReadOnlySpan<object?> args);
static member Format : string * ReadOnlySpan<obj> -> string
Public Shared Function Format (format As String, args As ReadOnlySpan(Of Object)) As String

매개 변수

args
ReadOnlySpan<Object>

서식을 지정할 개체가 0개 이상 포함된 개체 범위입니다.

반환

서식 항목이 args해당 개체의 문자열 표현으로 대체된 format 복사본입니다.

적용 대상

Format(String, Object[])

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

지정된 문자열의 서식 항목을 지정된 배열에 있는 해당 개체의 문자열 표현으로 바꿉니다.

public:
 static System::String ^ Format(System::String ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format (string format, params object[] args);
public static string Format (string format, params object?[] args);
static member Format : string * obj[] -> string
Public Shared Function Format (format As String, ParamArray args As Object()) As String

매개 변수

args
Object[]

서식을 지정할 개체가 0개 이상 포함된 개체 배열입니다.

반환

서식 항목이 args해당 개체의 문자열 표현으로 대체된 format 복사본입니다.

예외

format 또는 argsnull.

format 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0보다 작거나 args 배열의 길이보다 크거나 같습니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 4개 이상의 식 값을 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다. args 매개 변수는 System.ParamArrayAttribute 특성으로 표시되므로 개체를 개별 인수 또는 Object 배열로 메서드에 전달할 수 있습니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 하나 이상의 형식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

예: 세 개 이상의 인수 서식 지정

이 예제에서는 특정 날짜의 고온 및 저온 데이터를 포함하는 문자열을 만듭니다. 복합 형식 문자열에는 C# 예제에 5개의 형식 항목이 있고 Visual Basic 예제에서는 6개의 형식 항목이 있습니다. 형식 항목 중 두 가지는 해당 값의 문자열 표현 너비를 정의하며 첫 번째 서식 항목에는 표준 날짜 및 시간 형식 문자열도 포함됩니다.

using namespace System;

void main()
{
   DateTime date1 = DateTime(2009, 7, 1);
   TimeSpan hiTime = TimeSpan(14, 17, 32);
   Decimal hiTemp = (Decimal) 62.1; 
   TimeSpan loTime = TimeSpan(3, 16, 10);
   Decimal loTemp = (Decimal)54.8; 

   String^ result1 = String::Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", 
                                    date1, hiTime, hiTemp, loTime, loTemp);
   Console::WriteLine(result1);
   Console::WriteLine();
           
   String^ result2 = String::Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", 
                                    gcnew array<Object^> { date1, hiTime, hiTemp, loTime, loTemp });
   Console::WriteLine(result2);
}
// The example displays the following output:
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
DateTime date1 = new DateTime(2009, 7, 1);
TimeSpan hiTime = new TimeSpan(14, 17, 32);
decimal hiTemp = 62.1m; 
TimeSpan loTime = new TimeSpan(3, 16, 10);
decimal loTemp = 54.8m; 

string result1 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", 
                               date1, hiTime, hiTemp, loTime, loTemp);
Console.WriteLine(result1);
Console.WriteLine();
     
string result2 = String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", 
                               new object[] { date1, hiTime, hiTemp, loTime, loTemp });
Console.WriteLine(result2);
// The example displays output like the following:
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
let date1 = DateTime(2009, 7, 1)
let hiTime = TimeSpan(14, 17, 32)
let hiTemp = 62.1m 
let loTime = TimeSpan(3, 16, 10)
let loTemp = 54.8m 

String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", date1, hiTime, hiTemp, loTime, loTemp)
|> printfn "%s\n"
      
String.Format("Temperature on {0:d}:\n{1,11}: {2} degrees (hi)\n{3,11}: {4} degrees (lo)", [| date1 :> obj; hiTime; hiTemp; loTime; loTemp |])
|> printfn "%s"
// The example displays output like the following:
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
//       Temperature on 7/1/2009:
//          14:17:32: 62.1 degrees (hi)
//          03:16:10: 54.8 degrees (lo)
Module Example
   Public Sub Main()
      Dim date1 As Date = #7/1/2009#
      Dim hiTime As New TimeSpan(14, 17, 32)
      Dim hiTemp As Decimal = 62.1d 
      Dim loTime As New TimeSpan(3, 16, 10)
      Dim loTemp As Decimal = 54.8d 

      Dim result1 As String = String.Format("Temperature on {0:d}:{5}{1,11}: {2} degrees (hi){5}{3,11}: {4} degrees (lo)", _
                                           date1, hiTime, hiTemp, loTime, loTemp, vbCrLf)
      Console.WriteLine(result1)
      Console.WriteLine()
           
      Dim result2 As String = String.Format("Temperature on {0:d}:{5}{1,11}: {2} degrees (hi){5}{3,11}: {4} degrees (lo)", _
                                            New Object() { date1, hiTime, hiTemp, loTime, loTemp, vbCrLf })
      Console.WriteLine(result2)                                            
   End Sub
End Module
' The example displays the following output:
'       Temperature on 7/1/2009:
'          14:17:32: 62.1 degrees (hi)
'          03:16:10: 54.8 degrees (lo)
'
'       Temperature on 7/1/2009:
'          14:17:32: 62.1 degrees (hi)
'          03:16:10: 54.8 degrees (lo)

인수 목록이 아닌 배열로 서식을 지정할 개체를 전달할 수도 있습니다.

using namespace System;

ref class CityInfo
{
public:
   CityInfo(String^ name, int population, Decimal area, int year)
   {
      this->Name = name;
      this->Population = population;
      this->Area = area;
      this->Year = year;
   }
   
   String^ Name; 
   int Population;
   Decimal Area;
   int Year;
};

ref class Example
{
public:
   static void ShowPopulationData(CityInfo^ city)
   {
      array<Object^>^ args = gcnew array<Object^> { city->Name, city->Year, city->Population, city->Area };
      String^ result = String::Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", 
                                    args);
      Console::WriteLine(result); 
   }
};

void main()
{
   CityInfo^ nyc2010 = gcnew CityInfo("New York", 8175133, (Decimal) 302.64, 2010);
   Example::ShowPopulationData(nyc2010);
   CityInfo^ sea2010 = gcnew CityInfo("Seattle", 608660, (Decimal) 83.94, 2010);      
   Example::ShowPopulationData(sea2010); 
}
// The example displays the following output:
//       New York in 2010: Population 8,175,133, Area 302.6 sq. feet
//       Seattle in 2010: Population 608,660, Area 83.9 sq. feet
using System;

public class CityInfo
{
   public CityInfo(String name, int population, Decimal area, int year)
   {
      this.Name = name;
      this.Population = population;
      this.Area = area;
      this.Year = year;
   }
   
   public readonly String Name; 
   public readonly int Population;
   public readonly Decimal Area;
   public readonly int Year;
}

public class Example
{
   public static void Main()
   {
      CityInfo nyc2010 = new CityInfo("New York", 8175133, 302.64m, 2010);
      ShowPopulationData(nyc2010);
      CityInfo sea2010 = new CityInfo("Seattle", 608660, 83.94m, 2010);      
      ShowPopulationData(sea2010); 
   }

   private static void ShowPopulationData(CityInfo city)
   {
      object[] args = { city.Name, city.Year, city.Population, city.Area };
      String result = String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", 
                                    args);
      Console.WriteLine(result); 
   }
}
// The example displays the following output:
//       New York in 2010: Population 8,175,133, Area 302.6 sq. feet
//       Seattle in 2010: Population 608,660, Area 83.9 sq. feet
open System

type CityInfo =
  { Name: string
    Population: int
    Area: Decimal
    Year: int }

let showPopulationData city =
    let args: obj[] = [| city.Name; city.Year; city.Population; city.Area |]
    String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", args)
    |> printfn "%s"

{ Name = "New York"; Population = 8175133; Area = 302.64m; Year = 2010 }
|> showPopulationData

 
{ Name = "Seattle"; Population = 608660; Area = 83.94m; Year = 2010 }      
|> showPopulationData 

// The example displays the following output:
//       New York in 2010: Population 8,175,133, Area 302.6 sq. feet
//       Seattle in 2010: Population 608,660, Area 83.9 sq. feet
Public Class CityInfo
   Public Sub New(name As String, population As Integer, area As Decimal, year As Integer)
      Me.Name = name
      Me.Population = population
      Me.Area = area
      Me.Year = year
   End Sub
   
   Public ReadOnly Name As String
   Public ReadOnly Population As Integer
   Public ReadOnly Area As Decimal
   Public ReadOnly Year As Integer
End Class

Module Example
   Public Sub Main()
      Dim nyc2010 As New CityInfo("New York", 8175133, 302.64d, 2010)
      ShowPopulationData(nyc2010)
      Dim sea2010 As New CityInfo("Seattle", 608660, 83.94d, 2010)      
      ShowPopulationData(sea2010) 
   End Sub
   
   Private Sub ShowPopulationData(city As CityInfo)
      Dim args() As Object = { city.Name, city.Year, city.Population, city.Area }
      Dim result = String.Format("{0} in {1}: Population {2:N0}, Area {3:N1} sq. feet", args)
      Console.WriteLine(result) 
   End Sub
End Module
' The example displays the following output:
'       New York in 2010: Population 8,175,133, Area 302.6 sq. feet
'       Seattle in 2010: Population 608,660, Area 83.9 sq. feet

추가 정보

  • .NET 형식
  • 복합 서식
  • 표준 날짜 및 시간 형식 문자열
  • 사용자 지정 날짜 및 시간 형식 문자열
  • 표준 숫자 형식 문자열
  • 사용자 지정 숫자 형식 문자열
  • 표준 TimeSpan 형식 문자열
  • 사용자 지정 TimeSpan 형식 문자열
  • 열거형 형식 문자열

적용 대상

Format(String, Object)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

문자열에 있는 하나 이상의 서식 항목을 지정된 개체의 문자열 표현으로 바꿉니다.

public:
 static System::String ^ Format(System::String ^ format, System::Object ^ arg0);
public static string Format (string format, object arg0);
public static string Format (string format, object? arg0);
static member Format : string * obj -> string
Public Shared Function Format (format As String, arg0 As Object) As String

매개 변수

arg0
Object

서식을 지정할 개체입니다.

반환

형식 항목이 arg0문자열 표현으로 대체되는 format 복사본입니다.

예외

format null.

format 형식 항목이 잘못되었습니다.

-또는-

형식 항목의 인덱스가 0이 아닙니다.

설명

중요하다

String.Format 메서드를 호출하거나복합 형식 문자열을 사용하는 대신 언어에서 지원하는 경우 보간된 문자열 사용할 수 있습니다. 보간된 문자열은보간된 식을 포함하는 문자열입니다. 보간된 각 식은 식의 값으로 확인되고 문자열이 할당될 때 결과 문자열에 포함됩니다. 자세한 내용은 문자열 보간(C# 참조) 및 보간된 문자열 (Visual Basic 참조)참조하세요.

이 메서드는 복합 서식 지정 기능 사용하여 식 값을 해당 문자열 표현으로 변환하고 해당 표현을 문자열에 포함합니다.

그러나 String.Format 메서드를 호출할 때 호출하려는 특정 오버로드에 집중할 필요는 없습니다. 대신 하나 이상의 형식 항목을 포함하는 복합 서식 문자열 사용하여 메서드를 호출할 수 있습니다. 각 형식 항목에 숫자 인덱스 할당 첫 번째 인덱스는 0에서 시작합니다. 초기 문자열 외에도 메서드 호출에는 인덱스 값이 있는 만큼의 추가 인수가 있어야 합니다. 예를 들어 형식 항목의 인덱스가 0과 1인 문자열에는 2개의 인수가 있어야 합니다. 인덱스가 0~5인 하나에는 6개의 인수가 있어야 합니다. 그러면 언어 컴파일러가 메서드 호출을 String.Format 메서드의 특정 오버로드로 확인합니다.

String.Format 메서드 사용에 대한 자세한 설명서는 String.Format 메서드 시작 및 어떤 메서드를 호출합니까?.

예: 단일 인수 서식 지정

다음 예제에서는 Format(String, Object) 메서드를 사용하여 문자열 중간에 개인의 나이를 포함합니다.

using namespace System;

void main()
{
   DateTime birthdate = DateTime(1993, 7, 28);
   array<DateTime>^ dates = gcnew array<DateTime> { DateTime(1993, 8, 16), 
                                                    DateTime(1994, 7, 28), 
                                                    DateTime(2000, 10, 16), 
                                                    DateTime(2003, 7, 27), 
                                                    DateTime(2007, 5, 27) };

   for each (DateTime dateValue in dates)
   {
      TimeSpan interval = dateValue - birthdate;
      // Get the approximate number of years, without accounting for leap years.
      int years = ((int)interval.TotalDays) / 365;
      // See if adding the number of years exceeds dateValue.
      String^ output;
      if (birthdate.AddYears(years) <= dateValue) {
         output = String::Format("You are now {0} years old.", years);
         Console::WriteLine(output);
      }   
      else {
         output = String::Format("You are now {0} years old.", years - 1);
         Console::WriteLine(output);
      }      
   }
}
// The example displays the following output:
//       You are now 0 years old.
//       You are now 1 years old.
//       You are now 7 years old.
//       You are now 9 years old.
//       You are now 13 years old.
DateTime birthdate = new DateTime(1993, 7, 28);
DateTime[] dates = { new DateTime(1993, 8, 16), 
                     new DateTime(1994, 7, 28), 
                     new DateTime(2000, 10, 16), 
                     new DateTime(2003, 7, 27), 
                     new DateTime(2007, 5, 27) };

foreach (DateTime dateValue in dates)
{
   TimeSpan interval = dateValue - birthdate;
   // Get the approximate number of years, without accounting for leap years.
   int years = ((int) interval.TotalDays) / 365;
   // See if adding the number of years exceeds dateValue.
   string output;
   if (birthdate.AddYears(years) <= dateValue) {
      output = String.Format("You are now {0} years old.", years);
      Console.WriteLine(output);
   }   
   else {
      output = String.Format("You are now {0} years old.", years - 1);
      Console.WriteLine(output);
   }      
}
// The example displays the following output:
//       You are now 0 years old.
//       You are now 1 years old.
//       You are now 7 years old.
//       You are now 9 years old.
//       You are now 13 years old.
let birthdate = DateTime(1993, 7, 28)
let dates = 
    [ DateTime(1993, 8, 16) 
      DateTime(1994, 7, 28)
      DateTime(2000, 10, 16)
      DateTime(2003, 7, 27)
      DateTime(2007, 5, 27) ]

for dateValue in dates do
    let interval = dateValue - birthdate
    // Get the approximate number of years, without accounting for leap years.
    let years = (int interval.TotalDays) / 365
    // See if adding the number of years exceeds dateValue.
    if birthdate.AddYears years <= dateValue then
        String.Format("You are now {0} years old.", years)
    else
        String.Format("You are now {0} years old.", years - 1)
    |> printfn "%s"
// The example displays the following output:
//       You are now 0 years old.
//       You are now 1 years old.
//       You are now 7 years old.
//       You are now 9 years old.
//       You are now 13 years old.
Module Example
   Public Sub Main()
      Dim birthdate As Date = #7/28/1993#
      Dim dates() As Date = { #9/16/1993#, #7/28/1994#, #10/16/2000#, _
                              #7/27/2003#, #5/27/2007# }
      For Each dateValue As Date In dates
         Dim interval As TimeSpan = dateValue - birthdate
         ' Get the approximate number of years, without accounting for leap years.
         Dim years As Integer = CInt(interval.TotalDays) \ 365
         ' See if adding the number of years exceeds dateValue.
         Dim output As String
         If birthdate.AddYears(years) <= dateValue Then
            output = String.Format("You are now {0} years old.", years)
            Console.WriteLine(output)
         Else
            output = String.Format("You are now {0} years old.", years - 1)
            Console.WriteLine(output)   
         End If
      Next
   End Sub
End Module
' The example displays the following output:
'       You are now 0 years old.
'       You are now 1 years old.
'       You are now 7 years old.
'       You are now 9 years old.
'       You are now 13 years old.

추가 정보

  • .NET 형식
  • 복합 서식
  • 표준 날짜 및 시간 형식 문자열
  • 사용자 지정 날짜 및 시간 형식 문자열
  • 표준 숫자 형식 문자열
  • 사용자 지정 숫자 형식 문자열
  • 표준 TimeSpan 형식 문자열
  • 사용자 지정 TimeSpan 형식 문자열
  • 열거형 형식 문자열

적용 대상

Format(IFormatProvider, CompositeFormat, Object[])

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

public:
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, ... cli::array <System::Object ^> ^ args);
public static string Format (IFormatProvider? provider, System.Text.CompositeFormat format, params object?[] args);
static member Format : IFormatProvider * System.Text.CompositeFormat * obj[] -> string
Public Shared Function Format (provider As IFormatProvider, format As CompositeFormat, ParamArray args As Object()) As String

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

args
Object[]

서식을 지정할 개체의 배열입니다.

반환

서식이 지정된 문자열입니다.

예외

format 또는 argsnull.

형식 항목의 인덱스가 제공된 인수 수보다 크거나 같습니다.

적용 대상

Format<TArg0,TArg1,TArg2>(IFormatProvider, CompositeFormat, TArg0, TArg1, TArg2)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

public:
generic <typename TArg0, typename TArg1, typename TArg2>
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0, TArg1 arg1, TArg2 arg2);
public static string Format<TArg0,TArg1,TArg2> (IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1, TArg2 arg2);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 * 'TArg1 * 'TArg2 -> string
Public Shared Function Format(Of TArg0, TArg1, TArg2) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0, arg1 As TArg1, arg2 As TArg2) As String

형식 매개 변수

TArg0

서식을 지정할 첫 번째 개체의 형식입니다.

TArg1

형식을 지정할 두 번째 개체의 형식입니다.

TArg2

서식을 지정할 세 번째 개체의 형식입니다.

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

arg0
TArg0

서식을 지정할 첫 번째 개체입니다.

arg1
TArg1

서식을 지정할 두 번째 개체입니다.

arg2
TArg2

서식을 지정할 세 번째 개체입니다.

반환

서식이 지정된 문자열입니다.

예외

format null.

형식 항목의 인덱스가 제공된 인수 수보다 크거나 같습니다.

적용 대상

Format<TArg0,TArg1>(IFormatProvider, CompositeFormat, TArg0, TArg1)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

public:
generic <typename TArg0, typename TArg1>
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0, TArg1 arg1);
public static string Format<TArg0,TArg1> (IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0, TArg1 arg1);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 * 'TArg1 -> string
Public Shared Function Format(Of TArg0, TArg1) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0, arg1 As TArg1) As String

형식 매개 변수

TArg0

서식을 지정할 첫 번째 개체의 형식입니다.

TArg1

형식을 지정할 두 번째 개체의 형식입니다.

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

arg0
TArg0

서식을 지정할 첫 번째 개체입니다.

arg1
TArg1

서식을 지정할 두 번째 개체입니다.

반환

서식이 지정된 문자열입니다.

예외

format null.

형식 항목의 인덱스가 제공된 인수 수보다 크거나 같습니다.

적용 대상

Format<TArg0>(IFormatProvider, CompositeFormat, TArg0)

Source:
String.Manipulation.cs
Source:
String.Manipulation.cs

CompositeFormat 서식 항목 또는 항목을 지정된 형식의 해당 개체의 문자열 표현으로 바꿉니다.

public:
generic <typename TArg0>
 static System::String ^ Format(IFormatProvider ^ provider, System::Text::CompositeFormat ^ format, TArg0 arg0);
public static string Format<TArg0> (IFormatProvider? provider, System.Text.CompositeFormat format, TArg0 arg0);
static member Format : IFormatProvider * System.Text.CompositeFormat * 'TArg0 -> string
Public Shared Function Format(Of TArg0) (provider As IFormatProvider, format As CompositeFormat, arg0 As TArg0) As String

형식 매개 변수

TArg0

서식을 지정할 첫 번째 개체의 형식입니다.

매개 변수

provider
IFormatProvider

문화권별 서식 정보를 제공하는 개체입니다.

arg0
TArg0

서식을 지정할 첫 번째 개체입니다.

반환

서식이 지정된 문자열입니다.

예외

format null.

형식 항목의 인덱스가 제공된 인수 수보다 크거나 같습니다.

적용 대상