Int16.TryParse メソッド

定義

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

オーバーロード

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16)

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

TryParse(ReadOnlySpan<Char>, Int16)

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

TryParse(String, Int16)

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

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16)

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

TryParse(String, IFormatProvider, Int16)

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

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

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

TryParse(ReadOnlySpan<Byte>, Int16)

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

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

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

TryParse(String, NumberStyles, IFormatProvider, Int16)

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

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16)

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
Int16

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Char>, Int16)

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

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

パラメーター

s
ReadOnlySpan<Char>

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

result
Int16

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

戻り値

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

適用対象

TryParse(String, Int16)

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

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

パラメーター

s
String

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

result
Int16

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

戻り値

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

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

using System;

public class StringParsing
{
   public static void Main()
   {
      TryToParse(null);
      TryToParse("16051");
      TryToParse("9432.0");
      TryToParse("16,667");
      TryToParse("   -322   ");
      TryToParse("+4302");
      TryToParse("(100);");
      TryToParse("01FA");
   }

   private static void TryToParse(string value)
   {
      short number;
      bool result = Int16.TryParse(value, out number);
      if (result)
      {
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      else
      {
         if (value == null) value = "";
         Console.WriteLine("Attempted conversion of '{0}' failed.", value);
      }
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Converted '16051' to 16051.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100)' failed.
//       Attempted conversion of '01FA' failed.
open System

let tryToParse (value: string) =
    match Int16.TryParse value with
    | true, number -> printfn "Converted '{value}' to {number}."
    | _ ->
        let value = if isNull value then "" else value
        printfn $"Attempted conversion of '{value}' failed."

tryToParse null
tryToParse "16051"
tryToParse "9432.0"
tryToParse "16,667"
tryToParse "   -322   "
tryToParse "+4302"
tryToParse "(100);"
tryToParse "01FA"

// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Converted '16051' to 16051.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100)' failed.
//       Attempted conversion of '01FA' failed.
Module StringParsing
   Public Sub Main()
      TryToParse(Nothing)
      TryToParse("16051")
      TryToParse("9432.0")
      TryToParse("16,667")
      TryToParse("   -322   ")
      TryToParse("+4302")
      TryToParse("(100)")
      TryToParse("01FA")
      
   End Sub
   
   Private Sub TryToParse(value As String)
      Dim number As Int16
      Dim result As Boolean = Int16.TryParse(value, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Else
         If value Is Nothing Then value = "" 
         Console.WriteLine("Attempted conversion of '{0}' failed.", value)
      End If     
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '' failed.
'       Converted '16051' to 16051.
'       Attempted conversion of '9432.0' failed.
'       Attempted conversion of '16,667' failed.
'       Converted '   -322   ' to -322.
'       Converted '+4302' to 4302.
'       Attempted conversion of '(100)' failed.
'       Attempted conversion of '01FA' failed.

この例では、メソッドが TryParse(String, Int16) 変換できない文字列の一部を次に示します。

  • "9432.0". 文字列に小数点区切り記号を含めることができないため、変換は失敗します。整数の数字のみを含む必要があります。

  • "16,667". 文字列にグループ区切り記号を含めることができないため、変換は失敗します。整数の数字のみを含む必要があります。

  • "(100)". 文字列に、現在のカルチャの および NumberFormatInfo.NumberNegativePattern プロパティで定義されている負の符号以外の符号を含めることができないため、変換はNumberFormatInfo.NegativeSign失敗します。

  • "01FA" 文字列に 16 進数を含めることができないため、変換は失敗します。10 進数のみを含む必要があります。

注釈

メソッドは Int16.TryParse(String, Int16) 、解析された値を Int16.Parse(String) 返す代わりに解析操作が成功したかどうかを示すブール値を Int16 返すことで、メソッドとは異なります。 無効であり、正常に解析できないイベントsで 例外処理を使用して をテストFormatExceptionする必要がなくなります。

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

[ws][sign]digits[ws]

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

要素 説明
ws オプションの空白。
sign 省略可能な記号。
数値 0 から 9 までの数字のシーケンス。

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

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

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

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16)

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

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

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

result
Int16

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

戻り値

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

適用対象

TryParse(String, IFormatProvider, Int16)

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

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

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

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

result
Int16

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

戻り値

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

適用対象

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

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Int16

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

戻り値

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

適用対象

TryParse(ReadOnlySpan<Byte>, Int16)

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

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

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

result
Int16

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

戻り値

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

適用対象

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

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

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

パラメーター

s
ReadOnlySpan<Char>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Int16

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

戻り値

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

適用対象

TryParse(String, NumberStyles, IFormatProvider, Int16)

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

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

パラメーター

s
String

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Int16

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

戻り値

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

例外

styleNumberStyles 値ではありません。

- または -

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

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

using System;
using System.Globalization;

public class StringParsing
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;

      numericString = "10677";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);

