String.Format 方法

定義

根據指定的格式將物件的值轉換為字串,並將它們插入到另一個字串。

如果您不熟悉 String.Format 方法,請參閱 開始使用 String.Format 方法 以取得快速概觀。

多載

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, 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, Object, Object)

以三個指定物件的字串表示,取代字串中的格式項目。 參數提供特定文化特性格式資訊。

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

要格式化的第三個物件。

傳回

format 的複本,其中的格式項目已由 arg0arg1arg2 的字串表示取代。

例外狀況

formatnull

format 無效。

-或-

格式項目的索引小於零或大於二。

備註

重要

請不要呼叫 String.Format 方法或使用複合格式字串,您可以使用「內插字串」(如果您的語言支援它們的話)。 內插字串是包含「插入運算式」的字串。 每個插值的運算式會以運算式的值解析,且在字串指派時,包含在結果字串中。 如需詳細資訊,請參閱內插字串 (C# 參考)內插字串 (Visual Basic 參考)

這個方法會使用 複合格式功能 ,將三個運算式轉換成其字串表示,並將這些表示內嵌在字串中。 在執行轉換時,方法會使用區分文化特性的格式或自定義格式器。 方法會呼叫 ToString (IFormatProvider) 方法,或呼叫其 ToString (String,IFormatProvider) 方法呼叫其 ToString (String,IFormatProvider) 方法,將每個Object自變數轉換成其字元串表示。 如果這些方法不存在,它會呼叫物件的無參數 ToString 方法。

不過,呼叫 String.Format 方法時,不需要將焦點放在您想要呼叫的特定多載上。 相反地,您可以使用提供區分文化特性或自訂格式的物件和包含一或多個格式項目的複合格式字串來呼叫該方法。 請為每個格式項目指派一個數值索引;第一個索引會從 0 開始。 除了初始字串之外,您的方法呼叫應該具有與其索引值數目相同的其他引數。 例如,其格式項目具有索引 0 和 1 的字串應該有 2 個引數;具有索引 0 到 5 的字串應該有 6 個引數。 語言編譯程式接著會將方法呼叫解析為方法的特定多 String.Format 載。

如需使用 String.Format 方法的詳細檔,請參閱開始使用 String.Format 方法和我呼叫哪一種方法?

適用於

Format(String, Object, Object, Object)

以三個指定物件的字串表示,取代字串中的格式項目。

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

要格式化的第三個物件。

傳回

format 的複本,其中的格式項目已由 arg0arg1arg2 的字串表示取代。

例外狀況

formatnull

format 無效。

-或-

格式項目的索引小於零或大於二。

備註

重要

請不要呼叫 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 整數值。 請注意,格式字串包含六個格式專案,但方法在其參數清單中只有三個專案,因為每個專案都以兩種不同的方式格式化。

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)

另請參閱

適用於

Format(IFormatProvider, String, Object, Object)

以兩個指定物件的字串表示,取代字串中的格式項目。 參數提供特定文化特性格式資訊。

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

要格式化的第二個物件。

傳回

format 的複本,其中的格式項目已由 arg0arg1 的字串表示取代。

例外狀況

formatnull

format 無效。

-或-

格式項目的索引不是零或一。

備註

重要

