BigInteger.TryParse メソッド

定義

数値の文字列形式を対応する BigInteger 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

オーバーロード

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, BigInteger)

数値の文字列形式を対応する BigInteger 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

TryParse(String, IFormatProvider, BigInteger)

文字列を値に解析しようとします。

TryParse(ReadOnlySpan<Char>, IFormatProvider, BigInteger)

文字のスパンを値に解析しようとします。

TryParse(String, BigInteger)

数値の文字列形式を対応する BigInteger 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

TryParse(ReadOnlySpan<Char>, BigInteger)

指定された文字の読み取り専用スパンに含まれた数値の表現を、等価の BigInteger に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

TryParse(String, NumberStyles, IFormatProvider, BigInteger)

指定されたスタイルおよびカルチャ固有の書式の数値の文字列形式を等価の BigInteger に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, BigInteger)

数値の文字列形式を対応する BigInteger 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

public:
 static bool TryParse(ReadOnlySpan<char> value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public:
 static bool TryParse(ReadOnlySpan<char> value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = System::Numerics::INumberBase<System::Numerics::BigInteger>::TryParse;
public static bool TryParse (ReadOnlySpan<char> value, System.Globalization.NumberStyles style, IFormatProvider? provider, out System.Numerics.BigInteger result);
public static bool TryParse (ReadOnlySpan<char> value, System.Globalization.NumberStyles style, IFormatProvider provider, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (value As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As BigInteger) As Boolean

パラメーター

value
ReadOnlySpan<Char>

文字の読み取り専用スパンとしての数値の表現。

style
NumberStyles

value で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。

provider
IFormatProvider

value に関するカルチャ固有の書式情報を提供するオブジェクト。

result
BigInteger

このメソッドから制御が戻るときに、value に含まれる数値と等価の BigInteger が格納されます。変換に失敗した場合は Zero が格納されます。 value パラメーターが空の文字スパンの場合、または style に準拠した形式ではない場合、変換は失敗します。 このパラメーターは初期化せずに渡されます。

戻り値

value が正常に変換された場合は true。それ以外の場合は false

例外

styleNumberStyles 値ではありません。

- または -

style には、別の値と共に AllowHexSpecifier または HexNumber フラグが含まれます。

注釈

このオーバーロードは メソッドに Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) 似ていますが、変換が失敗しても例外がスローされないことを除きます。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをvalueテストFormatExceptionするために例外処理を使用する必要がなくなります。

パラメーターは style 、解析操作を成功させるために パラメーターで value 許可されるスタイル要素 (空白や正または負の符号など) を定義します。 列挙体のビット フラグ NumberStyles の組み合わせである必要があります。 の style値に応じて、 パラメーターに value 次の要素を含めることができます。

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

パラメーターに が style 含まれている AllowHexSpecifier場合、パラメーターには value 次の要素を含めることができます。

[ws]hexdigits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。 空白は、 が フラグを含むNumberStyles.AllowLeadingWhite場合styleは のvalue先頭に、フラグが含まれている場合styleは のvalue末尾にNumberStyles.AllowTrailingWhite表示されます。
$ カルチャ固有の通貨記号。 のvalue位置は、 パラメーターの NumberFormatInfoCurrencyPositivePattern メソッドによって返されるオブジェクトの provider プロパティによってGetFormat定義されます。 に フラグが含まれている場合styleは、通貨記号を にvalueNumberStyles.AllowCurrencySymbol表示できます。
sign 省略可能な記号。 署名は、 フラグが含まれている場合は のvalue先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合styleは のvalue末尾にNumberStyles.AllowTrailingSign表示styleされます。 に フラグが含まれている場合styleは、かっこを使用valueして負の値をNumberStyles.AllowParentheses示すことができます。
数値 0 から 9 までの数字のシーケンス。
, カルチャ固有のグループ区切り記号。 でprovider指定されたカルチャのグループ区切り記号は、 に フラグが含まれている場合stylevalueNumberStyles.AllowThousands表示できます。
. カルチャ固有の小数点記号。 でprovider指定されたカルチャの小数点記号は、 に フラグが含まれている場合stylevalueNumberStyles.AllowDecimalPoint表示できます。
fractional_digits 数字 0 が 1 回以上出現します。 小数部の数字は、 に value フラグがNumberStyles.AllowDecimalPoint含まれている場合styleにのみ表示されます。
E "e" または "E" 文字。値が指数 (指数) 表記で表されることを示します。 フラグが含まれている場合style、パラメーターはvalue指数表記で数値をNumberStyles.AllowExponent表すことができます。
exponential_digits 0 から 9 までの数字のシーケンス。 フラグが含まれている場合style、パラメーターはvalue指数表記で数値をNumberStyles.AllowExponent表すことができます。
hexdigits 0 から f、または 0 から F までの 16 進数のシーケンス。

注意

の終端の NUL (U+0000) 文字 s は、引数の style 値に関係なく、解析操作では無視されます。

非複合 NumberStyles 数字に加えて値で許可される要素
None 10 進数のみ。
AllowDecimalPoint 小数点 (.) 要素と fractional_digits 要素。 ただし、 fractional_digits は 1 つ以上の 0 桁のみで構成する必要があります。または、 メソッドは を返します false
AllowExponent 指数表記と exponential_digitsを示す "e" または "E" 文字。 指数表記で数値を表す場合 value は、0 以外の小数部を含めることはできません。
AllowLeadingWhite の先頭valueにある ws 要素。
AllowTrailingWhite の末尾valueにある ws 要素。
AllowLeadingSign 数字の前の sign 要素。
AllowTrailingSign 数字の後の sign 要素。
AllowParentheses 数値を囲むかっこの形式の sign 要素。
AllowThousands グループ区切り記号 (,) 要素。
AllowCurrencySymbol currency ($) 要素。
Currency すべての要素。 ただし、 value 16 進数または数値を指数表記で表すことはできません。
Float の先頭または末尾の valuews 要素、の先頭value符号、および小数点 (.) 記号。 パラメーターでは value 、指数表記を使用することもできます。
Number wssign、group separator (,)、および decimal point (.) 要素。
Any すべての要素。 ただし、 value 16 進数を表すことはできません。

フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 value 16 進値にする必要があります。 に存在できるその他の style フラグは NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStylesHexNumber両方の空白フラグを含む複合スタイル があります)。

パラメーターは provider 実装です IFormatProvider 。 そのメソッドは GetFormat 、 の形式に NumberFormatInfo 関するカルチャ固有の value情報を提供する オブジェクトを返します。 パラメーターには provider 、次のいずれかを指定できます。

  • CultureInfo書式設定情報を提供するカルチャを表す オブジェクト。 そのメソッドは GetFormat 、そのカルチャの NumberFormatInfo 数値書式情報を提供する オブジェクトを返します。

  • NumberFormatInfo数値書式情報を提供する オブジェクト。 (の実装 GetFormat は、それ自体を返すだけです)。

  • を実装 IFormatProviderするカスタム オブジェクト。 そのメソッドは GetFormat 、書式設定情報を提供する オブジェクトを NumberFormatInfo インスタンス化して返します。

nullの場合providerは、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。

こちらもご覧ください

適用対象

TryParse(String, IFormatProvider, BigInteger)

文字列を値に解析しようとします。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = IParsable<System::Numerics::BigInteger>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : string * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As BigInteger) As Boolean

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
BigInteger

