StringFormatとは? わかりやすく解説

String.Format メソッド (IFormatProvider, String, Object[])

指定した String の書式項目を、指定した配列内の対応する Object インスタンスの値と等価のテキストに置換します。指定したパラメータにより、カルチャ固有の書式設定情報が提供されます。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Format
 ( _
    provider As IFormatProvider, _
    format As String, _
    ParamArray args As Object()
 _
) As String
public static string Format
 (
    IFormatProvider provider,
    string format,
    params Object[] args
)
public:
static String^ Format (
    IFormatProvider^ provider, 
    String^ format, 
    ... array<Object^>^ args
)
public static String Format (
    IFormatProvider provider, 
    String format, 
    Object[] args
)
public static function Format
 (
    provider : IFormatProvider, 
    format : String, 
    ... args : Object[]
) : String

パラメータ

provider

カルチャ固有の書式情報を提供する IFormatProvider。

format

0 個以上の書式項目を含んだ String。

args

0 個以上の書式設定対象オブジェクトを含んだ Object 配列。

戻り値
書式項目が args の Object の対応インスタンスと等価の String に置換された format のコピー。

例外例外
例外の種類条件

ArgumentNullException

format または args が null 参照 (Visual Basic では Nothing) です。

FormatException

format が無効です。

または

書式設定対象の引数を示す数が 0 より小さいか、args 配列の長さ以上です。

解説解説

このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。

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 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれるスペースを表します。

"ブラッドの飼っている犬には 42______ 匹のノミがいる。"

使用例使用例

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。

' 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Format メソッド (String, Object[])

指定した String の書式項目を、指定した配列内の対応する Object インスタンスの値と等価のテキストに置換します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

format または args が null 参照 (Visual Basic では Nothing) です。

FormatException

format が無効です。

または

書式設定対象の引数を示す数が 0 より小さいか、args 配列の長さ以上です。

解説解説

このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。

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 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれるスペースを表します。

"ブラッドの飼っている犬には 42______ 匹のノミがいる。"

使用例使用例

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。

' 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Format メソッド (String, Object, Object, Object)

指定した String の書式項目を、指定した 3 つの Object インスタンスの値と等価のテキストに置換します。

名前空間: System
アセンブリ: 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)

パラメータ

format

0 個以上の書式項目を含んだ String。

arg0

書式設定する第 1 Object。

arg1

書式設定する第 2 Object。

arg2

書式指定する第 3 Object。

戻り値
第 1、第 2、第 3 の各書式項目が、それぞれ arg0、arg1、および arg2 と等価の String に置換された format のコピー。

例外例外
例外の種類条件

ArgumentNullException

format が null 参照 (Visual Basic では Nothing) です。

FormatException

format が無効です。

または

書式設定する引数を示す数が 0 より小さいか、書式設定する指定されたオブジェクトの数以上です。

解説解説

このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。

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 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれたスペースを表します。

"ブラッドの飼っている犬には 42______ 匹のノミがいる。"

使用例使用例

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。

' 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Format メソッド (String, Object)

指定した String の書式項目を、指定した Object インスタンスの値と等価のテキストに置換します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

例外例外
例外の種類条件

ArgumentNullException

format が null 参照 (Visual Basic では Nothing) です。

FormatException

format の書式項目が無効です。

または

書式設定する引数を示す数が 0 より小さいか、書式設定する指定されたオブジェクトの数以上です。

解説解説

このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。

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 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれたスペースを表します。

"ブラッドの飼っている犬には 42______ 匹のノミがいる。"

使用例使用例

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。

' 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

String.Format メソッド

指定した String の各書式項目を、対応するオブジェクトの値と等価のテキストに置換します。
オーバーロードの一覧オーバーロードの一覧

参照参照

String.Format メソッド (String, Object, Object)

指定した String の書式項目を、指定した 2 つの Object インスタンスの値と等価のテキストに置換します。

名前空間: System
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function Format
 ( _
    format As String, _
    arg0 As Object, _
    arg1 As Object _
) As String

パラメータ

format

0 個以上の書式項目を含んだ String。

arg0

書式設定する第 1 Object。

arg1

書式設定する第 2 Object。

戻り値
1 番目と 2 番目の書式項目が、arg0 および arg1 と等価の String に置換された format のコピー。

例外例外
例外の種類条件

ArgumentNullException

format が null 参照 (Visual Basic では Nothing) です。

FormatException

format が無効です。

または

書式設定する引数を示す数が 0 より小さいか、書式設定する指定されたオブジェクトの数以上です。

解説解説

このメソッドでは、.NET Framework の複合書式指定機能を使用して、オブジェクトの値を対応するテキスト表現に変換し、そのテキスト表現を文字列中に埋め込みます。.NET Framework は、さまざまな書式設定機能をサポートしています。詳細については、書式設定機能について解説した次のトピックを参照してください。

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 の場合、戻り値は次のようになります。この例では、アンダースコアは埋め込まれたスペースを表します。

