Byte.TryParse メソッド

定義

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

オーバーロード

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

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

TryParse(ReadOnlySpan<Char>, Byte)

数値のスパン表現の、等価の Byte への変換を試み、変換に成功したかどうかを示す値を返します。

TryParse(String, Byte)

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

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

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

TryParse(String, IFormatProvider, Byte)

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

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

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

TryParse(ReadOnlySpan<Byte>, Byte)

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

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

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

TryParse(String, NumberStyles, IFormatProvider, Byte)

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

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
Byte

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Char>, Byte)

数値のスパン表現の、等価の Byte への変換を試み、変換に成功したかどうかを示す値を返します。

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

パラメーター

s
ReadOnlySpan<Char>

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

result
Byte

このメソッドから制御が返されたとき、変換に成功した場合は、Byte に含まれる数値と等価な s 値が格納されます。変換に失敗した場合は 0 が格納されます。 このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

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

適用対象

TryParse(String, Byte)

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

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

パラメーター

s
String

変換する数値を含んだ文字列。

result
Byte

このメソッドから制御が返されたとき、変換に成功した場合は、Byte に含まれる数値と等価な s 値が格納されます。変換に失敗した場合は 0 が格納されます。 このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

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

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

using namespace System;

void main()
{
   array<String^>^ byteStrings = gcnew array<String^> { nullptr, String::Empty, 
                                                        "1024", "100.1", "100", 
                                                        "+100", "-100", "000000000000000100", 
                                                        "00,100", "   20   ", "FF", "0x1F" };
   Byte byteValue;
   for each (String^ byteString in byteStrings) {
      bool result = Byte::TryParse(byteString, byteValue);
      if (result)
         Console::WriteLine("Converted '{0}' to {1}", 
                            byteString, byteValue);
      else
         Console::WriteLine("Attempted conversion of '{0}' failed.", 
                            byteString);
   }
}
// The example displays the following output:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.`
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.}
using System;

public class ByteConversion
{
   public static void Main()
   {
      string[] byteStrings = { null, string.Empty, "1024",
                               "100.1", "100", "+100", "-100",
                               "000000000000000100", "00,100",
                               "   20   ", "FF", "0x1F" };

      foreach (var byteString in byteStrings)
      {
          CallTryParse(byteString);
      }
   }

   private static void CallTryParse(string stringToConvert)
   {
      byte byteValue;
      bool success = Byte.TryParse(stringToConvert, out byteValue);
      if (success)
      {
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      }
      else
      {
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert);
      }
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
open System

let callTryParse (stringToConvert: string) =
    match Byte.TryParse stringToConvert with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."

let byteStrings = 
    [ null; String.Empty; "1024"
      "100.1"; "100"; "+100"; "-100"
      "000000000000000100"; "00,100"
      "   20   "; "FF"; "0x1F" ]

for byteString in byteStrings do
    callTryParse byteString

// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
Module ByteConversion
   Public Sub Main()
      Dim byteStrings() As String = { Nothing, String.Empty, "1024", 
                                    "100.1", "100", "+100", "-100",
                                    "000000000000000100", "00,100",
                                    "   20   ", "FF", "0x1F"}

      For Each byteString As String In byteStrings
        CallTryParse(byteString)
      Next
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String)  
      Dim byteValue As Byte
      Dim success As Boolean = Byte.TryParse(stringToConvert, byteValue)
      If success Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert)
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '' failed.
'       Attempted conversion of '' failed.
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Attempted conversion of '00,100' failed.
'       Converted '   20   ' to 20
'       Attempted conversion of 'FF' failed.
'       Attempted conversion of '0x1F' failed.

注釈

変換は失敗し、パラメーターが 正しい形式でない場合s、または String.Emptyが の場合null、または より小さいMinValueかより大きいMaxValue数値を表す場合、メソッドは を返falseします。

メソッドは Byte.TryParse(String, Byte) メソッドに Byte.Parse(String) 似ていますが TryParse(String, Byte) 、変換が失敗しても例外はスローされません。

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

[ws][sign]digits[ws]

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

要素 説明
ws オプションの空白。
sign 現在のカルチャの プロパティで NumberFormatInfo.PositiveSign 指定された、省略可能な正符号。
数値 0 ~ 9 の範囲の 10 進数のシーケンス。