このメソッドが戻ったとき、 には、正常に解析 s された結果または失敗した場合に未定義の値が含まれます。

戻り値

true が正常に解析された場合 s は 。それ以外の場合 falseは 。

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, BigInteger)

文字のスパンを値に解析しようとします。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = ISpanParsable<System::Numerics::BigInteger>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As BigInteger) As Boolean

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
BigInteger

このメソッドが返されると、 には、正常に解析 sされた結果、または失敗した場合に未定義の値が格納されます。

戻り値

true が正常に解析された場合 s は 。それ以外の場合 falseは 。

適用対象

TryParse(String, BigInteger)

数値の文字列形式を対応する BigInteger 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

public:
 static bool TryParse(System::String ^ value, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public static bool TryParse (string value, out System.Numerics.BigInteger result);
public static bool TryParse (string? value, out System.Numerics.BigInteger result);
static member TryParse : string * BigInteger -> bool
Public Shared Function TryParse (value As String, ByRef result As BigInteger) As Boolean

パラメーター

value
String

数値の文字列形式。

result
BigInteger

このメソッドから制御が戻るときに、value に含まれる数値と等価の BigInteger が格納されます。変換に失敗した場合は 0 が格納されます。 value パラメーターが null の場合、または正しい形式ではない場合、変換は失敗します。 このパラメーターは初期化せずに渡されます。

戻り値

value が正常に変換された場合は true。それ以外の場合は false

例外

valuenullです。

次の例では、 メソッドを TryParse(String, BigInteger) 使用して 2 つの BigInteger オブジェクトをインスタンス化します。 変換が成功した場合は、各オブジェクトに別の数値を乗算し、 メソッドを Compare 呼び出して 2 つのオブジェクト間のリレーションシップを決定します。

BigInteger number1, number2;
bool succeeded1 = BigInteger.TryParse("-12347534159895123", out number1);
bool succeeded2 = BigInteger.TryParse("987654321357159852", out number2);
if (succeeded1 && succeeded2)
{
   number1 *= 3;
   number2 *= 2;
   switch (BigInteger.Compare(number1, number2))
   {
      case -1:
         Console.WriteLine("{0} is greater than {1}.", number2, number1);
         break;
      case 0:
         Console.WriteLine("{0} is equal to {1}.", number1, number2);
         break;
      case 1:
         Console.WriteLine("{0} is greater than {1}.", number1, number2);
         break;
   }
}
else
{
   if (! succeeded1)
      Console.WriteLine("Unable to initialize the first BigInteger value.");

   if (! succeeded2)
      Console.WriteLine("Unable to initialize the second BigInteger value.");
}
// The example displays the following output:
//      1975308642714319704 is greater than -37042602479685369.
Dim number1 As BigInteger = BigInteger.Zero
Dim number2 As BigInteger = BigInteger.Zero
Dim succeeded1 As Boolean = BigInteger.TryParse("-12347534159895123", number1)
Dim succeeded2 As Boolean = BigInteger.TryParse("987654321357159852", number2)
If succeeded1 AndAlso succeeded2
   number1 *= 3
   number2 *= 2
   Select Case BigInteger.Compare(number1, number2)
      Case -1
         Console.WriteLine("{0} is greater than {1}.", number2, number1)
      Case 0
         Console.WriteLine("{0} is equal to {1}.", number1, number2)
      Case 1
         Console.WriteLine("{0} is greater than {1}.", number1, number2)
   End Select      
Else
   If Not succeeded1 Then 
      Console.WriteLine("Unable to initialize the first BigInteger value.")
   End If
   If Not succeeded2 Then
      Console.WriteLine("Unable to initialize the second BigInteger value.")
   
   End If
End If
' The example displays the following output:
'      1975308642714319704 is greater than -37042602479685369.

注釈

メソッドは TryParse(String, BigInteger) メソッドに Parse(String) 似ていますが、変換が失敗しても例外はスローされません。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをvalueテストFormatExceptionするために例外処理を使用する必要がなくなります。

パラメーターは value 、次の形式の 10 進数の文字列表現である必要があります。

[ws][sign]digits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。
sign 省略可能な記号。 有効な符号文字は、現在のカルチャの NumberFormatInfo.NegativeSign プロパティと NumberFormatInfo.PositiveSign プロパティによって決まります。
数値 0 から 9 までの 10 進数のシーケンス。

注意

パラメーターで value 指定された文字列には、グループ区切り記号または小数点記号を含めることはできません。また、小数部を含めることはできません。

パラメーターは value 、 スタイルを使用 NumberStyles.Integer して解釈されます。 10 進数に加えて、先頭と末尾に先頭の記号が付いたスペースのみを使用できます。 に存在できるカルチャ固有の書式設定情報を使用してスタイル要素を明示的に value定義するには、 メソッドを TryParse(String, NumberStyles, IFormatProvider, BigInteger) 呼び出します。

パラメーターは value 、現在のカルチャの オブジェクトの書式設定情報を NumberFormatInfo 使用して解析されます。 詳細については、「NumberFormatInfo.CurrentInfo」を参照してください。

このオーバーロードは、パラメーター内のすべての数字を value 10 進数として解釈します。 16 進数の文字列表現を解析するには、代わりに オーバーロードを TryParse(String, NumberStyles, IFormatProvider, BigInteger) 呼び出します。

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Char>, BigInteger)