請不要呼叫 String.Format 方法或使用複合格式字串,您可以使用「內插字串」(如果您的語言支援它們的話)。 內插字串是包含「插入運算式」的字串。 每個插值的運算式會以運算式的值解析,且在字串指派時,包含在結果字串中。 如需詳細資訊,請參閱內插字串 (C# 參考)內插字串 (Visual Basic 參考)

這個方法會使用 複合格式功能 ,將兩個表達式轉換成其字串表示法,並將這些表示法內嵌在字串中。 在執行轉換時,方法會使用區分文化特性的格式或自定義格式器。 方法會藉由呼叫 ToString (IFormatProvider) 方法,或如果對象的對應格式專案包含格式字串,藉由呼叫其 ToString (String,IFormatProvider) 方法,將每個Object自變數轉換成其字元串表示。 如果這些方法不存在,它會呼叫物件的無參數 ToString 方法。

不過,呼叫 String.Format 方法時,不需要將焦點放在您想要呼叫的特定多載上。 相反地,您可以使用提供區分文化特性或自訂格式的物件和包含一或多個格式項目的複合格式字串來呼叫該方法。 請為每個格式項目指派一個數值索引;第一個索引會從 0 開始。 除了初始字串之外,您的方法呼叫應該具有與其索引值數目相同的其他引數。 例如,其格式項目具有索引 0 和 1 的字串應該有 2 個引數;具有索引 0 到 5 的字串應該有 6 個引數。 語言編譯程式接著會將方法呼叫解析為 方法的特定多載 String.Format

如需使用 String.Format 方法的詳細檔,請參閱開始使用 String.Format 方法和我呼叫哪一種方法?

適用於

Format(String, Object, Object)

以兩個指定物件的字串表示,取代字串中的格式項目。

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

要格式化的第二個物件。

傳回

format 的複本,其中的格式項目已由 arg0arg1 的字串表示取代。

例外狀況

formatnull

format 無效。

-或-

格式項目的索引不是零或一。

備註

重要

請不要呼叫 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

另請參閱

適用於

Format(IFormatProvider, CompositeFormat, ReadOnlySpan<Object>)

將中的 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);
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>

要格式化的物件範圍。

傳回

格式化的字串。

例外狀況

formatnull

格式專案的索引大於或等於提供的自變數數目。

適用於

Format(IFormatProvider, String, Object)

以對應物件的字串表示,取代指定之字串中的一或多個格式項目。 參數提供特定文化特性格式資訊。

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

要格式化的物件。

傳回

format 的複本,其中的一或多個格式項目已由 arg0 的字串表示取代。

例外狀況

formatnull

format 無效。

-或-

格式項目的索引不是零。

備註

重要