パラメーターは s 、 スタイルを使用して Integer 解釈されます。 バイト値の 10 進数に加えて、先頭と末尾のスペースと先頭記号のみを使用できます。 (符号が存在する場合は、正の符号である必要があります。または、メソッドによって が OverflowExceptionスローされます)。に存在できるカルチャ固有の書式設定情報と共にスタイル要素を明示的に s定義するには、 メソッドを使用します Byte.Parse(String, NumberStyles, IFormatProvider)

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

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

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

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

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

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

result
Byte

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

戻り値

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

適用対象

TryParse(String, IFormatProvider, Byte)

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

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

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

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

result
Byte

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

戻り値

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

適用対象

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

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Byte

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Byte>, Byte)

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

result
Byte

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

戻り値

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

適用対象

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

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

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

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を格納しているスパン。 スパンは、Integer 形式で解釈されます。

style
NumberStyles

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

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。 providernull の場合は、スレッドの現在のカルチャが使用されます。

result
Byte

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

戻り値

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

適用対象

TryParse(String, NumberStyles, IFormatProvider, Byte)

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

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

パラメーター

s
String

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

style
NumberStyles

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

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。 providernull の場合は、スレッドの現在のカルチャが使用されます。

result
Byte

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

戻り値

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

例外

styleNumberStyles 値ではありません。

- または -

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

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

using namespace System;
using namespace System::Globalization;

void CallTryParse(String^ byteString, NumberStyles styles);

void main()
{
   String^ byteString; 
   NumberStyles styles;

   byteString = "1024";
   styles = NumberStyles::Integer;
   CallTryParse(byteString, styles);

   byteString = "100.1";
   styles = NumberStyles::Integer | NumberStyles::AllowDecimalPoint;
   CallTryParse(byteString, styles);

   byteString = "100.0";
   CallTryParse(byteString, styles);

   byteString = "+100";
   styles = NumberStyles::Integer | NumberStyles::AllowLeadingSign 
            | NumberStyles::AllowTrailingSign;
   CallTryParse(byteString, styles);

   byteString = "-100";
   CallTryParse(byteString, styles);

   byteString = "000000000000000100";
   CallTryParse(byteString, styles);

   byteString = "00,100";
   styles = NumberStyles::Integer | NumberStyles::AllowThousands;
   CallTryParse(byteString, styles);

   byteString = "2E+3   ";
   styles = NumberStyles::Integer | NumberStyles::AllowExponent;
   CallTryParse(byteString, styles);

   byteString = "FF";
   styles = NumberStyles::HexNumber;
   CallTryParse(byteString, styles);

   byteString = "0x1F";
   CallTryParse(byteString, styles);
}

void CallTryParse(String^ stringToConvert, NumberStyles styles)
{  
   Byte byteValue;
   bool result = Byte::TryParse(stringToConvert, styles, 
                                 (IFormatProvider^) nullptr , byteValue);
   if (result)
      Console::WriteLine("Converted '{0}' to {1}", 
                     stringToConvert, byteValue);
   else
      Console::WriteLine("Attempted conversion of '{0}' failed.", 
                        stringToConvert);
}
// The example displays the following output:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.}
using System;
using System.Globalization;

public class ByteConversion2
{
   public static void Main()
   {
      string byteString;
      NumberStyles styles;

      byteString = "1024";
      styles = NumberStyles.Integer;
      CallTryParse(byteString, styles);

      byteString = "100.1";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(byteString, styles);

      byteString = "100.0";
      CallTryParse(byteString, styles);

      byteString = "+100";
      styles = NumberStyles.Integer | NumberStyles.AllowLeadingSign
               | NumberStyles.AllowTrailingSign;
      CallTryParse(byteString, styles);

      byteString = "-100";
      CallTryParse(byteString, styles);

      byteString = "000000000000000100";
      CallTryParse(byteString, styles);

      byteString = "00,100";
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(byteString, styles);

      byteString = "2E+3   ";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(byteString, styles);

      byteString = "FF";
      styles = NumberStyles.HexNumber;
      CallTryParse(byteString, styles);

      byteString = "0x1F";
      CallTryParse(byteString, styles);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      Byte byteValue;
      bool result = Byte.TryParse(stringToConvert, styles,
                                  null as IFormatProvider, out byteValue);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert.ToString());
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
    match Byte.TryParse(stringToConvert, styles, null) with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."
                        
[<EntryPoint>]
let main _ =
    let byteString = "1024"
    let styles = NumberStyles.Integer
    callTryParse byteString styles

