UInt64.TryParse メソッド

定義

数値の文字列形式を等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

オーバーロード

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt64)

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

TryParse(ReadOnlySpan<Char>, UInt64)

数値のスパン表現を等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(String, UInt64)

数値の文字列形式を等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt64)

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

TryParse(String, IFormatProvider, UInt64)

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

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt64)

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

TryParse(ReadOnlySpan<Byte>, UInt64)

数値の文字列形式を含む UTF-8 文字スパンを、等価の 64 ビット符号なし整数に変換しようとします。

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

指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(String, NumberStyles, IFormatProvider, UInt64)

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs

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

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = IUtf8SpanParsable<System::UInt64>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out ulong result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As ULong) As Boolean

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

provider
IFormatProvider

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

result
UInt64

戻り値には、utf8Text が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

戻り値

utf8Text が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

TryParse(ReadOnlySpan<Char>, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs
ソース:
UInt64.cs

重要

この API は CLS 準拠ではありません。

数値のスパン表現を等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::UInt64 % result);
public static bool TryParse (ReadOnlySpan<char> s, out ulong result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, out ulong result);
static member TryParse : ReadOnlySpan<char> * uint64 -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * uint64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As ULong) As Boolean

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を含むスパン。

result
UInt64

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する 64 ビット符号なし整数値が格納されます。変換に失敗した場合は 0 が格納されます。 パラメーターが または の場合、または正しい形式ではない場合、または UInt64.MinValue より小さいか、UInt64.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

属性

適用対象

TryParse(String, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs
ソース:
UInt64.cs

重要

この API は CLS 準拠ではありません。

CLS 準拠の代替
System.Decimal.Parse(String)

数値の文字列形式を等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::UInt64 % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, out ulong result);
public static bool TryParse (string? s, out ulong result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, out ulong result);
[<System.CLSCompliant(false)>]
static member TryParse : string * uint64 -> bool
static member TryParse : string * uint64 -> bool
Public Shared Function TryParse (s As String, ByRef result As ULong) As Boolean

パラメーター

s
String

変換する数値を表す文字列。

result
UInt64

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する 64 ビット符号なし整数値が格納されます。変換に失敗した場合は 0 が格納されます。 パラメーターが または の場合、または正しい形式ではない場合、または UInt64.MinValue より小さいか、UInt64.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

属性

次の例では、文字列配列内の各要素に対して TryParse(String, UInt64) メソッドを 1 回呼び出します。

string[] numericStrings = { "1293.8", "+1671.7", "28347.", 
                            "   33113684  ", "(0)", "-0", "+1293617", 
                            "18-", "119870", "31,024", "  3127094 ",  
                            "00700000" };
ulong number;
foreach (string numericString in numericStrings)
{
   if (UInt64.TryParse(numericString, out number)) 
      Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
   else
      Console.WriteLine("Cannot convert '{0}' to a UInt64.", numericString);
}
// The example displays the following output:
//       Cannot convert '1293.8' to a UInt64.
//       Cannot convert '+1671.7' to a UInt64.
//       Cannot convert '28347.' to a UInt64.
//       Converted '   33113684  ' to 33113684.
//       Cannot convert '(0)' to a UInt64.
//       Converted '-0' to 0.
//       Converted '+1293617' to 1293617.
//       Cannot convert '18-' to a UInt64.
//       Converted '119870' to 119870.
//       Cannot convert '31,024' to a UInt64.
//       Converted '  3127094 ' to 3127094.
//       Converted '0070000' to 70000.
open System

let numericStrings = 
    [| "1293.8"; "+1671.7"; "28347."
       "   33113684  "; "(0)"; "-0"; "+1293617" 
       "18-"; "119870"; "31,024"; "  3127094 "  
       "00700000" |]
for numericString in numericStrings do
    match UInt64.TryParse numericString with
    | true, number ->
        printfn $"Converted '{numericString}' to {number}."
    | _ ->
        printfn $"Cannot convert '{numericString}' to a UInt64."