      numericString = "-30677";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);

      numericString = "10345.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "10345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "22,593";
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(numericString, styles);

      numericString = "12E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles);

      numericString = "12E03";
      CallTryParse(numericString, styles);

      numericString = "80c1";
      CallTryParse(numericString, NumberStyles.HexNumber);

      numericString = "0x80C1";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      short number;
      bool result = Int16.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 to the console:
//       Converted '10677' to 10677.
//       Attempted conversion of '-30677' failed.
//       Converted '10345.00' to 10345.
//       Attempted conversion of '10345.72' failed.
//       Converted '22,593' to 22593.
//       Attempted conversion of '12E-01' failed.
//       Converted '12E03' to 12000.
//       Converted '80c1' to -32575.
//       Attempted conversion of '0x80C1' failed.
open System
open System.Globalization

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

[<EntryPoint>]
let main _ =
    let numericString = "10677"
    let styles = NumberStyles.Integer
    callTryParse numericString styles

    let numericString = "-30677"
    let styles = NumberStyles.None
    callTryParse numericString styles

    let numericString = "10345.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 = "22,593"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
    callTryParse numericString styles

    let numericString = "12E-01"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse numericString styles

    let numericString = "12E03"
    callTryParse numericString styles

    let numericString = "80c1"
    callTryParse numericString NumberStyles.HexNumber

    let numericString = "0x80C1"
    callTryParse numericString NumberStyles.HexNumber

    0


// The example displays the following output to the console:
//       Converted '10677' to 10677.
//       Attempted conversion of '-30677' failed.
//       Converted '10345.00' to 10345.
//       Attempted conversion of '10345.72' failed.
//       Converted '22,593' to 22593.
//       Attempted conversion of '12E-01' failed.
//       Converted '12E03' to 12000.
//       Converted '80c1' to -32575.
//       Attempted conversion of '0x80C1' failed.
Imports System.Globalization

Module StringParsing
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "10677"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-30677"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)
      
      numericString = "10345.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      
      numericString = "10345.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "22,593" 
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(numericString, styles)
      
      numericString = "12E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
          
      numericString = "12E03"
      CallTryParse(numericString, styles) 
      
      numericString = "80c1"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x80C1"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As Short
      Dim result As Boolean = Int16.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 '10677' to 10677.
'       Attempted conversion of '-30677' failed.
'       Converted '10345.00' to 10345.
'       Attempted conversion of '10345.72' failed.
'       Converted '22,593' to 22593.
'       Attempted conversion of '12E-01' failed.
'       Converted '12E03' to 12000.
'       Converted '80c1' to -32575.
'       Attempted conversion of '0x80C1' failed.

注釈

メソッドは Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 、解析された値を Int16.Parse(String, NumberStyles, IFormatProvider) 返す代わりに解析操作が成功したかどうかを示すブール値を Int16 返すことで、メソッドとは異なります。 無効であり、正常に解析できないイベントsで 例外処理を使用して をテストFormatExceptionする必要がなくなります。

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

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

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

[ws]hexdigits[ws]

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

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

注意

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

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

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

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

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

こちらもご覧ください

適用対象