    let byteString = "100.1"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse byteString styles

    let byteString = "100.0"
    callTryParse byteString styles

    let byteString = "+100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign
    callTryParse byteString styles

    let byteString = "-100"
    callTryParse byteString styles

    let byteString = "000000000000000100"
    callTryParse byteString styles

    let byteString = "00,100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
    callTryParse byteString styles

    let byteString = "2E+3   "
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse byteString styles

    let byteString = "FF"
    let styles = NumberStyles.HexNumber
    callTryParse byteString styles

    let byteString = "0x1F"
    callTryParse byteString styles

    0

// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
Imports System.Globalization

Module ByteConversion2
   Public Sub Main()
      Dim byteString As String 
      Dim styles As NumberStyles
      
      byteString = "1024"
      styles = NumberStyles.Integer
      CallTryParse(byteString, styles)
      
      byteString = "100.1"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(byteString, styles)
      
      byteString = "100.0"
      CallTryParse(byteString, styles)
      
      byteString = "+100"
      styles = NumberStyles.Integer Or NumberStyles.AllowLeadingSign _
               Or NumberStyles.AllowTrailingSign
      CallTryParse(byteString, styles)
      
      byteString = "-100"
      CallTryParse(byteString, styles)
      
      byteString = "000000000000000100"
      CallTryParse(byteString, styles)
      
      byteString = "00,100"      
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(byteString, styles)
      
      byteString = "2E+3   "
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(byteString, styles)
      
      byteString = "FF"
      styles = NumberStyles.HexNumber
      CallTryParse(byteString, styles)
      
      byteString = "0x1F"
      CallTryParse(byteString, styles)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles As NumberStyles)  
      Dim byteValue As Byte
      Dim result As Boolean = Byte.TryParse(stringToConvert, styles, Nothing, _
                                            byteValue)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         If stringToConvert Is Nothing Then stringToConvert = ""
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert.ToString())
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100.0' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Converted '00,100' to 100
'       Attempted conversion of '2E+3   ' failed.
'       Converted 'FF' to 255
'       Attempted conversion of '0x1F' failed.

注釈

メソッドは TryParse メソッドに Parse 似ていますが、変換が失敗した場合、 TryParse メソッドは例外をスローしません。

パラメーターはs、 パラメーターによってprovider提供されるオブジェクトの書式設定情報をNumberFormatInfo使用して解析されます。

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

[ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

または、 パラメーターに が style 含まれている場合は AllowHexSpecifier

[ws]hexdigits[ws]

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

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

注意

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

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

非複合 NumberStyles 値 数字に加えて、 で許可される要素
NumberStyles.None 10 進数のみ。
NumberStyles.AllowDecimalPoint 要素fractional_digits 要素。 ただし、 fractional_digits は 1 桁以上の数字のみで構成する必要があります。または、 メソッドは を返します false
NumberStyles.AllowExponent パラメーターでは s 指数表記を使用することもできます。 指数表記で数値を表す場合 s は、0 以外の小数部を含まないデータ型の範囲内の Byte 整数を表す必要があります。
NumberStyles.AllowLeadingWhite の先頭sにある ws 要素。
NumberStyles.AllowTrailingWhite の末尾sにある ws 要素。
NumberStyles.AllowLeadingSign 正符号は 数字の前に表示できます。
NumberStyles.AllowTrailingSign 正符号は 数字の後に表示できます。
NumberStyles.AllowParentheses このフラグはサポートされていますが、かっこが に存在する場合、 メソッドは をsfalseします。
NumberStyles.AllowThousands グループ区切り記号は に s表示できますが、先頭には 1 桁以上の 0 桁のみを指定できます。
NumberStyles.AllowCurrencySymbol $ 要素。

フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 s プレフィックスのない 16 進数の値にする必要があります。 たとえば、"F3" は正常に解析されますが、"0xF3" は解析されません。 に存在できるその他の style フラグは NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteだけです。 (列挙体 NumberStyles には、 NumberStyles.HexNumber両方の空白フラグを含む複合数値スタイル があります)。

パラメーターはproviderIFormatProvider、オブジェクトやオブジェクトなどのCultureInfo実装でありNumberFormatInfo、そのメソッドは GetFormat オブジェクトをNumberFormatInfo返します。 オブジェクトは NumberFormatInfo 、 の形式に関するカルチャ固有の s情報を提供します。

こちらもご覧ください

適用対象