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)

ソース:
Int16.cs
ソース:
Int16.cs

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 が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

戻り値

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

適用対象

TryParse(ReadOnlySpan<Char>, Int16)

ソース:
Int16.cs
ソース:
Int16.cs
ソース:
Int16.cs

カルチャ固有の形式の数値のスパン表現を、等価の 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 を格納します。 s パラメーターが null または Empty の場合、または Int16.MinValue より小さいか、Int16.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

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

適用対象

TryParse(String, Int16)

ソース:
Int16.cs
ソース:
Int16.cs
ソース:
Int16.cs

数値の文字列形式を等価の 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 を格納します。 パラメーターが または の場合、正しい形式ではない場合、または Int16.MinValue より 小さいか、Int16.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。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.NegativeSign および NumberFormatInfo.NumberNegativePattern プロパティで定義されている負の符号以外の負の符号を文字列に含めることができないため、変換は失敗します。

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

注釈

Int16.TryParse(String, Int16) メソッドは、解析された Int16 値を返す代わりに解析操作が成功したかどうかを示すブール値を返すことによって、Int16.Parse(String) メソッドとは異なります。 これにより、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)

ソース:
Int16.cs
ソース:
Int16.cs
ソース:
Int16.cs

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

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が正常に解析された結果、または失敗した場合は未定義の値が格納されます。

戻り値

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

適用対象

TryParse(String, IFormatProvider, Int16)

ソース:
Int16.cs
ソース:
Int16.cs
ソース:
Int16.cs

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

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 解析された結果または失敗した場合の未定義の値が格納されます。

戻り値

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

適用対象

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

ソース:
Int16.cs
ソース:
Int16.cs

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 が正常に解析された結果、または失敗した場合に未定義の値が含まれます。

戻り値

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

適用対象

TryParse(ReadOnlySpan<Byte>, Int16)

ソース:
Int16.cs
ソース:
Int16.cs

数値の文字列形式を含む 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 を格納します。 このパラメーターは初期化されていない状態で渡されます。結果で最初に指定された値は上書きされます。

戻り値

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

適用対象

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

ソース:
Int16.cs
ソース:
Int16.cs
ソース:
Int16.cs

指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の 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 を格納します。 パラメーターが または に準拠している形式ではない場合、または Int16.MinValue 未満の数値、または Int16.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

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

適用対象

TryParse(String, NumberStyles, IFormatProvider, Int16)

ソース:
Int16.cs
ソース:
Int16.cs
ソース:
Int16.cs

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の 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 を格納します。 パラメーターが または に準拠している形式ではない場合、または Int16.MinValue 未満の数値、または Int16.MaxValueより大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。result で最初に指定された値は上書きされます。

戻り値

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

例外

styleNumberStyles 値ではありません。

-又は-

style は、AllowHexSpecifier 値と 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 値を返す代わりに解析操作が成功したかどうかを示すブール値を返すことによって、Int16.Parse(String, NumberStyles, IFormatProvider) メソッドとは異なります。 これにより、s が無効であり、正常に解析できない場合に、例外処理を使用して FormatException をテストする必要がなくなります。

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

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

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

[ws]hexdigits[ws]

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

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

手記

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

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

非複合 NumberStyles 値 数字に加えて s で許可される要素
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 符号、数値を囲むかっこの形式で要素に署名します。
NumberStyles.AllowThousands 要素。
NumberStyles.AllowCurrencySymbol $ 要素。
NumberStyles.Currency すべての要素。 パラメーターは、指数表記で 16 進数または数値を表すことはできません。
NumberStyles.Float sの先頭または末尾にある ws 要素、sの先頭 符号、および 記号。 s パラメーターでは指数表記を使用することもできます。
NumberStyles.Number ws符号、桁区切り記号 ()、および小数点 (.) 要素。
NumberStyles.Any s を除くすべてのスタイルは、16 進数を表すことはできません。

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

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

こちらもご覧ください

適用対象