String.Format メソッド (IFormatProvider, String, Object[])
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Format ( _ provider As IFormatProvider, _ format As String, _ ParamArray args As Object() _ ) As String
Dim provider As IFormatProvider Dim format As String Dim args As Object() Dim returnValue As String returnValue = String.Format(provider, format, args)
public: static String^ Format ( IFormatProvider^ provider, String^ format, ... array<Object^>^ args )
public static function Format ( provider : IFormatProvider, format : String, ... args : Object[] ) : String
戻り値
書式項目が args の Object の対応インスタンスと等価の String に置換された format のコピー。


このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。
-
Format、AppendFormat、および、WriteLine の一部のオーバーロードでサポートされる複合書式指定の詳細については、「複合書式設定」を参照してください。
-
日時書式指定子の詳細については、「標準の DateTime 書式指定文字列」および「カスタム DateTime 書式指定文字列」を参照してください。
provider パラメータは、書式設定処理を調整するために使用するカスタム情報やカルチャ固有の情報を提供します。詳細については、「書式設定の概要」トピックの「書式設定の概要」を参照してください。
format パラメータは、ゼロ個以上のテキストに、このメソッドのパラメータ リストに指定されたオブジェクトと対応する、ゼロ個以上のインデックス付きプレースホルダ (書式項目と呼ばれる) を組み合わせて指定します。各書式項目は、書式設定プロセスで、対応するオブジェクト値のテキスト表現に置き換えられます。
書式項目は {index[,alignment][:formatString]} という構文で、それぞれ、インデックス (省略不可)、書式設定するテキストの長さと配置 (省略可)、および、対応するオブジェクトの値をどのように書式設定するかを制御する、書式指定子の文字列 (省略可) を指定します。書式項目の各要素を次に示します。
indexオブジェクトのリスト内のどの要素を書式化するかを示す 0 から始まる整数。index で指定されたオブジェクトが null 参照 (Visual Basic では Nothing) の場合、書式項目は空の文字列 ("") で置き換えられます。
alignment書式設定された値を格納する領域の最小幅を示す、省略可能な整数。書式設定された値の長さが alignment よりも小さい場合、その領域の余白は空白文字で埋められます。alignment が負の場合、書式設定された値は領域内で左詰めになります。alignment が正の場合、値は右詰めになります。alignment の指定を省略すると、領域の長さは書式設定された長さと同じになります。alignment を指定した場合、コンマは省略できません。
formatString書式指定子の文字列 (省略可能)。formatString が指定されておらず、対応する引数が IFormattable インターフェイスを実装している場合、null 参照 (Visual Basic では Nothing) が IFormattable.ToString 書式文字列として使用されます。そのため、IFormattable.ToString のすべての実装では、書式指定文字列として null 参照 (Visual Basic では Nothing) を許容し、オブジェクト表現の既定の書式を String として返す必要があります。formatString を指定した場合、コロンは省略できません。
先頭および末尾の中かっこ文字 '{' および '}' は必須です。format 内でリテラルな中かっこ文字を指定するには、"{{" または "}}" のように、先頭または末尾の中かっこ文字を 2 つ続けて指定します。
format の値が "『Microsoft® .NET (Core Reference)』を {0:####} 冊お買い上げいただき、ありがとうございました。" で、arg[0] が 123 という値を持つ Int16 だとすると、戻り値は次のようになります。
"『Microsoft® .NET (Core Reference)』を 123 冊お買い上げいただき、ありがとうございました。"
format の値が "ブラッドの飼っている犬には {0,-8:G} 匹のノミがいる。" で、arg[0] が 42 という値を持つ Int16 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれるスペースを表します。

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。
' This code example demonstrates the String.Format() method. ' Formatting for this example uses the "en-US" culture. Imports System Imports Microsoft.VisualBasic Class Sample Public Enum Color Yellow = 1 Blue = 2 Green = 3 End Enum 'Color Private Shared thisDate As DateTime = DateTime.Now Public Shared Sub Main() ' Store the output of the String.Format method in a string. Dim s As String = "" Console.Clear() ' Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers") s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _ "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _ "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _ "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _ "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _ "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _ "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ - 123, - 123.45F) Console.WriteLine(s) ' Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers") s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _ "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _ "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _ "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _ "(f) Full date/short time: . . {0:f}" & vbCrLf & _ "(F) Full date/long time:. . . {0:F}" & vbCrLf & _ "(g) General date/short time:. {0:g}" & vbCrLf & _ "(G) General date/long time: . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _ "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _ "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _ "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _ "(U) Universal sortable: . . . {0:U}" & vbCrLf & _ "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _ thisDate) Console.WriteLine(s) ' Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers") s = String.Format("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _ "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ Color.Green) Console.WriteLine(s) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' 'Standard Numeric Format Specifiers '(C) Currency: . . . . . . . . ($123.00) '(D) Decimal:. . . . . . . . . -123 '(E) Scientific: . . . . . . . -1.234500E+002 '(F) Fixed point:. . . . . . . -123.45 '(G) General:. . . . . . . . . -123 ' (default):. . . . . . . . -123 (default = 'G') '(N) Number: . . . . . . . . . -123.00 '(P) Percent:. . . . . . . . . -12,345.00 % '(R) Round-trip: . . . . . . . -123.45 '(X) Hexadecimal:. . . . . . . FFFFFF85 ' 'Standard DateTime Format Specifiers '(d) Short date: . . . . . . . 6/26/2004 '(D) Long date:. . . . . . . . Saturday, June 26, 2004 '(t) Short time: . . . . . . . 8:11 PM '(T) Long time:. . . . . . . . 8:11:04 PM '(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM '(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM '(g) General date/short time:. 6/26/2004 8:11 PM '(G) General date/long time: . 6/26/2004 8:11:04 PM ' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') '(M) Month:. . . . . . . . . . June 26 '(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT '(s) Sortable: . . . . . . . . 2004-06-26T20:11:04 '(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) '(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM '(Y) Year: . . . . . . . . . . June, 2004 ' 'Standard Enumeration Format Specifiers '(G) General:. . . . . . . . . Green ' (default):. . . . . . . . Green (default = 'G') '(F) Flags:. . . . . . . . . . Green (flags or integer) '(D) Decimal number: . . . . . 3 '(X) Hexadecimal:. . . . . . . 00000003 '
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using System; class Sample { enum Color {Yellow = 1, Blue, Green}; static DateTime thisDate = DateTime.Now; public static void Main() { // Store the output of the String.Format method in a string. string s = ""; Console.Clear(); // Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers"); s = String.Format( "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console.WriteLine(s); // Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers"); s = String.Format( "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console.WriteLine(s); // Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers"); s = String.Format( "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color.Green); Console.WriteLine(s); } } /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using namespace System; using namespace System::Globalization; enum class Color {Yellow = 1, Blue, Green}; int main(void) { DateTime^ thisDate = DateTime::Now; // Store the output of the String::Format method in a string. String^ resultString = ""; Console::Clear(); // Format a negative integer or floating-point number in // various ways. Console::WriteLine("Standard Numeric Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console::WriteLine(resultString); // Format the current date in various ways. Console::WriteLine("Standard DateTime Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console::WriteLine(resultString); // Format a Color enumeration value in various ways. Console::WriteLine("Standard Enumeration Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color::Green); Console::WriteLine(resultString); }; /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Format メソッド (String, Object[])
アセンブリ: mscorlib (mscorlib.dll 内)

Dim format As String Dim args As Object() Dim returnValue As String returnValue = String.Format(format, args)
戻り値
書式項目が args の Object の対応インスタンスと等価の String に置換された format のコピー。


このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。
-
Format、AppendFormat、および、WriteLine の一部のオーバーロードでサポートされる複合書式指定の詳細については、「複合書式設定」を参照してください。
-
日時書式指定子の詳細については、「標準の DateTime 書式指定文字列」および「カスタム DateTime 書式指定文字列」を参照してください。
format パラメータは、ゼロ個以上のテキストに、このメソッドのパラメータ リストに指定されたオブジェクトと対応する、ゼロ個以上のインデックス付きプレースホルダ (書式項目と呼ばれる) を組み合わせて指定します。各書式項目は、書式設定プロセスで、対応するオブジェクト値のテキスト表現に置き換えられます。
書式項目は {index[,alignment][:formatString]} という構文で、それぞれ、インデックス (省略不可)、書式設定するテキストの長さと配置 (省略可)、および、対応するオブジェクトの値をどのように書式設定するかを制御する、書式指定子の文字列 (省略可) を指定します。書式項目の各要素を次に示します。
indexオブジェクトのリスト内のどの要素を書式化するかを示す 0 から始まる整数。index で指定されたオブジェクトが null 参照 (Visual Basic では Nothing) の場合、書式項目は空の文字列 ("") で置き換えられます。
alignment書式設定された値を格納する領域の最小幅を示す、省略可能な整数。書式設定された値の長さが alignment よりも小さい場合、その領域の余白は空白文字で埋められます。alignment が負の場合、書式設定された値は領域内で左詰めになります。alignment が正の場合、値は右詰めになります。alignment の指定を省略すると、領域の長さは書式設定された長さと同じになります。alignment を指定した場合、コンマは省略できません。
formatString書式指定子の文字列 (省略可能)。formatString が指定されておらず、対応する引数が IFormattable インターフェイスを実装している場合、null 参照 (Visual Basic では Nothing) が IFormattable.ToString 書式文字列として使用されます。そのため、IFormattable.ToString のすべての実装では、書式指定文字列として null 参照 (Visual Basic では Nothing) を許容し、オブジェクト表現の既定の書式を String として返す必要があります。formatString を指定した場合、コロンは省略できません。
先頭および末尾の中かっこ文字 '{' および '}' は必須です。format 内でリテラルな中かっこ文字を指定するには、"{{" または "}}" のように、先頭または末尾の中かっこ文字を 2 つ続けて指定します。
format の値が "『Microsoft® .NET (Core Reference)』を {0:####} 冊お買い上げいただき、ありがとうございました。" で、arg[0] が 123 という値を持つ Int16 だとすると、戻り値は次のようになります。
"『Microsoft® .NET (Core Reference)』を 123 冊お買い上げいただき、ありがとうございました。"
format の値が "ブラッドの飼っている犬には {0,-8:G} 匹のノミがいる。" で、arg[0] が 42 という値を持つ Int16 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれるスペースを表します。

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。
' This code example demonstrates the String.Format() method. ' Formatting for this example uses the "en-US" culture. Imports System Imports Microsoft.VisualBasic Class Sample Public Enum Color Yellow = 1 Blue = 2 Green = 3 End Enum 'Color Private Shared thisDate As DateTime = DateTime.Now Public Shared Sub Main() ' Store the output of the String.Format method in a string. Dim s As String = "" Console.Clear() ' Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers") s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _ "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _ "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _ "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _ "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _ "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _ "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ - 123, - 123.45F) Console.WriteLine(s) ' Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers") s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _ "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _ "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _ "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _ "(f) Full date/short time: . . {0:f}" & vbCrLf & _ "(F) Full date/long time:. . . {0:F}" & vbCrLf & _ "(g) General date/short time:. {0:g}" & vbCrLf & _ "(G) General date/long time: . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _ "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _ "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _ "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _ "(U) Universal sortable: . . . {0:U}" & vbCrLf & _ "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _ thisDate) Console.WriteLine(s) ' Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers") s = String.Format("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _ "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ Color.Green) Console.WriteLine(s) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' 'Standard Numeric Format Specifiers '(C) Currency: . . . . . . . . ($123.00) '(D) Decimal:. . . . . . . . . -123 '(E) Scientific: . . . . . . . -1.234500E+002 '(F) Fixed point:. . . . . . . -123.45 '(G) General:. . . . . . . . . -123 ' (default):. . . . . . . . -123 (default = 'G') '(N) Number: . . . . . . . . . -123.00 '(P) Percent:. . . . . . . . . -12,345.00 % '(R) Round-trip: . . . . . . . -123.45 '(X) Hexadecimal:. . . . . . . FFFFFF85 ' 'Standard DateTime Format Specifiers '(d) Short date: . . . . . . . 6/26/2004 '(D) Long date:. . . . . . . . Saturday, June 26, 2004 '(t) Short time: . . . . . . . 8:11 PM '(T) Long time:. . . . . . . . 8:11:04 PM '(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM '(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM '(g) General date/short time:. 6/26/2004 8:11 PM '(G) General date/long time: . 6/26/2004 8:11:04 PM ' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') '(M) Month:. . . . . . . . . . June 26 '(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT '(s) Sortable: . . . . . . . . 2004-06-26T20:11:04 '(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) '(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM '(Y) Year: . . . . . . . . . . June, 2004 ' 'Standard Enumeration Format Specifiers '(G) General:. . . . . . . . . Green ' (default):. . . . . . . . Green (default = 'G') '(F) Flags:. . . . . . . . . . Green (flags or integer) '(D) Decimal number: . . . . . 3 '(X) Hexadecimal:. . . . . . . 00000003 '
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using System; class Sample { enum Color {Yellow = 1, Blue, Green}; static DateTime thisDate = DateTime.Now; public static void Main() { // Store the output of the String.Format method in a string. string s = ""; Console.Clear(); // Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers"); s = String.Format( "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console.WriteLine(s); // Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers"); s = String.Format( "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console.WriteLine(s); // Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers"); s = String.Format( "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color.Green); Console.WriteLine(s); } } /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using namespace System; using namespace System::Globalization; enum class Color {Yellow = 1, Blue, Green}; int main(void) { DateTime^ thisDate = DateTime::Now; // Store the output of the String::Format method in a string. String^ resultString = ""; Console::Clear(); // Format a negative integer or floating-point number in // various ways. Console::WriteLine("Standard Numeric Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console::WriteLine(resultString); // Format the current date in various ways. Console::WriteLine("Standard DateTime Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console::WriteLine(resultString); // Format a Color enumeration value in various ways. Console::WriteLine("Standard Enumeration Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color::Green); Console::WriteLine(resultString); }; /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Format メソッド (String, Object, Object, Object)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Format ( _ format As String, _ arg0 As Object, _ arg1 As Object, _ arg2 As Object _ ) As String
Dim format As String Dim arg0 As Object Dim arg1 As Object Dim arg2 As Object Dim returnValue As String returnValue = String.Format(format, arg0, arg1, arg2)
public static function Format ( format : String, arg0 : Object, arg1 : Object, arg2 : Object ) : String
戻り値
第 1、第 2、第 3 の各書式項目が、それぞれ arg0、arg1、および arg2 と等価の String に置換された format のコピー。


このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。
-
Format、AppendFormat、および、WriteLine の一部のオーバーロードでサポートされる複合書式指定の詳細については、「複合書式設定」を参照してください。
-
日時書式指定子の詳細については、「標準の DateTime 書式指定文字列」および「カスタム DateTime 書式指定文字列」を参照してください。
format パラメータは、ゼロ個以上のテキストに、このメソッドのパラメータ リストに指定されたオブジェクトと対応する、ゼロ個以上のインデックス付きプレースホルダ (書式項目と呼ばれる) を組み合わせて指定します。各書式項目は、書式設定プロセスで、対応するオブジェクト値のテキスト表現に置き換えられます。
書式項目は {index[,alignment][:formatString]} という構文で、それぞれ、インデックス (省略不可)、書式設定するテキストの長さと配置 (省略可)、および、対応するオブジェクトの値をどのように書式設定するかを制御する、書式指定子の文字列 (省略可) を指定します。書式項目の各要素を次に示します。
indexオブジェクトのリスト内のどの要素を書式化するかを示す 0 から始まる整数。index で指定されたオブジェクトが null 参照 (Visual Basic では Nothing) の場合、書式項目は空の文字列 ("") で置き換えられます。
alignment書式設定された値を格納する領域の最小幅を示す、省略可能な整数。書式設定された値の長さが alignment よりも小さい場合、その領域の余白は空白文字で埋められます。alignment が負の場合、書式設定された値は領域内で左詰めになります。alignment が正の場合、値は右詰めになります。alignment の指定を省略すると、領域の長さは書式設定された長さと同じになります。alignment を指定した場合、コンマは省略できません。
formatString書式指定子の文字列 (省略可能)。formatString が指定されておらず、対応する引数が IFormattable インターフェイスを実装している場合、null 参照 (Visual Basic では Nothing) が IFormattable.ToString 書式文字列として使用されます。そのため、IFormattable.ToString のすべての実装では、書式指定文字列として null 参照 (Visual Basic では Nothing) を許容し、オブジェクト表現の既定の書式を String として返す必要があります。formatString を指定した場合、コロンは省略できません。
先頭および末尾の中かっこ文字 '{' および '}' は必須です。format 内でリテラルな中かっこ文字を指定するには、"{{" または "}}" のように、先頭または末尾の中かっこ文字を 2 つ続けて指定します。
format の値が "『Microsoft® .NET (Core Reference)』を {0:####} 冊お買い上げいただき、ありがとうございました。" で、arg0 が 123 という値を持つ Int16 だとすると、戻り値は次のようになります。
"『Microsoft® .NET (Core Reference)』を 123 冊お買い上げいただき、ありがとうございました。"
format の値が "ブラッドの飼っている犬には {0,-8:G} 匹のノミがいる。" で、arg0 が 42 という値を持つ Int16 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれたスペースを表します。

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。
' This code example demonstrates the String.Format() method. ' Formatting for this example uses the "en-US" culture. Imports System Imports Microsoft.VisualBasic Class Sample Public Enum Color Yellow = 1 Blue = 2 Green = 3 End Enum 'Color Private Shared thisDate As DateTime = DateTime.Now Public Shared Sub Main() ' Store the output of the String.Format method in a string. Dim s As String = "" Console.Clear() ' Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers") s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _ "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _ "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _ "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _ "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _ "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _ "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ - 123, - 123.45F) Console.WriteLine(s) ' Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers") s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _ "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _ "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _ "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _ "(f) Full date/short time: . . {0:f}" & vbCrLf & _ "(F) Full date/long time:. . . {0:F}" & vbCrLf & _ "(g) General date/short time:. {0:g}" & vbCrLf & _ "(G) General date/long time: . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _ "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _ "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _ "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _ "(U) Universal sortable: . . . {0:U}" & vbCrLf & _ "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _ thisDate) Console.WriteLine(s) ' Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers") s = String.Format("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _ "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ Color.Green) Console.WriteLine(s) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' 'Standard Numeric Format Specifiers '(C) Currency: . . . . . . . . ($123.00) '(D) Decimal:. . . . . . . . . -123 '(E) Scientific: . . . . . . . -1.234500E+002 '(F) Fixed point:. . . . . . . -123.45 '(G) General:. . . . . . . . . -123 ' (default):. . . . . . . . -123 (default = 'G') '(N) Number: . . . . . . . . . -123.00 '(P) Percent:. . . . . . . . . -12,345.00 % '(R) Round-trip: . . . . . . . -123.45 '(X) Hexadecimal:. . . . . . . FFFFFF85 ' 'Standard DateTime Format Specifiers '(d) Short date: . . . . . . . 6/26/2004 '(D) Long date:. . . . . . . . Saturday, June 26, 2004 '(t) Short time: . . . . . . . 8:11 PM '(T) Long time:. . . . . . . . 8:11:04 PM '(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM '(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM '(g) General date/short time:. 6/26/2004 8:11 PM '(G) General date/long time: . 6/26/2004 8:11:04 PM ' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') '(M) Month:. . . . . . . . . . June 26 '(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT '(s) Sortable: . . . . . . . . 2004-06-26T20:11:04 '(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) '(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM '(Y) Year: . . . . . . . . . . June, 2004 ' 'Standard Enumeration Format Specifiers '(G) General:. . . . . . . . . Green ' (default):. . . . . . . . Green (default = 'G') '(F) Flags:. . . . . . . . . . Green (flags or integer) '(D) Decimal number: . . . . . 3 '(X) Hexadecimal:. . . . . . . 00000003 '
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using System; class Sample { enum Color {Yellow = 1, Blue, Green}; static DateTime thisDate = DateTime.Now; public static void Main() { // Store the output of the String.Format method in a string. string s = ""; Console.Clear(); // Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers"); s = String.Format( "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console.WriteLine(s); // Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers"); s = String.Format( "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console.WriteLine(s); // Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers"); s = String.Format( "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color.Green); Console.WriteLine(s); } } /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using namespace System; using namespace System::Globalization; enum class Color {Yellow = 1, Blue, Green}; int main(void) { DateTime^ thisDate = DateTime::Now; // Store the output of the String::Format method in a string. String^ resultString = ""; Console::Clear(); // Format a negative integer or floating-point number in // various ways. Console::WriteLine("Standard Numeric Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console::WriteLine(resultString); // Format the current date in various ways. Console::WriteLine("Standard DateTime Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console::WriteLine(resultString); // Format a Color enumeration value in various ways. Console::WriteLine("Standard Enumeration Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color::Green); Console::WriteLine(resultString); }; /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Format メソッド (String, Object)
アセンブリ: mscorlib (mscorlib.dll 内)

Dim format As String Dim arg0 As Object Dim returnValue As String returnValue = String.Format(format, arg0)
戻り値
1 番目の書式項目が、arg0 と等価の String により置換された format のコピー。


このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。
-
Format、AppendFormat、および、WriteLine の一部のオーバーロードでサポートされる複合書式指定の詳細については、「複合書式設定」を参照してください。
-
日時書式指定子の詳細については、「標準の DateTime 書式指定文字列」および「カスタム DateTime 書式指定文字列」を参照してください。
format パラメータは、ゼロ個以上のテキストに、このメソッドのパラメータ リストに指定されたオブジェクトと対応する、ゼロ個以上のインデックス付きプレースホルダ (書式項目と呼ばれる) を組み合わせて指定します。各書式項目は、書式設定プロセスで、対応するオブジェクト値のテキスト表現に置き換えられます。
書式項目は {index[,alignment][:formatString]} という構文で、それぞれ、インデックス (省略不可)、書式設定するテキストの長さと配置 (省略可)、および、対応するオブジェクトの値をどのように書式設定するかを制御する、書式指定子の文字列 (省略可) を指定します。書式項目の各要素を次に示します。
indexオブジェクトのリスト内のどの要素を書式化するかを示す 0 から始まる整数。index で指定されたオブジェクトが null 参照 (Visual Basic では Nothing) の場合、書式項目は空の文字列 ("") で置き換えられます。
alignment書式設定された値を格納する領域の最小幅を示す、省略可能な整数。書式設定された値の長さが alignment よりも小さい場合、その領域の余白は空白文字で埋められます。alignment が負の場合、書式設定された値は領域内で左詰めになります。alignment が正の場合、値は右詰めになります。alignment の指定を省略すると、領域の長さは書式設定された長さと同じになります。alignment を指定した場合、コンマは省略できません。
formatString書式指定子の文字列 (省略可能)。formatString が指定されておらず、対応する引数が IFormattable インターフェイスを実装している場合、null 参照 (Visual Basic では Nothing) が IFormattable.ToString 書式文字列として使用されます。そのため、IFormattable.ToString のすべての実装では、書式指定文字列として null 参照 (Visual Basic では Nothing) を許容し、オブジェクト表現の既定の書式を String として返す必要があります。formatString を指定した場合、コロンは省略できません。
先頭および末尾の中かっこ文字 '{' および '}' は必須です。format 内でリテラルな中かっこ文字を指定するには、"{{" または "}}" のように、先頭または末尾の中かっこ文字を 2 つ続けて指定します。
format の値が "『Microsoft® .NET (Core Reference)』を {0:####} 冊お買い上げいただき、ありがとうございました。" で、arg0 が 123 という値を持つ Int16 だとすると、戻り値は次のようになります。
"『Microsoft® .NET (Core Reference)』を 123 冊お買い上げいただき、ありがとうございました。"
format の値が "ブラッドの飼っている犬には {0,-8:G} 匹のノミがいる。" で、arg0 が 42 という値を持つ Int16 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれたスペースを表します。

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。
' This code example demonstrates the String.Format() method. ' Formatting for this example uses the "en-US" culture. Imports System Imports Microsoft.VisualBasic Class Sample Public Enum Color Yellow = 1 Blue = 2 Green = 3 End Enum 'Color Private Shared thisDate As DateTime = DateTime.Now Public Shared Sub Main() ' Store the output of the String.Format method in a string. Dim s As String = "" Console.Clear() ' Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers") s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _ "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _ "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _ "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _ "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _ "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _ "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ - 123, - 123.45F) Console.WriteLine(s) ' Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers") s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _ "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _ "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _ "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _ "(f) Full date/short time: . . {0:f}" & vbCrLf & _ "(F) Full date/long time:. . . {0:F}" & vbCrLf & _ "(g) General date/short time:. {0:g}" & vbCrLf & _ "(G) General date/long time: . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _ "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _ "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _ "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _ "(U) Universal sortable: . . . {0:U}" & vbCrLf & _ "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _ thisDate) Console.WriteLine(s) ' Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers") s = String.Format("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _ "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ Color.Green) Console.WriteLine(s) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' 'Standard Numeric Format Specifiers '(C) Currency: . . . . . . . . ($123.00) '(D) Decimal:. . . . . . . . . -123 '(E) Scientific: . . . . . . . -1.234500E+002 '(F) Fixed point:. . . . . . . -123.45 '(G) General:. . . . . . . . . -123 ' (default):. . . . . . . . -123 (default = 'G') '(N) Number: . . . . . . . . . -123.00 '(P) Percent:. . . . . . . . . -12,345.00 % '(R) Round-trip: . . . . . . . -123.45 '(X) Hexadecimal:. . . . . . . FFFFFF85 ' 'Standard DateTime Format Specifiers '(d) Short date: . . . . . . . 6/26/2004 '(D) Long date:. . . . . . . . Saturday, June 26, 2004 '(t) Short time: . . . . . . . 8:11 PM '(T) Long time:. . . . . . . . 8:11:04 PM '(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM '(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM '(g) General date/short time:. 6/26/2004 8:11 PM '(G) General date/long time: . 6/26/2004 8:11:04 PM ' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') '(M) Month:. . . . . . . . . . June 26 '(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT '(s) Sortable: . . . . . . . . 2004-06-26T20:11:04 '(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) '(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM '(Y) Year: . . . . . . . . . . June, 2004 ' 'Standard Enumeration Format Specifiers '(G) General:. . . . . . . . . Green ' (default):. . . . . . . . Green (default = 'G') '(F) Flags:. . . . . . . . . . Green (flags or integer) '(D) Decimal number: . . . . . 3 '(X) Hexadecimal:. . . . . . . 00000003 '
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using System; class Sample { enum Color {Yellow = 1, Blue, Green}; static DateTime thisDate = DateTime.Now; public static void Main() { // Store the output of the String.Format method in a string. string s = ""; Console.Clear(); // Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers"); s = String.Format( "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console.WriteLine(s); // Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers"); s = String.Format( "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console.WriteLine(s); // Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers"); s = String.Format( "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color.Green); Console.WriteLine(s); } } /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using namespace System; using namespace System::Globalization; enum class Color {Yellow = 1, Blue, Green}; int main(void) { DateTime^ thisDate = DateTime::Now; // Store the output of the String::Format method in a string. String^ resultString = ""; Console::Clear(); // Format a negative integer or floating-point number in // various ways. Console::WriteLine("Standard Numeric Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console::WriteLine(resultString); // Format the current date in various ways. Console::WriteLine("Standard DateTime Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console::WriteLine(resultString); // Format a Color enumeration value in various ways. Console::WriteLine("Standard Enumeration Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color::Green); Console::WriteLine(resultString); }; /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


String.Format メソッド

名前 | 説明 |
---|---|
String.Format (String, Object) | 指定した String の書式項目を、指定した Object インスタンスの値と等価のテキストに置換します。 .NET Compact Framework によってサポートされています。 |
String.Format (String, Object[]) | 指定した String の書式項目を、指定した配列内の対応する Object インスタンスの値と等価のテキストに置換します。 .NET Compact Framework によってサポートされています。 |
String.Format (IFormatProvider, String, Object[]) | 指定した String の書式項目を、指定した配列内の対応する Object インスタンスの値と等価のテキストに置換します。指定したパラメータにより、カルチャ固有の書式設定情報が提供されます。 .NET Compact Framework によってサポートされています。 |
String.Format (String, Object, Object) | 指定した String の書式項目を、指定した 2 つの Object インスタンスの値と等価のテキストに置換します。 .NET Compact Framework によってサポートされています。 |
String.Format (String, Object, Object, Object) | 指定した String の書式項目を、指定した 3 つの Object インスタンスの値と等価のテキストに置換します。 .NET Compact Framework によってサポートされています。 |

String.Format メソッド (String, Object, Object)
アセンブリ: mscorlib (mscorlib.dll 内)

Public Shared Function Format ( _ format As String, _ arg0 As Object, _ arg1 As Object _ ) As String
Dim format As String Dim arg0 As Object Dim arg1 As Object Dim returnValue As String returnValue = String.Format(format, arg0, arg1)
戻り値
1 番目と 2 番目の書式項目が、arg0 および arg1 と等価の String に置換された format のコピー。


このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。
-
Format、AppendFormat、および、WriteLine の一部のオーバーロードでサポートされる複合書式指定の詳細については、「複合書式設定」を参照してください。
-
日時書式指定子の詳細については、「標準の DateTime 書式指定文字列」および「カスタム DateTime 書式指定文字列」を参照してください。
format パラメータは、ゼロ個以上のテキストに、このメソッドのパラメータ リストに指定されたオブジェクトと対応する、ゼロ個以上のインデックス付きプレースホルダ (書式項目と呼ばれる) を組み合わせて指定します。各書式項目は、書式設定プロセスで、対応するオブジェクト値のテキスト表現に置き換えられます。
書式項目は {index[,alignment][:formatString]} という構文で、それぞれ、インデックス (省略不可)、書式設定するテキストの長さと配置 (省略可)、および、対応するオブジェクトの値をどのように書式設定するかを制御する、書式指定子の文字列 (省略可) を指定します。書式項目の各要素を次に示します。
indexオブジェクトのリスト内のどの要素を書式化するかを示す 0 から始まる整数。index で指定されたオブジェクトが null 参照 (Visual Basic では Nothing) の場合、書式項目は空の文字列 ("") で置き換えられます。
alignment書式設定された値を格納する領域の最小幅を示す、省略可能な整数。書式設定された値の長さが alignment よりも小さい場合、その領域の余白は空白文字で埋められます。alignment が負の場合、書式設定された値は領域内で左詰めになります。alignment が正の場合、値は右詰めになります。alignment の指定を省略すると、領域の長さは書式設定された長さと同じになります。alignment を指定した場合、コンマは省略できません。
formatString書式指定子の文字列 (省略可能)。formatString が指定されておらず、対応する引数が IFormattable インターフェイスを実装している場合、null 参照 (Visual Basic では Nothing) が IFormattable.ToString 書式文字列として使用されます。そのため、IFormattable.ToString のすべての実装では、書式指定文字列として null 参照 (Visual Basic では Nothing) を許容し、オブジェクト表現の既定の書式を String として返す必要があります。formatString を指定した場合、コロンは省略できません。
先頭および末尾の中かっこ文字 '{' および '}' は必須です。format 内でリテラルな中かっこ文字を指定するには、"{{" または "}}" のように、先頭または末尾の中かっこ文字を 2 つ続けて指定します。
format の値が "『Microsoft® .NET (Core Reference)』を {0:####} 冊お買い上げいただき、ありがとうございました。" で、arg0 が 123 という値を持つ Int16 だとすると、戻り値は次のようになります。
"『Microsoft® .NET (Core Reference)』を 123 冊お買い上げいただき、ありがとうございました。"
format の値が "ブラッドの飼っている犬には {0,-8:G} 匹のノミがいる。" で、arg0 が 42 という値を持つ Int16 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれたスペースを表します。

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。
' This code example demonstrates the String.Format() method. ' Formatting for this example uses the "en-US" culture. Imports System Imports Microsoft.VisualBasic Class Sample Public Enum Color Yellow = 1 Blue = 2 Green = 3 End Enum 'Color Private Shared thisDate As DateTime = DateTime.Now Public Shared Sub Main() ' Store the output of the String.Format method in a string. Dim s As String = "" Console.Clear() ' Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers") s = String.Format("(C) Currency: . . . . . . . . {0:C}" & vbCrLf & _ "(D) Decimal:. . . . . . . . . {0:D}" & vbCrLf & _ "(E) Scientific: . . . . . . . {1:E}" & vbCrLf & _ "(F) Fixed point:. . . . . . . {1:F}" & vbCrLf & _ "(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(N) Number: . . . . . . . . . {0:N}" & vbCrLf & _ "(P) Percent:. . . . . . . . . {1:P}" & vbCrLf & _ "(R) Round-trip: . . . . . . . {1:R}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ - 123, - 123.45F) Console.WriteLine(s) ' Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers") s = String.Format("(d) Short date: . . . . . . . {0:d}" & vbCrLf & _ "(D) Long date:. . . . . . . . {0:D}" & vbCrLf & _ "(t) Short time: . . . . . . . {0:t}" & vbCrLf & _ "(T) Long time:. . . . . . . . {0:T}" & vbCrLf & _ "(f) Full date/short time: . . {0:f}" & vbCrLf & _ "(F) Full date/long time:. . . {0:F}" & vbCrLf & _ "(g) General date/short time:. {0:g}" & vbCrLf & _ "(G) General date/long time: . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(M) Month:. . . . . . . . . . {0:M}" & vbCrLf & _ "(R) RFC1123:. . . . . . . . . {0:R}" & vbCrLf & _ "(s) Sortable: . . . . . . . . {0:s}" & vbCrLf & _ "(u) Universal sortable: . . . {0:u} (invariant)" & vbCrLf & _ "(U) Universal sortable: . . . {0:U}" & vbCrLf & _ "(Y) Year: . . . . . . . . . . {0:Y}" & vbCrLf, _ thisDate) Console.WriteLine(s) ' Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers") s = String.Format("(G) General:. . . . . . . . . {0:G}" & vbCrLf & _ " (default):. . . . . . . . {0} (default = 'G')" & vbCrLf & _ "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)" & vbCrLf & _ "(D) Decimal number: . . . . . {0:D}" & vbCrLf & _ "(X) Hexadecimal:. . . . . . . {0:X}" & vbCrLf, _ Color.Green) Console.WriteLine(s) End Sub 'Main End Class 'Sample ' 'This code example produces the following results: ' 'Standard Numeric Format Specifiers '(C) Currency: . . . . . . . . ($123.00) '(D) Decimal:. . . . . . . . . -123 '(E) Scientific: . . . . . . . -1.234500E+002 '(F) Fixed point:. . . . . . . -123.45 '(G) General:. . . . . . . . . -123 ' (default):. . . . . . . . -123 (default = 'G') '(N) Number: . . . . . . . . . -123.00 '(P) Percent:. . . . . . . . . -12,345.00 % '(R) Round-trip: . . . . . . . -123.45 '(X) Hexadecimal:. . . . . . . FFFFFF85 ' 'Standard DateTime Format Specifiers '(d) Short date: . . . . . . . 6/26/2004 '(D) Long date:. . . . . . . . Saturday, June 26, 2004 '(t) Short time: . . . . . . . 8:11 PM '(T) Long time:. . . . . . . . 8:11:04 PM '(f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM '(F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM '(g) General date/short time:. 6/26/2004 8:11 PM '(G) General date/long time: . 6/26/2004 8:11:04 PM ' (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') '(M) Month:. . . . . . . . . . June 26 '(R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT '(s) Sortable: . . . . . . . . 2004-06-26T20:11:04 '(u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) '(U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM '(Y) Year: . . . . . . . . . . June, 2004 ' 'Standard Enumeration Format Specifiers '(G) General:. . . . . . . . . Green ' (default):. . . . . . . . Green (default = 'G') '(F) Flags:. . . . . . . . . . Green (flags or integer) '(D) Decimal number: . . . . . 3 '(X) Hexadecimal:. . . . . . . 00000003 '
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using System; class Sample { enum Color {Yellow = 1, Blue, Green}; static DateTime thisDate = DateTime.Now; public static void Main() { // Store the output of the String.Format method in a string. string s = ""; Console.Clear(); // Format a negative integer or floating-point number in various ways. Console.WriteLine("Standard Numeric Format Specifiers"); s = String.Format( "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console.WriteLine(s); // Format the current date in various ways. Console.WriteLine("Standard DateTime Format Specifiers"); s = String.Format( "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console.WriteLine(s); // Format a Color enumeration value in various ways. Console.WriteLine("Standard Enumeration Format Specifiers"); s = String.Format( "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color.Green); Console.WriteLine(s); } } /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */
// This code example demonstrates the String.Format() method. // Formatting for this example uses the "en-US" culture. using namespace System; using namespace System::Globalization; enum class Color {Yellow = 1, Blue, Green}; int main(void) { DateTime^ thisDate = DateTime::Now; // Store the output of the String::Format method in a string. String^ resultString = ""; Console::Clear(); // Format a negative integer or floating-point number in // various ways. Console::WriteLine("Standard Numeric Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(C) Currency: . . . . . . . . {0:C}\n" + "(D) Decimal:. . . . . . . . . {0:D}\n" + "(E) Scientific: . . . . . . . {1:E}\n" + "(F) Fixed point:. . . . . . . {1:F}\n" + "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(N) Number: . . . . . . . . . {0:N}\n" + "(P) Percent:. . . . . . . . . {1:P}\n" + "(R) Round-trip: . . . . . . . {1:R}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", -123, -123.45f); Console::WriteLine(resultString); // Format the current date in various ways. Console::WriteLine("Standard DateTime Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(d) Short date: . . . . . . . {0:d}\n" + "(D) Long date:. . . . . . . . {0:D}\n" + "(t) Short time: . . . . . . . {0:t}\n" + "(T) Long time:. . . . . . . . {0:T}\n" + "(f) Full date/short time: . . {0:f}\n" + "(F) Full date/long time:. . . {0:F}\n" + "(g) General date/short time:. {0:g}\n" + "(G) General date/long time: . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(M) Month:. . . . . . . . . . {0:M}\n" + "(R) RFC1123:. . . . . . . . . {0:R}\n" + "(s) Sortable: . . . . . . . . {0:s}\n" + "(u) Universal sortable: . . . {0:u} (invariant)\n" + "(U) Universal sortable: . . . {0:U}\n" + "(Y) Year: . . . . . . . . . . {0:Y}\n", thisDate); Console::WriteLine(resultString); // Format a Color enumeration value in various ways. Console::WriteLine("Standard Enumeration Format Specifiers"); resultString = String::Format(CultureInfo::CurrentCulture, "(G) General:. . . . . . . . . {0:G}\n" + " (default):. . . . . . . . {0} (default = 'G')\n" + "(F) Flags:. . . . . . . . . . {0:F} (flags or integer)\n" + "(D) Decimal number: . . . . . {0:D}\n" + "(X) Hexadecimal:. . . . . . . {0:X}\n", Color::Green); Console::WriteLine(resultString); }; /* This code example produces the following results: Standard Numeric Format Specifiers (C) Currency: . . . . . . . . ($123.00) (D) Decimal:. . . . . . . . . -123 (E) Scientific: . . . . . . . -1.234500E+002 (F) Fixed point:. . . . . . . -123.45 (G) General:. . . . . . . . . -123 (default):. . . . . . . . -123 (default = 'G') (N) Number: . . . . . . . . . -123.00 (P) Percent:. . . . . . . . . -12,345.00 % (R) Round-trip: . . . . . . . -123.45 (X) Hexadecimal:. . . . . . . FFFFFF85 Standard DateTime Format Specifiers (d) Short date: . . . . . . . 6/26/2004 (D) Long date:. . . . . . . . Saturday, June 26, 2004 (t) Short time: . . . . . . . 8:11 PM (T) Long time:. . . . . . . . 8:11:04 PM (f) Full date/short time: . . Saturday, June 26, 2004 8:11 PM (F) Full date/long time:. . . Saturday, June 26, 2004 8:11:04 PM (g) General date/short time:. 6/26/2004 8:11 PM (G) General date/long time: . 6/26/2004 8:11:04 PM (default):. . . . . . . . 6/26/2004 8:11:04 PM (default = 'G') (M) Month:. . . . . . . . . . June 26 (R) RFC1123:. . . . . . . . . Sat, 26 Jun 2004 20:11:04 GMT (s) Sortable: . . . . . . . . 2004-06-26T20:11:04 (u) Universal sortable: . . . 2004-06-26 20:11:04Z (invariant) (U) Universal sortable: . . . Sunday, June 27, 2004 3:11:04 AM (Y) Year: . . . . . . . . . . June, 2004 Standard Enumeration Format Specifiers (G) General:. . . . . . . . . Green (default):. . . . . . . . Green (default = 'G') (F) Flags:. . . . . . . . . . Green (flags or integer) (D) Decimal number: . . . . . 3 (X) Hexadecimal:. . . . . . . 00000003 */

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringFormat クラス
アセンブリ: System.Drawing (system.drawing.dll 内)

Public NotInheritable Class StringFormat Inherits MarshalByRefObject Implements ICloneable, IDisposable
public sealed class StringFormat : MarshalByRefObject, ICloneable, IDisposable
public final class StringFormat extends MarshalByRefObject implements ICloneable, IDisposable
public final class StringFormat extends MarshalByRefObject implements ICloneable, IDisposable


System.MarshalByRefObject
System.Drawing.StringFormat


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringFormat コンストラクタ ()
アセンブリ: System.Drawing (system.drawing.dll 内)


Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringFormat コンストラクタ (StringFormatFlags)
アセンブリ: System.Drawing (system.drawing.dll 内)


この例は、Windows フォームでの使用を意図してデザインされています。コードをフォームに貼り付け、フォームの Paint イベントを処理するときに PaintEventArgs の e を渡して ShowLineAndAlignment メソッドを呼び出します。
Private Sub ShowLineAndAlignment(ByVal e As PaintEventArgs) ' Construct a new Rectangle. Dim displayRectangle _ As New Rectangle(New Point(40, 40), New Size(80, 80)) ' Construct two new StringFormat objects Dim format1 As New StringFormat(StringFormatFlags.NoClip) Dim format2 As New StringFormat(format1) ' Set the LineAlignment and Alignment properties for ' both StringFormat objects to different values. format1.LineAlignment = StringAlignment.Near format1.Alignment = StringAlignment.Center format2.LineAlignment = StringAlignment.Center format2.Alignment = StringAlignment.Far ' Draw the bounding rectangle and a string for each ' StringFormat object. e.Graphics.DrawRectangle(Pens.Black, displayRectangle) e.Graphics.DrawString("Showing Format1", Me.Font, Brushes.Red, _ RectangleF.op_Implicit(displayRectangle), format1) e.Graphics.DrawString("Showing Format2", Me.Font, Brushes.Red, _ RectangleF.op_Implicit(displayRectangle), format2) End Sub
private void ShowLineAndAlignment(PaintEventArgs e) { // Construct a new Rectangle . Rectangle displayRectangle = new Rectangle (new Point(40, 40), new Size (80, 80)); // Construct 2 new StringFormat objects StringFormat format1 = new StringFormat(StringFormatFlags.NoClip); StringFormat format2 = new StringFormat(format1); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1.LineAlignment = StringAlignment.Near; format1.Alignment = StringAlignment.Center; format2.LineAlignment = StringAlignment.Center; format2.Alignment = StringAlignment.Far; // Draw the bounding rectangle and a string for each // StringFormat object. e.Graphics.DrawRectangle(Pens.Black, displayRectangle); e.Graphics.DrawString("Showing Format1", this.Font, Brushes.Red, (RectangleF)displayRectangle, format1); e.Graphics.DrawString("Showing Format2", this.Font, Brushes.Red, (RectangleF)displayRectangle, format2); }
private: void ShowLineAndAlignment( PaintEventArgs^ e ) { // Construct a new Rectangle . Rectangle displayRectangle = Rectangle(Point(40,40),System::Drawing::Size( 80, 80 )); // Construct 2 new StringFormat objects StringFormat^ format1 = gcnew StringFormat( StringFormatFlags::NoClip ); StringFormat^ format2 = gcnew StringFormat( format1 ); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1->LineAlignment = StringAlignment::Near; format1->Alignment = StringAlignment::Center; format2->LineAlignment = StringAlignment::Center; format2->Alignment = StringAlignment::Far; // Draw the bounding rectangle and a string for each // StringFormat object. e->Graphics->DrawRectangle( Pens::Black, displayRectangle ); e->Graphics->DrawString( "Showing Format1", this->Font, Brushes::Red, displayRectangle, format1 ); e->Graphics->DrawString( "Showing Format2", this->Font, Brushes::Red, displayRectangle, format2 ); }
private void ShowLineAndAlignment(PaintEventArgs e) { // Construct a new Rectangle . Rectangle displayRectangle = new Rectangle(new Point(40, 40), new Size(80, 80)); // Construct 2 new StringFormat objects StringFormat format1 = new StringFormat(StringFormatFlags.NoClip); StringFormat format2 = new StringFormat(format1); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1.set_LineAlignment(StringAlignment.Near); format1.set_Alignment(StringAlignment.Center); format2.set_LineAlignment(StringAlignment.Center); format2.set_Alignment(StringAlignment.Far); // Draw the bounding rectangle and a string for each // StringFormat object. e.get_Graphics().DrawRectangle(Pens.get_Black(), displayRectangle); e.get_Graphics().DrawString("Showing Format1", this.get_Font(), Brushes.get_Red(), (RectangleF.op_Implicit((displayRectangle))), format1); e.get_Graphics().DrawString("Showing Format2", this.get_Font() , Brushes.get_Red(), RectangleF.op_Implicit((displayRectangle)), format2); } //ShowLineAndAlignment

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringFormat コンストラクタ (StringFormatFlags, Int32)
アセンブリ: System.Drawing (system.drawing.dll 内)

Dim options As StringFormatFlags Dim language As Integer Dim instance As New StringFormat(options, language)

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringFormat コンストラクタ

名前 | 説明 |
---|---|
StringFormat () | 新しい StringFormat オブジェクトを初期化します。 .NET Compact Framework によってサポートされています。 |
StringFormat (StringFormat) | 指定の既存 StringFormat オブジェクトから、新しい StringFormat オブジェクトを初期化します。 .NET Compact Framework によってサポートされています。 |
StringFormat (StringFormatFlags) | 指定の StringFormatFlags 列挙体を使用して、新しい StringFormat オブジェクトを初期化します。 .NET Compact Framework によってサポートされています。 |
StringFormat (StringFormatFlags, Int32) | 指定の StringFormatFlags 列挙体および言語を使用して、新しい StringFormat オブジェクトを初期化します。 |

StringFormat コンストラクタ (StringFormat)
アセンブリ: System.Drawing (system.drawing.dll 内)


この例は、Windows フォームでの使用を意図してデザインされています。コードをフォームに貼り付け、フォームの Paint イベントを処理するときに PaintEventArgs の e を渡して ShowLineAndAlignment メソッドを呼び出します。
Private Sub ShowLineAndAlignment(ByVal e As PaintEventArgs) ' Construct a new Rectangle. Dim displayRectangle _ As New Rectangle(New Point(40, 40), New Size(80, 80)) ' Construct two new StringFormat objects Dim format1 As New StringFormat(StringFormatFlags.NoClip) Dim format2 As New StringFormat(format1) ' Set the LineAlignment and Alignment properties for ' both StringFormat objects to different values. format1.LineAlignment = StringAlignment.Near format1.Alignment = StringAlignment.Center format2.LineAlignment = StringAlignment.Center format2.Alignment = StringAlignment.Far ' Draw the bounding rectangle and a string for each ' StringFormat object. e.Graphics.DrawRectangle(Pens.Black, displayRectangle) e.Graphics.DrawString("Showing Format1", Me.Font, Brushes.Red, _ RectangleF.op_Implicit(displayRectangle), format1) e.Graphics.DrawString("Showing Format2", Me.Font, Brushes.Red, _ RectangleF.op_Implicit(displayRectangle), format2) End Sub
private void ShowLineAndAlignment(PaintEventArgs e) { // Construct a new Rectangle . Rectangle displayRectangle = new Rectangle (new Point(40, 40), new Size (80, 80)); // Construct 2 new StringFormat objects StringFormat format1 = new StringFormat(StringFormatFlags.NoClip); StringFormat format2 = new StringFormat(format1); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1.LineAlignment = StringAlignment.Near; format1.Alignment = StringAlignment.Center; format2.LineAlignment = StringAlignment.Center; format2.Alignment = StringAlignment.Far; // Draw the bounding rectangle and a string for each // StringFormat object. e.Graphics.DrawRectangle(Pens.Black, displayRectangle); e.Graphics.DrawString("Showing Format1", this.Font, Brushes.Red, (RectangleF)displayRectangle, format1); e.Graphics.DrawString("Showing Format2", this.Font, Brushes.Red, (RectangleF)displayRectangle, format2); }
private: void ShowLineAndAlignment( PaintEventArgs^ e ) { // Construct a new Rectangle . Rectangle displayRectangle = Rectangle(Point(40,40),System::Drawing::Size( 80, 80 )); // Construct 2 new StringFormat objects StringFormat^ format1 = gcnew StringFormat( StringFormatFlags::NoClip ); StringFormat^ format2 = gcnew StringFormat( format1 ); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1->LineAlignment = StringAlignment::Near; format1->Alignment = StringAlignment::Center; format2->LineAlignment = StringAlignment::Center; format2->Alignment = StringAlignment::Far; // Draw the bounding rectangle and a string for each // StringFormat object. e->Graphics->DrawRectangle( Pens::Black, displayRectangle ); e->Graphics->DrawString( "Showing Format1", this->Font, Brushes::Red, displayRectangle, format1 ); e->Graphics->DrawString( "Showing Format2", this->Font, Brushes::Red, displayRectangle, format2 ); }
private void ShowLineAndAlignment(PaintEventArgs e) { // Construct a new Rectangle . Rectangle displayRectangle = new Rectangle(new Point(40, 40), new Size(80, 80)); // Construct 2 new StringFormat objects StringFormat format1 = new StringFormat(StringFormatFlags.NoClip); StringFormat format2 = new StringFormat(format1); // Set the LineAlignment and Alignment properties for // both StringFormat objects to different values. format1.set_LineAlignment(StringAlignment.Near); format1.set_Alignment(StringAlignment.Center); format2.set_LineAlignment(StringAlignment.Center); format2.set_Alignment(StringAlignment.Far); // Draw the bounding rectangle and a string for each // StringFormat object. e.get_Graphics().DrawRectangle(Pens.get_Black(), displayRectangle); e.get_Graphics().DrawString("Showing Format1", this.get_Font(), Brushes.get_Red(), (RectangleF.op_Implicit((displayRectangle))), format1); e.get_Graphics().DrawString("Showing Format2", this.get_Font() , Brushes.get_Red(), RectangleF.op_Implicit((displayRectangle)), format2); } //ShowLineAndAlignment

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


StringFormat プロパティ

名前 | 説明 | |
---|---|---|
![]() | HotkeyPrefix | この StringFormat オブジェクトの HotkeyPrefix オブジェクトを取得または設定します。 |
![]() ![]() | Trimming | この StringFormat オブジェクトの StringTrimming 列挙体を取得または設定します。 |

StringFormat メソッド

名前 | 説明 | |
---|---|---|
![]() | Clone | この StringFormat オブジェクトの同一コピーを作成します。 |
![]() | CreateObjRef | リモート オブジェクトとの通信に使用するプロキシの生成に必要な情報をすべて格納しているオブジェクトを作成します。 ( MarshalByRefObject から継承されます。) |
![]() | Dispose | この StringFormat オブジェクトによって使用されているすべてのリソースを解放します。 |
![]() | Equals | オーバーロードされます。 2 つの Object インスタンスが等しいかどうかを判断します。 ( Object から継承されます。) |
![]() | GetHashCode | 特定の型のハッシュ関数として機能します。GetHashCode は、ハッシュ アルゴリズムや、ハッシュ テーブルのようなデータ構造での使用に適しています。 ( Object から継承されます。) |
![]() | GetLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、現在の有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | GetTabStops | この StringFormat オブジェクトのタブ ストップを取得します。 |
![]() | GetType | 現在のインスタンスの Type を取得します。 ( Object から継承されます。) |
![]() | InitializeLifetimeService | 対象のインスタンスの有効期間ポリシーを制御する、有効期間サービス オブジェクトを取得します。 ( MarshalByRefObject から継承されます。) |
![]() | ReferenceEquals | 指定した複数の Object インスタンスが同一かどうかを判断します。 ( Object から継承されます。) |
![]() | SetDigitSubstitution | 西ヨーロッパ言語の数字をローカルの数字に置換する際に使用される言語と方法を指定します。 |
![]() | SetMeasurableCharacterRanges | MeasureCharacterRanges メソッドを呼び出したときに計測される、文字の範囲を表す CharacterRange 構造体の配列を指定します。 |
![]() | SetTabStops | この StringFormat オブジェクトのタブ ストップを設定します。 |
![]() | ToString | オーバーライドされます。 この StringFormat オブジェクトをユーザーが判読できる文字列に変換します。 |

名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 ( Object から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

StringFormat メンバ
配置、方向、タブ ストップなどのテキスト レイアウト情報、省略記号の挿入や国別の代替の数字形式などの表示方法、および OpenType 機能をカプセル化します。このクラスは継承できません。
StringFormat データ型で公開されるメンバを以下の表に示します。


名前 | 説明 | |
---|---|---|
![]() | HotkeyPrefix | この StringFormat オブジェクトの HotkeyPrefix オブジェクトを取得または設定します。 |
![]() ![]() | Trimming | この StringFormat オブジェクトの StringTrimming 列挙体を取得または設定します。 |


名前 | 説明 | |
---|---|---|
![]() | Finalize | Object がガベージ コレクションにより収集される前に、その Object がリソースを解放し、その他のクリーンアップ操作を実行できるようにします。 (Object から継承されます。) |
![]() | MemberwiseClone | オーバーロードされます。 ( MarshalByRefObject から継承されます。) |

Weblioに収録されているすべての辞書からStringFormatを検索する場合は、下記のリンクをクリックしてください。

- StringFormatのページへのリンク