SByte.TryParse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を、それと等価な 8 ビット符号付き整数に変換します。 リターン コードは、変換が成功したか失敗したかを示します。
オーバーロード
TryParse(ReadOnlySpan<Byte>, IFormatProvider, SByte) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Char>, SByte) |
数値のスパン表現の、等価の SByte への変換を試み、変換に成功したかどうかを示す値を返します。 |
TryParse(String, SByte) |
数値の文字列形式を対応する SByte 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, SByte) |
文字のスパンを値に解析しようとします。 |
TryParse(String, IFormatProvider, SByte) |
文字列を値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, SByte) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, SByte) |
数値の文字列表現を含む UTF-8 文字スパンを、等価の 8 ビット符号付き整数に変換しようとします。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, SByte) |
指定されたスタイルおよびカルチャ固有の書式の数値のスパン表現の、等価の SByte への変換を試み、変換に成功したかどうかを示す値を返します。 |
TryParse(String, NumberStyles, IFormatProvider, SByte) |
指定されたスタイルおよびカルチャ固有の書式の数値の文字列形式を等価の SByte に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = IUtf8SpanParsable<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As SByte) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- provider
- IFormatProvider
utf8Text
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- SByte
戻り値には、正常に解析 utf8Text
された結果または失敗した場合の未定義の値が含まれます。
戻り値
true
正常に解析された場合 utf8Text
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Char>, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
- ソース:
- SByte.cs
重要
この API は CLS 準拠ではありません。
数値のスパン表現の、等価の SByte への変換を試み、変換に成功したかどうかを示す値を返します。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::SByte % result);
public static bool TryParse (ReadOnlySpan<char> s, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, out sbyte result);
static member TryParse : ReadOnlySpan<char> * sbyte -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * sbyte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As SByte) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を格納しているスパン。
- result
- SByte
このメソッドから制御が返されたとき、変換に成功した場合は、s
に格納された数値と等価な 8 ビット符号付き整数値が格納されます。変換に失敗した場合は 0 が格納されます。 パラメーターが null
または Emptyの形式でない場合、または SByte.MinValue より小さい数値または SByte.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
適用対象
TryParse(String, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
- ソース:
- SByte.cs
重要
この API は CLS 準拠ではありません。
数値の文字列形式を対応する SByte 表現に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::SByte % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, out sbyte result);
public static bool TryParse (string? s, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, out sbyte result);
[<System.CLSCompliant(false)>]
static member TryParse : string * sbyte -> bool
static member TryParse : string * sbyte -> bool
Public Shared Function TryParse (s As String, ByRef result As SByte) As Boolean
パラメーター
- s
- String
変換する数値を含んだ文字列。
- result
- SByte
このメソッドから制御が返されたとき、変換に成功した場合は、s
に格納された数値と等価な 8 ビット符号付き整数値が格納されます。変換に失敗した場合は 0 が格納されます。 パラメーターが null
または Emptyの形式でない場合、または SByte.MinValue より小さい数値または SByte.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
例
次の例では、 メソッドを呼び出TryParse(String, SByte)して、配列内の文字列を値にSByte変換しようとします。
string[] numericStrings = {"-3.6", "12.8", "+16.7", " 3 ", "(17)",
"-17", "+12", "18-", "987", "1,024", " 127 "};
sbyte number;
foreach (string numericString in numericStrings)
{
if (sbyte.TryParse(numericString, out number))
Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
else
Console.WriteLine("Cannot convert '{0}' to an SByte.", numericString);
}
// The example displays the following output to the console:
// Cannot convert '-3.6' to an SByte.
// Cannot convert '12.8' to an SByte.
// Cannot convert '+16.7' to an SByte.
// Converted ' 3 ' to 3.
// Cannot convert '(17)' to an SByte.
// Converted '-17' to -17.
// Converted '+12' to 12.
// Cannot convert '18-' to an SByte.
// Cannot convert '987' to an SByte.
// Cannot convert '1,024' to an SByte.
// Converted ' 127 ' to 127.
open System
let numericStrings =
[| "-3.6"; "12.8"; "+16.7"; " 3 "; "(17)"
"-17"; "+12"; "18-"; "987"; "1,024"; " 127 " |]
for numericString in numericStrings do
match SByte.TryParse numericString with
| true, number ->
printfn $"Converted '{numericString}' to {number}."
| _ ->
printfn $"Cannot convert '{numericString}' to an SByte."
// The example displays the following output to the console:
// Cannot convert '-3.6' to an SByte.
// Cannot convert '12.8' to an SByte.
// Cannot convert '+16.7' to an SByte.
// Converted ' 3 ' to 3.
// Cannot convert '(17)' to an SByte.
// Converted '-17' to -17.
// Converted '+12' to 12.
// Cannot convert '18-' to an SByte.
// Cannot convert '987' to an SByte.
// Cannot convert '1,024' to an SByte.
// Converted ' 127 ' to 127.
Dim numericStrings() As String = {"-3.6", "12.8", "+16.7", " 3 ", _
"(17)", "-17", "+12", "18-", "987", _
"1,024", " 127 "}
Dim number As SByte
For Each numericString As String In numericStrings
If SByte.TryParse(numericString, number) Then
Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
Else
Console.WriteLine("Cannot convert '{0}' to an SByte.", numericString)
End If
Next
' The example displays the following output to the console:
' Cannot convert '-3.6' to an SByte.
' Cannot convert '12.8' to an SByte.
' Cannot convert '+16.7' to an SByte.
' Converted ' 3 ' to 3.
' Cannot convert '(17)' to an SByte.
' Converted '-17' to -17.
' Converted '+12' to 12.
' Cannot convert '18-' to an SByte.
' Cannot convert '987' to an SByte.
' Cannot convert '1,024' to an SByte.
' Converted ' 127 ' to 127.
注釈
メソッドは SByte.TryParse(String, SByte) メソッドに SByte.Parse(String) 似ていますが、変換が失敗しても例外がスローされないことを除きます。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをvalue
テストFormatExceptionするために例外処理を使用する必要がなくなります。
パラメーターは s
、次の形式の 10 進数の文字列表現である必要があります。
[ws][sign]digits[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 |
sign | 省略可能な記号。 有効な符号文字は、現在のカルチャの NumberFormatInfo.NegativeSign プロパティと NumberFormatInfo.PositiveSign プロパティによって決まります。 |
数値 | 0 から 9 までの 10 進数のシーケンス。 |
注意
パラメーターで value
指定された文字列には、グループ区切り記号または小数点区切り記号を含めることはできません。また、小数部を含めることはできません。
パラメーターは s
、 スタイルを使用 NumberStyles.Integer して解釈されます。 10 進数に加えて、先頭と末尾に先頭の記号を付けるスペースのみを使用できます。 に存在できるカルチャ固有の書式設定情報を使用してスタイル要素を明示的に value
定義するには、 メソッドを TryParse(String, NumberStyles, IFormatProvider, SByte) 呼び出します。
パラメーターは s
、現在のカルチャの オブジェクトの書式設定情報を NumberFormatInfo 使用して解析されます。 詳細については、「NumberFormatInfo.CurrentInfo」を参照してください。
このオーバーロードでは、 パラメーター内のすべての数字が value
10 進数として解釈されます。 16 進数の文字列形式を解析するには、代わりに オーバーロードを TryParse(String, NumberStyles, IFormatProvider, SByte) 呼び出します。
こちらもご覧ください
適用対象
TryParse(ReadOnlySpan<Char>, IFormatProvider, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
- ソース:
- SByte.cs
文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = ISpanParsable<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As SByte) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
解析する文字のスパン。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- SByte
このメソッドから制御が戻るときに、 には、正常に解析 s
された結果、または失敗した場合は未定義の値が格納されます。
戻り値
true
正常に解析された場合 s
は 。それ以外の場合 false
は 。
適用対象
TryParse(String, IFormatProvider, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
- ソース:
- SByte.cs
文字列を値に解析しようとします。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = IParsable<System::SByte>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out sbyte result);
static member TryParse : string * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As SByte) As Boolean
パラメーター
- s
- String
解析する文字列。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- SByte
このメソッドから制御が戻るときに、正常に解析 s
された結果または失敗した場合は未定義の値が格納されます。
戻り値
true
正常に解析された場合 s
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- style
- NumberStyles
に utf8Text
存在できる数値スタイルのビットごとの組み合わせ。
- provider
- IFormatProvider
utf8Text
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- SByte
戻り値には、正常に解析 utf8Text
された結果または失敗した場合の未定義の値が含まれます。
戻り値
true
正常に解析された場合 utf8Text
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Byte>, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
数値の文字列表現を含む UTF-8 文字スパンを、等価の 8 ビット符号付き整数に変換しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::SByte % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out sbyte result);
static member TryParse : ReadOnlySpan<byte> * sbyte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As SByte) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
変換する数値を表す UTF-8 文字を含むスパン。
- result
- SByte
このメソッドが戻るときに、 には、変換が成功した場合は に utf8Text
含まれる数値と等価の 8 ビット符号付き整数値が格納され、変換に失敗した場合は 0 が格納されます。 このパラメーターは、初期化されていない状態で渡されます。result にもともと入っていた値は上書きされます。
戻り値
utf8Text
が正常に変換された場合は true
。それ以外の場合は false
。
適用対象
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
- ソース:
- SByte.cs
重要
この API は CLS 準拠ではありません。
指定されたスタイルおよびカルチャ固有の書式の数値のスパン表現の、等価の SByte への変換を試み、変換に成功したかどうかを示す値を返します。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を格納しているスパン。
- style
- NumberStyles
s
で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- SByte
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等価の 8 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが または である場合、または Emptyが null
に準拠style
している形式ではない場合、または SByte.MinValue より小さい数値または SByte.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
適用対象
TryParse(String, NumberStyles, IFormatProvider, SByte)
- ソース:
- SByte.cs
- ソース:
- SByte.cs
- ソース:
- SByte.cs
指定されたスタイルおよびカルチャ固有の書式の数値の文字列形式を等価の SByte に変換できるかどうかを試行し、変換に成功したかどうかを示す値を返します。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;
[System.CLSCompliant(false)]
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out sbyte result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
[<System.CLSCompliant(false)>]
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean
パラメーター
- s
- String
変換する数値を表す文字列。
- style
- NumberStyles
s
で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- SByte
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等価の 8 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが または である場合、または Emptyが null
に準拠style
している形式ではない場合、または SByte.MinValue より小さい数値または SByte.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
例外
例
次の例では、 TryParse(String, NumberStyles, IFormatProvider, SByte) さまざまな文字列と値を使用して メソッドを NumberStyles 呼び出します。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "106";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-106";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "103.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "103.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E-01";
CallTryParse(numericString, styles);
numericString = "12E01";
CallTryParse(numericString, styles);
numericString = "C8";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x8C";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
sbyte number;
bool result = SByte.TryParse(stringToConvert, styles,
CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
else
Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
}
}
// The example displays the following output:
// Converted '106' to 106.
// Attempted conversion of '-106' failed.
// Converted '103.00' to 103.
// Attempted conversion of '103.72' failed.
// Converted '10E-01' to 1.
// Attempted conversion of '12E-01' failed.
// Converted '12E01' to 120.
// Converted 'C8' to -56.
// Attempted conversion of '0x8C' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) styles =
match SByte.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
let numericString = "106"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-106"
let styles = NumberStyles.None
callTryParse numericString styles
let numericString = "103.00"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "103.72"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "10E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "12E-01"
callTryParse numericString styles
let numericString = "12E01"
callTryParse numericString styles
let numericString = "C8"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x8C"
callTryParse numericString NumberStyles.HexNumber
0
// The example displays the following output:
// Converted '106' to 106.
// Attempted conversion of '-106' failed.
// Converted '103.00' to 103.
// Attempted conversion of '103.72' failed.
// Converted '10E-01' to 1.
// Attempted conversion of '12E-01' failed.
// Converted '12E01' to 120.
// Converted 'C8' to -56.
// Attempted conversion of '0x8C' failed.
Imports System.Globalization
Module StringParsing
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "106"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-106"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "103.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "103.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "12E-01"
CallTryParse(numericString, styles)
numericString = "12E01"
CallTryParse(numericString, styles)
numericString = "C8"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x8C"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As SByte
Dim result As Boolean = SByte.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 '106' to 106.
' Attempted conversion of '-106' failed.
' Converted '103.00' to 103.
' Attempted conversion of '103.72' failed.
' Converted '10E-01' to 1.
' Attempted conversion of '12E-01' failed.
' Converted '12E01' to 120.
' Converted 'C8' to -56.
' Attempted conversion of '0x8C' failed.
注釈
メソッドは TryParse(String, NumberStyles, IFormatProvider, SByte) メソッドに Parse(String, NumberStyles, IFormatProvider) 似ていますが、変換が失敗しても例外がスローされないことを除きます。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをvalue
テストFormatExceptionするために例外処理を使用する必要がなくなります。
パラメーターは style
、解析操作を成功させるために パラメーターで value
許可されるスタイル要素 (空白や正または負の符号など) を定義します。 列挙体のビット フラグ NumberStyles の組み合わせである必要があります。 の style
値に応じて、 パラメーターに value
次の要素を含めることができます。
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
パラメーターに が style
含まれている AllowHexSpecifier場合、パラメーターには value
次の要素を含めることができます。
[ws]hexdigits[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 空白は、 が フラグを含むNumberStyles.AllowLeadingWhite場合style は のvalue 先頭に、フラグが含まれている場合style は のvalue 末尾にNumberStyles.AllowTrailingWhite表示されます。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、 パラメーターの CurrencyPositivePatternNumberFormatInfo メソッドによって返されるオブジェクトの provider プロパティによってGetFormat定義されます。 に フラグが含まれている場合style は、通貨記号を にvalue NumberStyles.AllowCurrencySymbol表示できます。 |
sign | 省略可能な記号。 署名は、 フラグが含まれている場合は のvalue 先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合style は のvalue 末尾にNumberStyles.AllowTrailingSign表示style されます。 に フラグが含まれている場合style は、かっこを使用value して負の値をNumberStyles.AllowParentheses示すことができます。 |
数値 | 0 から 9 までの数字のシーケンス。 |
, | カルチャ固有のグループ区切り記号。 でprovider 指定されたカルチャのグループ区切り記号は、 に フラグが含まれている場合style にvalue NumberStyles.AllowThousands表示できます。 |
. | カルチャ固有の小数点記号。 でprovider 指定されたカルチャの小数点記号は、 に フラグが含まれている場合style にvalue NumberStyles.AllowDecimalPoint表示できます。 |
fractional_digits | 数字 0 が 1 回以上出現します。 小数部の数字は、 に value フラグがNumberStyles.AllowDecimalPoint含まれている場合style にのみ表示されます。 |
E | "e" または "E" 文字。値が指数 (指数) 表記で表されることを示します。 フラグが含まれている場合style 、パラメーターはvalue 指数表記で数値をNumberStyles.AllowExponent表すことができます。 |
exponential_digits | 0 から 9 までの数字のシーケンス。 フラグが含まれている場合style 、パラメーターはvalue 指数表記で数値をNumberStyles.AllowExponent表すことができます。 |
hexdigits | 0 から f、または 0 から F までの 16 進数のシーケンス。 |
注意
の終端の NUL (U+0000) 文字 s
は、引数の style
値に関係なく、解析操作では無視されます。
10 進数のみの文字列 (フラグに NumberStyles.None 対応) は常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、この入力文字列に存在する可能性がありますが、存在する必要がない要素を制御します。 次の表は、 に存在する可能性がある要素に対する個々 NumberStyles のメンバーの value
影響を示しています。
非複合 NumberStyles 値 |
数字に加えて値で許可される要素 |
---|---|
None | 10 進数のみ。 |
AllowDecimalPoint | 小数点 (.) 要素と fractional_digits 要素。 ただし、 fractional_digits は 1 つ以上の 0 桁のみで構成する必要があります。または、 メソッドは を返します false 。 |
AllowExponent | 指数表記と exponential_digitsを示す "e" または "E" 文字。 指数表記で数値を表す場合 value は、0 以外の小数部を含めることはできません。 |
AllowLeadingWhite | の先頭value にある ws 要素。 |
AllowTrailingWhite | の末尾value にある ws 要素。 |
AllowLeadingSign | 数字の前の sign 要素。 |
AllowTrailingSign | 数字の後の sign 要素。 |
AllowParentheses | 数値を囲むかっこの形式の sign 要素。 |
AllowThousands | グループ区切り記号 (,) 要素。 |
AllowCurrencySymbol | currency ($) 要素。 |
Currency | すべての要素。 ただし、 value 16 進数または数値を指数表記で表すことはできません。 |
Float | の先頭または末尾の value ws 要素、の先頭value の符号、および小数点 (.) 記号。 パラメーターでは value 、指数表記を使用することもできます。 |
Number | ws、sign、group separator (,)、および decimal point (.) 要素。 |
Any | すべての要素。 ただし、 value 16 進数を表すことはできません。 |
フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 value
16 進値にする必要があります。 有効な 16 進数は、0 から 9、a から f、および A から F です。 に存在できるその他の style
フラグは NumberStyles.AllowLeadingWhite と NumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStyles 、 HexNumber両方の空白フラグを含む複合スタイル があります)。
注意
が 16 進数の文字列表現である場合value
、その前に 16 進数として区別する装飾 (や &h
など0x
) を付けることはできません。 これにより、変換が失敗します。
パラメーターは provider
実装です IFormatProvider 。 そのメソッドは GetFormat 、 の形式に NumberFormatInfo 関するカルチャ固有の value
情報を提供する オブジェクトを返します。 パラメーターには provider
、次のいずれかを指定できます。
CultureInfo書式設定情報を提供するカルチャを表す オブジェクト。 そのメソッドは GetFormat 、そのカルチャの NumberFormatInfo 数値書式情報を提供する オブジェクトを返します。
NumberFormatInfo数値書式情報を提供する オブジェクト。 (の実装 GetFormat は、それ自体を返すだけです)。
を実装 IFormatProviderするカスタム オブジェクト。 そのメソッドは GetFormat 、書式設定情報を提供する オブジェクトを NumberFormatInfo インスタンス化して返します。
が null
の場合provider
は、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。
こちらもご覧ください
適用対象
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示