指定された文字の読み取り専用スパンに含まれた数値の表現を、等価の BigInteger に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

public:
 static bool TryParse(ReadOnlySpan<char> value, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public static bool TryParse (ReadOnlySpan<char> value, out System.Numerics.BigInteger result);
static member TryParse : ReadOnlySpan<char> * BigInteger -> bool
Public Shared Function TryParse (value As ReadOnlySpan(Of Char), ByRef result As BigInteger) As Boolean

パラメーター

value
ReadOnlySpan<Char>

文字の読み取り専用スパンとしての数値の表現。

result
BigInteger

このメソッドから制御が戻るときに、value に含まれる数値と等価の BigInteger が格納されます。変換に失敗した場合は 0 が格納されます。 value パラメーターが空の文字スパンである場合、または正しい形式ではない場合、変換は失敗します。 このパラメーターは初期化せずに渡されます。

戻り値

value が正常に変換された場合は true。それ以外の場合は false

こちらもご覧ください

適用対象

TryParse(String, NumberStyles, IFormatProvider, BigInteger)

指定されたスタイルおよびカルチャ固有の書式の数値の文字列形式を等価の BigInteger に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。

public:
 static bool TryParse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result);
public:
 static bool TryParse(System::String ^ value, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Numerics::BigInteger % result) = System::Numerics::INumberBase<System::Numerics::BigInteger>::TryParse;
