String.Formatとは? わかりやすく解説

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 配列

戻り値
書式項目が argsObject の対応インスタンス等価String置換されformatコピー

例外例外
例外種類条件

ArgumentNullException

format または argsnull 参照 (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 または argsnull 参照 (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各書式項目が、それぞれ arg0arg1、および arg2等価String置換されformatコピー

例外例外
例外種類条件

ArgumentNullException

formatnull 参照 (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:####} 冊お買い上げいただきありがとうございました。" で、arg0123 という値を持つ Int16 だとすると、戻り値次のようになります

"『Microsoft® .NET (Core Reference)』を 123お買い上げいただきありがとうございました。"

format の値が "ブラッド飼っているには {0,-8:G} 匹のノミがいる。" で、arg042 という値を持つ 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

formatnull 参照 (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:####} 冊お買い上げいただきありがとうございました。" で、arg0123 という値を持つ Int16 だとすると、戻り値次のようになります

"『Microsoft® .NET (Core Reference)』を 123お買い上げいただきありがとうございました。"

format の値が "ブラッド飼っているには {0,-8:G} 匹のノミがいる。" で、arg042 という値を持つ 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

formatnull 参照 (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:####} 冊お買い上げいただきありがとうございました。" で、arg0123 という値を持つ Int16 だとすると、戻り値次のようになります

"『Microsoft® .NET (Core Reference)』を 123お買い上げいただきありがとうございました。"

format の値が "ブラッド飼っているには {0,-8:G} 匹のノミがいる。" で、arg042 という値を持つ 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 名前空間

その他の技術情報

フォントテキスト使用


このページでは「.NET Framework クラス ライブラリ リファレンス」からString.Formatを検索した結果を表示しています。
Weblioに収録されているすべての辞書からString.Formatを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からString.Format を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「String.Format」の関連用語

String.Formatのお隣キーワード
検索ランキング

   

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



String.Formatのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2024 GRAS Group, Inc.RSS