Int16.TryParse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を、それと等価な 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
された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 utf8Text
は 。それ以外の場合 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 を格納します。 パラメーターが null
または である場合、または Emptyが に準拠style
している形式ではない場合、または Int16.MinValue より小さい数値または Int16.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。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 を格納します。 パラメーターが null
または の形式ではない場合、または Int16.MinValue より小さい数値または Int16.MaxValue より大きい数値を表す場合s
、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。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.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) 呼び出します。
こちらもご覧ください
- Parse(String)
- ToString()
- .NET での数値文字列の解析
- サンプル: .NET Core WinForms 書式設定ユーティリティ (C#)
- サンプル: .NET Core WinForms 書式設定ユーティリティ (Visual Basic)
適用対象
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
された結果、または失敗した場合に未定義の値が格納されます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 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
された結果または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 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
された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 utf8Text
は 。それ以外の場合 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 が格納されます。 このパラメーターは、初期化されていない状態で渡されます。result にもともと入っていた値は上書きされます。
戻り値
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 を格納します。 パラメーターが null
または の形式style
でない場合、または Int16.MinValue より小さい数値または Int16.MaxValue より大きい数値を表す場合s
、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。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 を格納します。 パラメーターが null
または の形式style
でない場合、または Int16.MinValue より小さい数値または Int16.MaxValue より大きい数値を表す場合s
、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
例外
例
次の例では、 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 は、通貨記号を にs NumberStyles.AllowCurrencySymbol表示できます。 |
sign | 省略可能な記号。 |
数値 | 0 から 9 までの数字のシーケンス。 |
, | カルチャ固有の桁区切り記号。 でprovider 指定されたカルチャの桁区切り記号は、 に フラグが含まれている場合style にs NumberStyles.AllowThousands表示できます。 |
. | カルチャ固有の小数点記号。 でprovider 指定されたカルチャの小数点記号は、 に フラグが含まれている場合style にs NumberStyles.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 | ws、sign、thousands separator (,)、および decimal point (.) 要素。 |
NumberStyles.Any | を除く s すべてのスタイルは、16 進数を表すことはできません。 |
フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 s
プレフィックスのない 16 進値を指定する必要があります。 たとえば、"9AF3" は正常に解析されますが、"0x9AF3" は解析されません。 に存在できるその他の style
フラグは NumberStyles.AllowLeadingWhite と NumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStyles 、 NumberStyles.HexNumber両方の空白フラグを含む複合スタイル があります)。
パラメーターはprovider
IFormatProvider、オブジェクトや オブジェクトなどのCultureInfo実装でありNumberFormatInfo、そのメソッドは GetFormat オブジェクトをNumberFormatInfo返します。 オブジェクトは NumberFormatInfo 、 の形式に関するカルチャ固有の s
情報を提供します。 が null
の場合provider
は、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。
こちらもご覧ください
適用対象
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示