UInt32.TryParse メソッド

定義

数値の文字列表記を、それと等価の 32 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

オーバーロード

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt32)

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

TryParse(ReadOnlySpan<Char>, UInt32)

数値のスパン表現の、それと等価の 32 ビット符号なし整数への変換を試みます。 戻り値は変換が成功したか失敗したかを示します。

TryParse(String, UInt32)

数値の文字列表記を、それと等価の 32 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt32)

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

TryParse(String, IFormatProvider, UInt32)

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

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

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

TryParse(ReadOnlySpan<Byte>, UInt32)

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

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

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

TryParse(String, NumberStyles, IFormatProvider, UInt32)

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

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt32)

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
UInt32

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Char>, UInt32)

重要

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

数値のスパン表現の、それと等価の 32 ビット符号なし整数への変換を試みます。 戻り値は変換が成功したか失敗したかを示します。

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

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を格納しているスパン。

result
UInt32

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

戻り値

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

属性

適用対象

TryParse(String, UInt32)

重要

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

CLS 準拠の代替
System.Int64.TryParse(String, Int64)

数値の文字列表記を、それと等価の 32 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

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

パラメーター

s
String

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

result
UInt32

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

戻り値

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

属性

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

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

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

注釈

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

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

[ws][sign]digits[ws]

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

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

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

注意

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

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

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

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt32)

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

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

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

result
UInt32

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

戻り値

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

適用対象

TryParse(String, IFormatProvider, UInt32)

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

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

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

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

result
UInt32

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

戻り値

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

適用対象

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

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
UInt32

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Byte>, UInt32)

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

result
UInt32

このメソッドが戻るとき、 には、変換が成功した場合は に utf8Text 含まれる数値と等価の 32 ビット符号なし整数値が格納され、変換に失敗した場合は 0 が格納されます。 このパラメーターは、初期化されていない状態で渡されます。result にもともと入っていた値は上書きされます。

戻り値

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

適用対象

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

重要

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

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

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

パラメーター

s
ReadOnlySpan<Char>

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

style
NumberStyles

s で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。

provider
IFormatProvider

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

result
UInt32

変換が成功した場合、このメソッドが返されるときに、s に格納された数値と等しい 32 ビット符号なし整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーター が null または の形式styleでない場合、または UInt32.MinValue より小さい数値または UInt32.MaxValue より大きい数値を表す場合s、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

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

属性

適用対象

TryParse(String, NumberStyles, IFormatProvider, UInt32)

重要

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

CLS 準拠の代替
System.Int64.TryParse(String, Int64)

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

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

パラメーター

s
String

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

style
NumberStyles

s で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。

provider
IFormatProvider

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

result
UInt32

変換が成功した場合、このメソッドが返されるときに、s に格納された数値と等しい 32 ビット符号なし整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーター が null または の形式styleでない場合、または UInt32.MinValue より小さい数値または UInt32.MaxValue より大きい数値を表す場合s、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

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

属性

例外

styleNumberStyles 値ではありません。

- または -

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

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

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)
   {
      uint number;
      bool result = UInt32.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 UInt32.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 UInteger
      Dim result As Boolean = UInt32.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, UInt32) メソッドに Parse(String, NumberStyles, IFormatProvider) 似ていますが、変換が失敗しても例外がスローされないことを除きます。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをsテストFormatExceptionするために例外処理を使用する必要がなくなります。

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

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

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

[ws]hexdigits[ws]

次の表は、それぞれの要素の説明です。

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

注意

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

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

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

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

Note

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

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

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

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

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

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

こちらもご覧ください

適用対象