請不要呼叫 String.Format 方法或使用複合格式字串,您可以使用「內插字串」(如果您的語言支援它們的話)。 內插字串是包含「插入運算式」的字串。 每個插值的運算式會以運算式的值解析,且在字串指派時,包含在結果字串中。 如需詳細資訊,請參閱內插字串 (C# 參考)內插字串 (Visual Basic 參考)

這個方法會使用 複合格式功能 ,將表達式的值轉換成其字串表示法,並將該表示法內嵌在字串中。 在執行轉換時,方法會使用區分文化特性的格式或自定義格式器。 方法會 arg0 呼叫 其 ToString (IFormatProvider) 方法,或者,如果對象的對應格式專案包含格式字串,則呼叫其 ToString (String,IFormatProvider) 方法, 以轉換成其字串表示。 如果這些方法不存在,它會呼叫物件的無參數 ToString 方法。

不過,呼叫 String.Format 方法時,不需要將焦點放在您想要呼叫的特定多載上。 相反地,您可以使用提供區分文化特性或自訂格式的物件和包含一或多個格式項目的複合格式字串來呼叫該方法。 請為每個格式項目指派一個數值索引;第一個索引會從 0 開始。 除了初始字串之外,您的方法呼叫應該具有與其索引值數目相同的其他引數。 例如,其格式項目具有索引 0 和 1 的字串應該有 2 個引數;具有索引 0 到 5 的字串應該有 6 個引數。 語言編譯程式接著會將方法呼叫解析為 方法的特定多載 String.Format

如需使用 String.Format 方法的詳細檔,請參閱開始使用 String.Format 方法和我呼叫哪一種方法?

適用於

Format(IFormatProvider, String, Object[])

以指定陣列中對應物件的字串表示,取代字串中的格式項目。 參數提供特定文化特性格式資訊。

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[]

物件陣列,包含零或多個要格式化的物件。

傳回

format 的複本,其中的格式項目已由 args 中對應物件的字串表示取代。

例外狀況

formatargsnull

format 無效。

-或-

格式項目的索引小於零,或大於等於 args 陣列的長度。

備註

重要

請不要呼叫 String.Format 方法或使用複合格式字串,您可以使用「內插字串」(如果您的語言支援它們的話)。 內插字串是包含「插入運算式」的字串。 每個插值的運算式會以運算式的值解析,且在字串指派時,包含在結果字串中。 如需詳細資訊,請參閱內插字串 (C# 參考)內插字串 (Visual Basic 參考)

這個方法會使用 複合格式功能 ,將四個或多個運算式轉換成其字串表示法,並在字串中內嵌這些表示法。 在執行轉換時,方法會使用區分文化特性的格式或自定義格式器。 方法會呼叫 ToString (IFormatProvider) 方法,或呼叫其 ToString (String,IFormatProvider) 方法呼叫其 ToString (String,IFormatProvider) 方法,將每個Object自變數轉換成其字元串表示。 如果這些方法不存在,它會呼叫物件的無參數 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(String, Object[])

以指定陣列中對應物件的字串表示,取代指定之字串中的格式項目。

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[]

物件陣列,包含零或多個要格式化的物件。

傳回

format 的複本,其中的格式項目已由 args 中對應物件的字串表示取代。

例外狀況

formatargsnull

format 無效。

-或-

格式項目的索引小於零,或大於等於 args 陣列的長度。

備註

重要

請不要呼叫 String.Format 方法或使用複合格式字串,您可以使用「內插字串」(如果您的語言支援它們的話)。 內插字串是包含「插入運算式」的字串。 每個插值的運算式會以運算式的值解析,且在字串指派時,包含在結果字串中。 如需詳細資訊,請參閱內插字串 (C# 參考)內插字串 (Visual Basic 參考)

這個方法會使用 複合格式功能 ,將四個或多個表達式的值轉換成其字串表示法,並將這些表示法內嵌在字串中。 args由於 參數會以 System.ParamArrayAttribute 屬性標示,因此您可以將物件以個別自變數或Object陣列的形式傳遞至 方法。

不過,呼叫 String.Format 方法時,不需要將焦點放在您想要呼叫的特定多載上。 相反地,您可以使用包含一或多個格式項目的複合格式字串來呼叫該方法。 請為每個格式項目指派一個數值索引;第一個索引會從 0 開始。 除了初始字串之外,您的方法呼叫應該具有與其索引值數目相同的其他引數。 例如,其格式項目具有索引 0 和 1 的字串應該有 2 個引數;具有索引 0 到 5 的字串應該有 6 個引數。 語言編譯程式接著會將方法呼叫解析為方法的特定多 String.Format 載。

如需使用 String.Format 方法的詳細檔,請參閱開始使用 String.Format 方法和我呼叫哪一種方法?

範例:格式化三個以上的自變數

本範例會建立字串,其中包含特定日期上高溫度和低溫度的數據。 複合格式字串在 C# 範例中有五個格式專案,而 Visual Basic 範例中有六個。 其中兩個格式專案會定義其對應值字串表示的寬度,而第一個格式專案也包含標準日期和時間格式字串。

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

另請參閱

適用於

Format(String, Object)

以指定物件的字串表示,取代字串中的一或多個格式項目。

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

要格式化的物件。

傳回

format 的複本,其中的所有格式項目已由 arg0 的字串表示取代。

例外狀況

formatnull

format 中的格式項目無效。

-或-

格式項目的索引不是零。

備註

重要

請不要呼叫 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.

另請參閱

適用於

Format(IFormatProvider, CompositeFormat, Object[])

將中的 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[]

要格式化的物件陣列。

傳回

格式化的字串。

例外狀況

formatargsnull

格式專案的索引大於或等於提供的自變數數目。

適用於

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

將中的 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

要格式化的第三個物件。

傳回

格式化的字串。

例外狀況

formatnull

格式專案的索引大於或等於提供的自變數數目。

適用於

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

將中的 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

要格式化的第二個物件。

傳回

格式化的字串。

例外狀況

formatnull

格式專案的索引大於或等於提供的自變數數目。

適用於

Format<TArg0>(IFormatProvider, CompositeFormat, TArg0)

將中的 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

要格式化的第一個物件。

傳回

格式化的字串。

例外狀況

formatnull

格式專案的索引大於或等於提供的自變數數目。

適用於