"ブラッドの飼っている犬には 42______ 匹のノミがいる。"

使用例使用例

数値、日付、および列挙体の標準的な書式指定子を使用したコード例を次に示します。

' 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

*/
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

StringFormat クラス

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

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

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

StringFormatFlags 列挙体により、多数の一般的な形式を使用できます。StringFormat オブジェクトは変更可能です。

継承階層継承階層
System.Object
   System.MarshalByRefObject
    System.Drawing.StringFormat
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

StringFormat コンストラクタ ()


StringFormat コンストラクタ (StringFormatFlags)

指定の StringFormatFlags 列挙体を使用して、新しい StringFormat オブジェクトを初期化します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Sub New ( _
    options As StringFormatFlags _
)
Dim options As StringFormatFlags

Dim instance As New StringFormat(options)
public StringFormat (
    StringFormatFlags options
)
public:
StringFormat (
    StringFormatFlags options
)
public StringFormat (
    StringFormatFlags options
)
public function StringFormat (
    options : StringFormatFlags
)

パラメータ

options

新しい StringFormat オブジェクトの StringFormatFlags 列挙体。

使用例使用例

次のメンバのコード例を次に示します。

  • Rectangle

  • StringFormat

  • StringFormat

  • Alignment

  • LineAlignment

  • StringAlignment

  • StringFormatFlags

この例は、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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

StringFormat コンストラクタ (StringFormatFlags, Int32)

指定の StringFormatFlags 列挙体および言語を使用して、新しい StringFormat オブジェクトを初期化します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Sub New ( _
    options As StringFormatFlags, _
    language As Integer _
)
Dim options As StringFormatFlags
Dim language As Integer

Dim instance As New StringFormat(options,
 language)
public StringFormat (
    StringFormatFlags options,
    int language
)
public:
StringFormat (
    StringFormatFlags options, 
    int language
)
public StringFormat (
    StringFormatFlags options, 
    int language
)
public function StringFormat (
    options : StringFormatFlags, 
    language : int
)

パラメータ

options

新しい StringFormat オブジェクトの StringFormatFlags 列挙体。

language

テキストの言語を示す値。

プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

StringFormat コンストラクタ

新しい 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 名前空間

StringFormat コンストラクタ (StringFormat)

指定の既存 StringFormat オブジェクトから、新しい StringFormat オブジェクトを初期化します。

名前空間: System.Drawing
アセンブリ: System.Drawing (system.drawing.dll 内)
構文構文

Public Sub New ( _
    format As StringFormat _
)
Dim format As StringFormat

Dim instance As New StringFormat(format)
public StringFormat (
    StringFormat format
)
public:
StringFormat (
    StringFormat^ format
)
public StringFormat (
    StringFormat format
)
public function StringFormat (
    format : StringFormat
)

パラメータ

format

新しい StringFormat オブジェクトを初期化する場合に使用する StringFormat オブジェクト。

使用例使用例

次のメンバのコード例を次に示します。

  • Rectangle

  • StringFormat

  • StringFormat

  • Alignment

  • LineAlignment

  • StringAlignment

  • StringFormatFlags

この例は、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
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照

StringFormat プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ HotkeyPrefix この StringFormat オブジェクトの HotkeyPrefix オブジェクトを取得または設定します。
パブリック プロパティ .NET Compact Framework によるサポート Trimming この StringFormat オブジェクトの StringTrimming 列挙体を取得または設定します。
参照参照

関連項目

StringFormat クラス
System.Drawing 名前空間

その他の技術情報

フォントとテキストの使用

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 オブジェクトをユーザーが判読できる文字列に変換します。
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

StringFormat クラス
System.Drawing 名前空間

その他の技術情報

フォントとテキストの使用

StringFormat メンバ

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

StringFormat データ型で公開されるメンバを以下の表に示します。


パブリック コンストラクタパブリック コンストラクタ
パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ HotkeyPrefix この StringFormat オブジェクトの HotkeyPrefix オブジェクトを取得または設定します。
パブリック プロパティ .NET Compact Framework によるサポート Trimming この StringFormat オブジェクトの StringTrimming 列挙体を取得または設定します。
パブリック メソッドパブリック メソッド
( プロテクト メソッド も参照)
  名前 説明
パブリック メソッド 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 オブジェクトをユーザーが判読できる文字列に変換します。
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

StringFormat クラス
System.Drawing 名前空間

その他の技術情報

フォントとテキストの使用



英和和英テキスト翻訳

英語⇒日本語日本語⇒英語

辞書ショートカット

すべての辞書の索引

「StringFormat」の関連用語

StringFormatのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



StringFormatのページの著作権

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2026 Microsoft.All rights reserved.

©2026 GRAS Group, Inc.RSS