// The example displays the following output:
//       Cannot convert '1293.8' to a UInt64.
//       Cannot convert '+1671.7' to a UInt64.
//       Cannot convert '28347.' to a UInt64.
//       Converted '   33113684  ' to 33113684.
//       Cannot convert '(0)' to a UInt64.
//       Converted '-0' to 0.
//       Converted '+1293617' to 1293617.
//       Cannot convert '18-' to a UInt64.
//       Converted '119870' to 119870.
//       Cannot convert '31,024' to a UInt64.
//       Converted '  3127094 ' to 3127094.
//       Converted '0070000' to 70000.
Dim numericStrings() As String = {"1293.8", "+1671.7", "28347.", _
                                  "   33113684  ", "(0)", "-0", "+1293617", _
                                  "18-", "119870", "31,024", "  3127094 ", _ 
                                  "0070000" }
Dim number As ULong
For Each numericString As String In numericStrings
   If UInt64.TryParse(numericString, number) Then
      Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
   Else
      Console.WriteLine("Cannot convert '{0}' to a UInt64.", numericString)
   End If      
Next
' The example displays the following output:
'       Cannot convert '1293.8' to a UInt64.
'       Cannot convert '+1671.7' to a UInt64.
'       Cannot convert '28347.' to a UInt64.
'       Converted '   33113684  ' to 33113684.
'       Cannot convert '(0)' to a UInt64.
'       Converted '-0' to 0.
'       Converted '+1293617' to 1293617.
'       Cannot convert '18-' to a UInt64.
'       Converted '119870' to 119870.
'       Cannot convert '31,024' to a UInt64.
'       Converted '  3127094 ' to 3127094.
'       Converted '0070000' to 70000.

注釈

TryParse(String, UInt64) メソッドは、変換が失敗した場合に例外をスローしない点を除き、Parse(String) メソッドに似ています。 このメソッドを使用すると、s が無効で正常に解析できない場合に、例外処理を使用して FormatException をテストする必要がなくなります。

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

[ ws][符号][ws]

角かっこ ([ と ]) の要素は省略可能です。 次の表では、各要素について説明します。

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

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

手記

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

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

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

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs
ソース:
UInt64.cs

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

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

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

result
UInt64

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

戻り値

s が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

TryParse(String, IFormatProvider, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs
ソース:
UInt64.cs

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

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

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

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

result
UInt64

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

戻り値

s が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs

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

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = System::Numerics::INumberBase<System::UInt64>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out ulong result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As ULong) As Boolean

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

style
NumberStyles

utf8Textに存在できる数値スタイルのビットごとの組み合わせ。

provider
IFormatProvider

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

result
UInt64

戻り値には、utf8Text が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

戻り値

utf8Text が正常に解析されたかどうかを true します。それ以外の場合は、falseします。

適用対象

TryParse(ReadOnlySpan<Byte>, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs

数値の文字列形式を含む UTF-8 文字スパンを、等価の 64 ビット符号なし整数に変換しようとします。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::UInt64 % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out ulong result);
static member TryParse : ReadOnlySpan<byte> * uint64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As ULong) As Boolean

パラメーター

utf8Text
ReadOnlySpan<Byte>

変換する数値を表す UTF-8 文字を含むスパン。

result
UInt64

このメソッドから制御が戻るときに、変換に成功した場合は utf8Text に含まれる数値に相当する 64 ビット符号なし整数値を格納し、変換に失敗した場合は 0 を格納します。 このパラメーターは初期化されていない状態で渡されます。結果で最初に指定された値は上書きされます。

戻り値

utf8Text が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

適用対象

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

ソース:
UInt64.cs
ソース:
UInt64.cs
ソース:
UInt64.cs

重要

この API は CLS 準拠ではありません。

指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を含むスパン。 スパンは、style パラメーターで指定されたスタイルを使用して解釈されます。

style
NumberStyles

sの許可された形式を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は Integerです。

provider
IFormatProvider

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

result
UInt64

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する 64 ビット符号なし整数値を格納し、変換に失敗した場合は 0 を格納します。 パラメーターが または に準拠している形式ではない場合、または UInt64.MinValue より小さいか、UInt64.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

属性

適用対象

