UInt16.TryParse メソッド

定義

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

オーバーロード

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt16)

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

TryParse(ReadOnlySpan<Char>, UInt16)

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

TryParse(String, UInt16)

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

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt16)

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

TryParse(String, IFormatProvider, UInt16)

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

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

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

TryParse(ReadOnlySpan<Byte>, UInt16)

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

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

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

TryParse(String, NumberStyles, IFormatProvider, UInt16)

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

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt16)

Source:
UInt16.cs
Source:
UInt16.cs

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
UInt16

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Char>, UInt16)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

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

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

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

パラメーター

s
ReadOnlySpan<Char>

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

result
UInt16

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

戻り値

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

属性

適用対象

TryParse(String, UInt16)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

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

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

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

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

パラメーター

s
String

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

result
UInt16

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

戻り値

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

属性

次の例では、文字列配列内の TryParse(String, UInt16) 各要素に対して メソッドを 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(String, UInt16) メソッドに Parse(String) 似ていますが、変換が失敗しても例外はスローされません。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをsテストFormatExceptionするために例外処理を使用する必要がなくなります。

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

[ws][sign]digits[ws]

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

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

注意

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

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

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

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

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt16)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

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

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

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

result
UInt16

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

戻り値

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

適用対象

TryParse(String, IFormatProvider, UInt16)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

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

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

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

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

result
UInt16

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

戻り値

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

適用対象

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

Source:
UInt16.cs
Source:
UInt16.cs

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
UInt16

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Byte>, UInt16)

Source:
UInt16.cs
Source:
UInt16.cs

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

result
UInt16

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

戻り値

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

適用対象

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

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

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

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

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

パラメーター

s
ReadOnlySpan<Char>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
UInt16

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

戻り値

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

属性

適用対象

TryParse(String, NumberStyles, IFormatProvider, UInt16)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

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

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

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

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

パラメーター

s
String

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

style
NumberStyles

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

provider
IFormatProvider

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

result
UInt16

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

戻り値

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

属性

例外

styleNumberStyles 値ではありません。

- または -

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

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

using System;
using System.Globalization;

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

      numericString = "2210E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 
      
      numericString = "9112E-01";
      CallTryParse(numericString, styles);
          
      numericString = "312E01";
      CallTryParse(numericString, styles); 
      
      numericString = "FFC8";
      CallTryParse(numericString, NumberStyles.HexNumber);
      
      numericString = "0x8F8C";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }
   
   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      ushort number;
      bool result = UInt16.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 '10603' to 10603.
//       Attempted conversion of '-10603' failed.
//       Converted '29103.00' to 29103.
//       Attempted conversion of '10345.72' failed.
//       Converted '2210E-01' to 221.
//       Attempted conversion of '9112E-01' failed.
//       Converted '312E01' to 3120.
//       Converted 'FFC8' to 65480.
//       Attempted conversion of '0x8F8C' failed.
open System
open System.Globalization

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

do
    let numericString = "10603"
    let styles = NumberStyles.Integer
    callTryParse numericString styles
    
    let numericString = "-10603"
    let styles = NumberStyles.None
    callTryParse numericString styles
    
    let numericString = "29103.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 = "2210E-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 = "FFC8"
    callTryParse numericString NumberStyles.HexNumber
    
    let numericString = "0x8F8C"
    callTryParse numericString NumberStyles.HexNumber
// The example displays the following output:
//       Converted '10603' to 10603.
//       Attempted conversion of '-10603' failed.
//       Converted '29103.00' to 29103.
//       Attempted conversion of '10345.72' failed.
//       Converted '2210E-01' to 221.
//       Attempted conversion of '9112E-01' failed.
//       Converted '312E01' to 3120.
//       Converted 'FFC8' to 65480.
//       Attempted conversion of '0x8F8C' failed.
Imports System.Globalization

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

      numericString = "2210E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
      
      numericString = "9112E-01"
      CallTryParse(numericString, styles)
          
      numericString = "312E01"
      CallTryParse(numericString, styles) 
      
      numericString = "FFC8"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x8F8C"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As UShort
      Dim result As Boolean = UInt16.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 '10603' to 10603.
'       Attempted conversion of '-10603' failed.
'       Converted '29103.00' to 29103.
'       Attempted conversion of '10345.72' failed.
'       Converted '2210E-01' to 221.
'       Attempted conversion of '9112E-01' failed.
'       Converted '312E01' to 3120.
'       Converted 'FFC8' to 65480.
'       Attempted conversion of '0x8F8C' failed.

注釈

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

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

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

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

[ws]hexdigits[ws]

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

Ws オプションの空白。 空白は、フラグが含まれている場合は のs先頭に表示され、フラグが含NumberStyles.AllowLeadingWhiteまれている場合styleは のs末尾にNumberStyles.AllowTrailingWhite表示styleされます。

$ カルチャ固有の通貨記号。 文字列内の位置は、 パラメーターの CurrencyPositivePatternNumberFormatInfo メソッドによって返される オブジェクトの provider プロパティによってGetFormat定義されます。 フラグが含まれている場合styleは、通貨記号を にsNumberStyles.AllowCurrencySymbol表示できます。

署名 省略可能な記号。 この記号は、 フラグを含む場合styleは のs先頭に表示され、フラグが含まれている場合styleは のs末尾にNumberStyles.AllowTrailingSign表示NumberStyles.AllowLeadingSignできます。 に フラグが含まれている場合style、かっこを使用sして負の値をNumberStyles.AllowParentheses示すことができます。 ただし、負符号が存在する場合、 s は解析操作が成功するための値 0 のみを表すことができます。

0 ~ 9 の数字のシーケンス。

, カルチャ固有のグループ区切り記号。 で指定されたproviderカルチャのグループ区切り記号は、 フラグが含まれている場合styleに にsNumberStyles.AllowThousands表示できます。

. カルチャ固有の小数点記号。 で指定されたproviderカルチャの小数点記号は、 フラグが含まれている場合styleに にsNumberStyles.AllowDecimalPoint表示できます。

fractional_digits 数字 0 の 1 つ以上の出現。 小数部の数字は、 フラグが s 含まれている場合 style にのみ に NumberStyles.AllowDecimalPoint 表示できます。

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 メンバーのほとんどは、この入力文字列に存在する可能性がありますが、存在する必要がない要素を制御します。 次の表は、 にs存在する可能性がある要素に個々NumberStylesのメンバーがどのように影響するかを示しています。

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

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

Note

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

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

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

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

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

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

こちらもご覧ください

適用対象