public static bool TryParse (string value, System.Globalization.NumberStyles style, IFormatProvider provider, out System.Numerics.BigInteger result);
public static bool TryParse (string? value, System.Globalization.NumberStyles style, IFormatProvider? provider, out System.Numerics.BigInteger result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * BigInteger -> bool
Public Shared Function TryParse (value As String, style As NumberStyles, provider As IFormatProvider, ByRef result As BigInteger) As Boolean

パラメーター

value
String

数値の文字列形式。 文字列は、style で指定されたスタイルを使用して解釈されます。

style
NumberStyles

value で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。

provider
IFormatProvider

value に関するカルチャ固有の書式情報を提供するオブジェクト。

result
BigInteger

このメソッドから制御が戻るときに、value に含まれる数値と等価の BigInteger が格納されます。変換に失敗した場合は Zero が格納されます。 value パラメーターが null の場合、または style に従った形式ではない場合、変換は失敗します。 このパラメーターは初期化せずに渡されます。

戻り値

value パラメーターが正常に変換された場合は true。それ以外の場合は false

例外

styleNumberStyles 値ではありません。

- または -

style には、別の値と共に AllowHexSpecifier または HexNumber フラグが含まれます。

次の例では、 パラメーターと provider パラメーターのTryParse(String, NumberStyles, IFormatProvider, BigInteger)styleのさまざまな組み合わせを使用して、 メソッドを呼び出します。

string numericString;
BigInteger number = BigInteger.Zero;

// Call TryParse with default values of style and provider.
numericString = "  -300   ";
if (BigInteger.TryParse(numericString, NumberStyles.Integer,
                       null, out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with the default value of style and
// a provider supporting the tilde as negative sign.
numericString = "  -300   ";
if (BigInteger.TryParse(numericString, NumberStyles.Integer,
                       new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with only AllowLeadingWhite and AllowTrailingWhite.
// Method returns false because of presence of negative sign.
numericString = "  -500   ";
if (BigInteger.TryParse(numericString,
                        NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite,
                        new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with AllowHexSpecifier and a hex value.
numericString = "F14237FFAAC086455192";
if (BigInteger.TryParse(numericString,
                        NumberStyles.AllowHexSpecifier,
                        null, out number))
   Console.WriteLine("'{0}' was converted to {1} (0x{1:x}).",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with AllowHexSpecifier and a negative hex value.
// Conversion fails because of presence of negative sign.
numericString = "-3af";
if (BigInteger.TryParse(numericString, NumberStyles.AllowHexSpecifier,
                       new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with only NumberStyles.None.
// Conversion fails because of presence of white space and sign.
numericString = " -300 ";
if (BigInteger.TryParse(numericString, NumberStyles.None,
                       new BigIntegerFormatProvider(), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
// Conversion fails because the string is formatted for the en-US culture.
numericString = "9,031,425,666,123,546.00";
if (BigInteger.TryParse(numericString, NumberStyles.Any,
                       new CultureInfo("fr-FR"), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);

// Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
// Conversion succeeds because the string is properly formatted
// For the fr-FR culture.
numericString = "9 031 425 666 123 546,00";
if (BigInteger.TryParse(numericString, NumberStyles.Any,
                       new CultureInfo("fr-FR"), out number))
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number);
else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString);
// The example displays the following output:
//    '  -300   ' was converted to -300.
//    Conversion of '  -300   ' to a BigInteger failed.
//    Conversion of '  -500   ' to a BigInteger failed.
//    'F14237FFAAC086455192' was converted to -69613977002644837412462 (0xf14237ffaac086455192).
//    Conversion of '-3af' to a BigInteger failed.
//    Conversion of ' -300 ' to a BigInteger failed.
//    Conversion of '9,031,425,666,123,546.00' to a BigInteger failed.
//    '9 031 425 666 123 546,00' was converted to 9031425666123546.
Dim numericString As String
Dim number As BigInteger = BigInteger.Zero

' Call TryParse with default values of style and provider.
numericString = "  -300   "
If BigInteger.TryParse(numericString, NumberStyles.Integer,
                       Nothing, number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with the default value of style and 
' a provider supporting the tilde as negative sign.
numericString = "  -300   "
If BigInteger.TryParse(numericString, NumberStyles.Integer,
                       New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with only AllowLeadingWhite and AllowTrailingWhite.
' Method returns false because of presence of negative sign.
numericString = "  -500   "
If BigInteger.TryParse(numericString,
                    NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite,
                    New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with AllowHexSpecifier and a hex value.
numericString = "F14237FFAAC086455192"
If BigInteger.TryParse(numericString,
                    NumberStyles.AllowHexSpecifier,
                    Nothing, number) Then
   Console.WriteLine("'{0}' was converted to {1} (0x{1:x}).",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with AllowHexSpecifier and a negative hex value.
' Conversion fails because of presence of negative sign.
numericString = "-3af"
If BigInteger.TryParse(numericString, NumberStyles.AllowHexSpecifier,
                       New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If                                             

' Call TryParse with only NumberStyles.None.
' Conversion fails because of presence of white space and sign.
numericString = " -300 "
If BigInteger.TryParse(numericString, NumberStyles.None,
                       New BigIntegerFormatProvider(), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If 
                                            
' Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
' Conversion fails because the string is formatted for the en-US culture.
numericString = "9,031,425,666,123,546.00"
If BigInteger.TryParse(numericString, NumberStyles.Any,
                       New CultureInfo("fr-FR"), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If

' Call TryParse with NumberStyles.Any and a provider for the fr-FR culture.
' Conversion succeeds because the string is properly formatted 
' For the fr-FR culture.
numericString = "9 031 425 666 123 546,00"
If BigInteger.TryParse(numericString, NumberStyles.Any,
                       New CultureInfo("fr-FR"), number) Then
   Console.WriteLine("'{0}' was converted to {1}.",
                     numericString, number)                             
Else
   Console.WriteLine("Conversion of '{0}' to a BigInteger failed.",
                     numericString)
End If
' The example displays the following output:
'    '  -300   ' was converted to -300.
'    Conversion of '  -300   ' to a BigInteger failed.
'    Conversion of '  -500   ' to a BigInteger failed.
'    'F14237FFAAC086455192' was converted to -69613977002644837412462 (0xf14237ffaac086455192).
'    Conversion of '-3af' to a BigInteger failed.
'    Conversion of ' -300 ' to a BigInteger failed.
'    Conversion of '9,031,425,666,123,546.00' to a BigInteger failed.
'    '9 031 425 666 123 546,00' was converted to 9031425666123546.

メソッドの TryParse(String, NumberStyles, IFormatProvider, BigInteger) 個々の呼び出しの数は、チルダ (~) を負の符号として定義する次 BigIntegerFormatProvider のクラスのインスタンスを渡します。

public class BigIntegerFormatProvider : IFormatProvider
{
   public object GetFormat(Type formatType)
   {
      if (formatType == typeof(NumberFormatInfo))
      {
         NumberFormatInfo numberFormat = new NumberFormatInfo();
         numberFormat.NegativeSign = "~";
         return numberFormat;
      }
      else
      {
         return null;
      }
   }
}
Public Class BigIntegerFormatProvider : Implements IFormatProvider
   Public Function GetFormat(formatType As Type) As Object _
                            Implements IFormatProvider.GetFormat
      If formatType Is GetType(NumberFormatInfo) Then
         Dim numberFormat As New NumberFormatInfo
         numberFormat.NegativeSign = "~"
         Return numberFormat
      Else
         Return Nothing
      End If      
   End Function
End Class

注釈

メソッドは TryParse(String, NumberStyles, IFormatProvider, BigInteger) メソッドに Parse(String, NumberStyles, IFormatProvider) 似ていますが、変換が失敗しても例外はスローされません。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをvalueテストFormatExceptionするために例外処理を使用する必要がなくなります。

パラメーターは style 、解析操作を成功させるためにパラメーターで value 許可されるスタイル要素 (空白や正または負の符号など) を定義します。 列挙からのビット フラグ NumberStyles の組み合わせである必要があります。 の style値によっては、パラメーターに value 次の要素が含まれる場合があります。

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

パラメーターに が style 含まれている AllowHexSpecifier場合、パラメーターには value 次の要素を含めることができます。

[ws]hexdigits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。 空白は、フラグが含まれている場合は のvalue先頭に表示され、フラグが含NumberStyles.AllowLeadingWhiteまれている場合styleは のvalue末尾にNumberStyles.AllowTrailingWhite表示styleされます。
$ カルチャ固有の通貨記号。 文字列内の位置は、 パラメーターの CurrencyPositivePatternNumberFormatInfo メソッドによって返される オブジェクトの provider プロパティによってGetFormat定義されます。 フラグが含まれている場合styleは、通貨記号を にvalueNumberStyles.AllowCurrencySymbol表示できます。
sign 省略可能な記号。 この記号は、 フラグを含む場合styleは のvalue先頭に表示され、フラグが含まれている場合styleは のvalue末尾にNumberStyles.AllowTrailingSign表示NumberStyles.AllowLeadingSignできます。 に フラグが含まれている場合style、かっこを使用valueして負の値をNumberStyles.AllowParentheses示すことができます。
数値 0 ~ 9 の数字のシーケンス。
, カルチャ固有のグループ区切り記号。 で指定されたproviderカルチャのグループ区切り記号は、 フラグが含まれている場合styleに にvalueNumberStyles.AllowThousands表示できます。
. カルチャ固有の小数点記号。 で指定されたproviderカルチャの小数点記号は、 フラグが含まれている場合styleに にvalueNumberStyles.AllowDecimalPoint表示できます。
fractional_digits 数字 0 の 1 つ以上の出現。 小数部の数字は、 フラグが value 含まれている場合 style にのみ に NumberStyles.AllowDecimalPoint 表示できます。
E "e" または "E" 文字。値が指数 (指数) 表記で表されることを示します。 フラグが含まれている場合style、パラメーターはvalue指数表記で数値をNumberStyles.AllowExponent表すことができます。
exponential_digits 0 ~ 9 の数字のシーケンス。 フラグが含まれている場合style、パラメーターはvalue指数表記で数値をNumberStyles.AllowExponent表すことができます。
hexdigits 0 から f、または 0 から F までの 16 進数のシーケンス。

注意

の終端の NUL (U+0000) 文字 s は、引数の style 値に関係なく、解析操作では無視されます。

10 進数のみの文字列 (フラグに NumberStyles.None 対応) は常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、この入力文字列に存在する可能性がありますが、存在する必要がない要素を制御します。 次の表は、 に存在する可能性がある要素に対する個々 NumberStyles のメンバーの value影響を示しています。

非複合 NumberStyles 数字に加えて値で許可される要素
None 10 進数のみ。
AllowDecimalPoint 小数点 (.) 要素と fractional_digits 要素。 ただし、 fractional_digits は 1 つ以上の 0 桁のみで構成する必要があります。または、 メソッドは を返します false
AllowExponent 指数表記と exponential_digitsを示す "e" または "E" 文字。 指数表記で数値を表す場合 value は、0 以外の小数部を含めることはできません。
AllowLeadingWhite の先頭valueにある ws 要素。
AllowTrailingWhite の末尾valueにある ws 要素。
AllowLeadingSign 数字の前の sign 要素。
AllowTrailingSign 数字の後の sign 要素。
AllowParentheses 数値を囲むかっこの形式の sign 要素。
AllowThousands グループ区切り記号 (,) 要素。
AllowCurrencySymbol currency ($) 要素。
Currency すべての要素。 ただし、 value 16 進数または数値を指数表記で表すことはできません。
Float の先頭または末尾の valuews 要素、の先頭value符号、および小数点 (.) 記号。 パラメーターでは value 、指数表記を使用することもできます。
Number wssign、group separator (,)、および decimal point (.) 要素。
Any すべての要素。 ただし、 value 16 進数を表すことはできません。

重要

メソッドをTryParse使用して、 メソッドによってToString出力された値の文字列表現をBigIntegerラウンドトリップする場合は、 メソッドを "R" 書式指定子と共に使用BigInteger.ToString(String)して、値の文字列表現を生成するBigInteger必要があります。 それ以外の場合、 の文字列表現では元の BigInteger 値の最上位 50 桁のみが保持され、 メソッドを使用 TryParse して値を復元 BigInteger するとデータが失われる可能性があります。

フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 value 16 進値にする必要があります。 に存在できるその他の style フラグは NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStylesHexNumber両方の空白フラグを含む複合スタイル があります)。

注意

が 16 進数の文字列表現である場合value、その前に 16 進数として区別する装飾 (や &hなど0x) を付けることはできません。 これにより、変換が失敗します。

が 16 進数の文字列の場合value、メソッドは、TryParse(String, NumberStyles, IFormatProvider, BigInteger)最初の 2 つの 16 進数が 以上0x80の場合、2 つの補数表現を使用して格納される負の数として解釈valueします。 つまり、 メソッドは、 の最初のバイト value の最上位ビットを符号ビットとして解釈します。 16 進文字列が正の数値として正しく解釈されるようにするには、 の value 最初の桁の値が 0 である必要があります。 たとえば、 メソッドは負の0x80値として解釈されますが、 または 0x00800x080正の値として解釈されます。 次の例は、負の値と正の値を表す 16 進数の文字列の違いを示しています。

using System;
using System.Globalization;
using System.Numerics;

public class Example
{
   public static void Main()
   {
      string[] hexStrings = { "80", "E293", "F9A2FF", "FFFFFFFF",
                              "080", "0E293", "0F9A2FF", "0FFFFFFFF",
                              "0080", "00E293", "00F9A2FF", "00FFFFFFFF" };
      BigInteger number = BigInteger.Zero;

      foreach (string hexString in hexStrings)
      {
         if (BigInteger.TryParse(hexString, NumberStyles.AllowHexSpecifier,
                                 null, out number))
            Console.WriteLine("Converted 0x{0} to {1}.", hexString, number);
         else
            Console.WriteLine("Cannot convert '{0}' to a BigInteger.", hexString);
      }
   }
}
// The example displays the following output:
//       Converted 0x80 to -128.
//       Converted 0xE293 to -7533.
//       Converted 0xF9A2FF to -417025.
//       Converted 0xFFFFFFFF to -1.
//       Converted 0x080 to 128.
//       Converted 0x0E293 to 58003.
//       Converted 0x0F9A2FF to 16360191.
//       Converted 0x0FFFFFFFF to 4294967295.
//       Converted 0x0080 to 128.
//       Converted 0x00E293 to 58003.
//       Converted 0x00F9A2FF to 16360191.
//       Converted 0x00FFFFFFFF to 4294967295.
Imports System.Globalization
Imports System.Numerics

Module Example
   Public Sub Main()
      Dim hexStrings() As String = { "80", "E293", "F9A2FF", "FFFFFFFF", 
                                     "080", "0E293", "0F9A2FF", "0FFFFFFFF",  
                                     "0080", "00E293", "00F9A2FF", "00FFFFFFFF" }
      Dim number As BigInteger = BigInteger.Zero
      
      For Each hexString As String In hexStrings
         If BigInteger.TryParse(hexString, NumberStyles.AllowHexSpecifier, 
                                Nothing, number) Then
            Console.WriteLine("Converted 0x{0} to {1}.", hexString, number)
         Else
            Console.WriteLine("Cannot convert '{0}' to a BigInteger.", hexString)
         End If
      Next         
   End Sub
End Module
' The example displays the following output:
'       Converted 0x80 to -128.
'       Converted 0xE293 to -7533.
'       Converted 0xF9A2FF to -417025.
'       Converted 0xFFFFFFFF to -1.
'       Converted 0x080 to 128.
'       Converted 0x0E293 to 58003.
'       Converted 0x0F9A2FF to 16360191.
'       Converted 0x0FFFFFFFF to 4294967295.
'       Converted 0x0080 to 128.
'       Converted 0x00E293 to 58003.
'       Converted 0x00F9A2FF to 16360191.
'       Converted 0x00FFFFFFFF to 4294967295.

パラメーターは provider 実装です IFormatProvider 。 そのメソッドは GetFormat 、 の形式に NumberFormatInfo 関するカルチャ固有の value情報を提供する オブジェクトを返します。 パラメーターには provider 、次のいずれかを指定できます。

  • CultureInfo書式設定情報を提供するカルチャを表す オブジェクト。 そのメソッドは GetFormat 、そのカルチャの NumberFormatInfo 数値書式情報を提供する オブジェクトを返します。

  • NumberFormatInfo数値書式情報を提供する オブジェクト。 (の実装 GetFormat は、それ自体を返すだけです)。

  • を実装 IFormatProviderするカスタム オブジェクト。 そのメソッドは GetFormat 、書式設定情報を提供する オブジェクトを NumberFormatInfo インスタンス化して返します。

nullの場合providerは、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。

こちらもご覧ください

適用対象