TryParse(String, NumberStyles, IFormatProvider, UInt64)

ソース:
UInt64.cs
ソース:
UInt64.cs
ソース:
UInt64.cs

重要

この API は CLS 準拠ではありません。

CLS 準拠の代替
System.Decimal.Parse(String)

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の 64 ビット符号なし整数に変換しようとします。 戻り値は、変換が成功したか失敗したかを示します。

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

パラメーター

s
String

変換する数値を表す文字列。 文字列は、style パラメーターで指定されたスタイルを使用して解釈されます。

style
NumberStyles

sの許可された形式を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は Integerです。

provider
IFormatProvider

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

result
UInt64

このメソッドから制御が戻るときに、変換に成功した場合は sに含まれる数値に相当する 64 ビット符号なし整数値を格納し、変換に失敗した場合は 0 を格納します。 パラメーターが または に準拠している形式ではない場合、または UInt64.MinValue より小さいか、UInt64.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

s が正常に変換されたかどうかを true します。それ以外の場合は、falseします。

属性

例外

styleNumberStyles 値ではありません。

-又は-

style は、AllowHexSpecifier 値と HexNumber 値の組み合わせではありません。

次の例では、さまざまな文字列と NumberStyles 値を使用して、TryParse(String, NumberStyles, IFormatProvider, UInt64) メソッドを呼び出します。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;
      
      numericString = "2106034";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);
      
      numericString = "-10603";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);
      
      numericString = "29103674.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);
      
      numericString = "10345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "41792210E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 
      
      numericString = "9112E-01";
      CallTryParse(numericString, styles);
          
      numericString = "312E01";
      CallTryParse(numericString, styles); 
      
      numericString = "FFC86DA1";
      CallTryParse(numericString, NumberStyles.HexNumber);
      
      numericString = "0x8F8C";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }
   
   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      ulong number;
      bool result = UInt64.TryParse(stringToConvert, styles, 
                                   CultureInfo.InvariantCulture, out number);
      if (result)
         Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
      else
         Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
   }
}
// The example displays the following output:
//       Converted '2106034' to 2106034.
//       Attempted conversion of '-10603' failed.
//       Converted '29103674.00' to 29103674.
//       Attempted conversion of '10345.72' failed.
//       Converted '41792210E-01' to 4179221.
//       Attempted conversion of '9112E-01' failed.
//       Converted '312E01' to 3120.
//       Converted 'FFC86DA1' to 4291325345.
//       Attempted conversion of '0x8F8C' failed.
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
    match UInt64.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
    | true, number ->
        printfn $"Converted '{stringToConvert}' to {number}."
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."

do
    let numericString = "2106034"
    let styles = NumberStyles.Integer
    callTryParse numericString styles
    
    let numericString = "-10603"
    let styles = NumberStyles.None
    callTryParse numericString styles
    
    let numericString = "29103674.00"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse numericString styles
    
    let numericString = "10345.72"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse numericString styles

    let numericString = "41792210E-01"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse numericString styles
    
    let numericString = "9112E-01"
    callTryParse numericString styles
        
    let numericString = "312E01"
    callTryParse numericString styles
    
    let numericString = "FFC86DA1"
    callTryParse numericString NumberStyles.HexNumber
    
    let numericString = "0x8F8C"
    callTryParse numericString NumberStyles.HexNumber
// The example displays the following output:
//       Converted '2106034' to 2106034.
//       Attempted conversion of '-10603' failed.
//       Converted '29103674.00' to 29103674.
//       Attempted conversion of '10345.72' failed.
//       Converted '41792210E-01' to 4179221.
//       Attempted conversion of '9112E-01' failed.
//       Converted '312E01' to 3120.
//       Converted 'FFC86DA1' to 4291325345.
//       Attempted conversion of '0x8F8C' failed.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "2106034"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-10603"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)
      
      numericString = "29103674.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      
      numericString = "10345.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "41792210E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
      
      numericString = "9112E-01"
      CallTryParse(numericString, styles)
          
      numericString = "312E01"
      CallTryParse(numericString, styles) 
      
      numericString = "FFC86DA1"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x8F8C"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As ULong
      Dim result As Boolean = UInt64.TryParse(stringToConvert, styles, _
                                              CultureInfo.InvariantCulture, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           Convert.ToString(stringToConvert))
      End If                                                                           
   End Sub
End Module
' The example displays the following output to the console:
'       Converted '2106034' to 2106034.
'       Attempted conversion of '-10603' failed.
'       Converted '29103674.00' to 29103674.
'       Attempted conversion of '10345.72' failed.
'       Converted '41792210E-01' to 4179221.
'       Attempted conversion of '9112E-01' failed.
'       Converted '312E01' to 3120.
'       Converted 'FFC86DA1' to 4291325345.
'       Attempted conversion of '0x8F8C' failed.

注釈

TryParse(String, NumberStyles, IFormatProvider, UInt64) メソッドは、変換が失敗した場合に例外をスローしない点を除き、Parse(String, NumberStyles, IFormatProvider) メソッドに似ています。 このメソッドでは、例外処理を使用して、s が無効であり、正常に解析できない場合に FormatException をテストする必要がなくなります。

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

[ ws][][sign][,][fractional_digits][E[符号]exponential_digits][ws]

角かっこ ([ と ]) の項目は省略可能です。 または、style パラメーターに NumberStyles.AllowHexSpecifierが含まれている場合は、s パラメーターに次の要素を含めることができます。

[ws]hexdigits[ws]

次の表では、各要素について説明します。

要素 形容
ws の 省略可能な空白。 空白は、s の開始時 styleNumberStyles.AllowLeadingWhite フラグが含まれている場合は表示され、NumberStyles.AllowTrailingWhite フラグ style 含まれている場合は s の最後に表示されます。
$ カルチャ固有の通貨記号。 文字列内での位置は、provider パラメーターの GetFormat メソッドによって返される NumberFormatInfo オブジェクトの CurrencyPositivePattern プロパティによって定義されます。 styleNumberStyles.AllowCurrencySymbol フラグが含まれている場合、通貨記号は s に表示されます。
sign 省略可能な記号。 styleNumberStyles.AllowLeadingSign フラグが含まれている場合は s の開始時に表示され、styleNumberStyles.AllowTrailingSign フラグが含まれている場合は s の末尾に表示されます。 s でかっこを使用すると、styleNumberStyles.AllowParentheses フラグが含まれている場合に負の値を示すことができます。 ただし、負の符号が存在する場合、s は解析操作が成功するための値 0 のみを表すことができます。
桁の 0 から 9 までの数字のシーケンス。
カルチャ固有のグループ区切り記号。 styleNumberStyles.AllowThousands フラグが含まれている場合、provider で指定されたカルチャのグループ区切り記号を s に表示できます。
. カルチャ固有の小数点記号。 provider で指定されたカルチャの小数点記号は、NumberStyles.AllowDecimalPoint フラグ style 含まれている場合 s に表示できます。
fractional_digits 数字 0 の 1 つ以上の出現。 小数部の数字は、NumberStyles.AllowDecimalPoint フラグ style 含まれている場合にのみ、s に表示できます。
E "e" または "E" 文字。値が指数 (指数) 表記で表されることを示します。 s パラメーターは、NumberStyles.AllowExponent フラグが含まれている場合 style 指数表記で数値を表すことができます。
exponential_digits 0 から 9 までの数字のシーケンス。 s パラメーターは、NumberStyles.AllowExponent フラグが含まれている場合 style 指数表記で数値を表すことができます。
hexdigits を する 0 から f、または 0 から F までの 16 進数のシーケンス。

手記

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

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

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

NumberStyles.AllowHexSpecifier フラグを使用する場合、s は 16 進値である必要があります。 有効な 16 進文字は、0 から 9、A から F、および a から f です。 "0x" などのプレフィックスはサポートされていないため、解析操作が失敗します。 style に存在できる他のフラグは、NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteだけです。 (NumberStyles 列挙型には、両方の空白フラグを含む複合スタイル (HexNumber) があります。

手記

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

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

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

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

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

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

こちらもご覧ください

適用対象