Byte.TryParse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を等価の Byte に変換し、変換が成功したかどうかを示す値を返します。
オーバーロード
| 名前 | 説明 |
|---|---|
| TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Char>, Byte) |
数値のスパン表現を等価の Byte に変換し、変換が成功したかどうかを示す値を返します。 |
| TryParse(String, Byte) |
数値の文字列形式を等価の Byte に変換し、変換が成功したかどうかを示す値を返します。 |
| TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte) |
文字のスパンを値に解析しようとします。 |
| TryParse(String, IFormatProvider, Byte) |
文字列を値に解析しようとします。 |
| TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Byte) |
UTF-8 文字のスパンを値に解析しようとします。 |
| TryParse(ReadOnlySpan<Byte>, Byte) |
数値の文字列形式を含む UTF-8 文字スパンを、等価の 8 ビット符号なし整数に変換しようとします。 |
| TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte) |
指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、同等の Byte に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
| TryParse(String, NumberStyles, IFormatProvider, Byte) |
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、同等の Byte に変換します。 戻り値は、変換が成功したか失敗したかを示します。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = IUtf8SpanParsable<System::Byte>::TryParse;
public static bool TryParse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Byte) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- provider
- IFormatProvider
utf8Textに関するカルチャ固有の書式設定情報を提供するオブジェクト。
- result
- Byte
戻り値には、 utf8Text が正常に解析された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
utf8Textが正常に解析された場合は。それ以外の場合はfalse。
適用対象
TryParse(ReadOnlySpan<Char>, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
数値のスパン表現を等価の Byte に変換し、変換が成功したかどうかを示す値を返します。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse(ReadOnlySpan<char> s, out byte result);
static member TryParse : ReadOnlySpan<char> * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Byte) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を含むスパン。
- result
- Byte
このメソッドから制御が戻るときに、変換に成功した場合は s に含まれる数値に相当するByte値を格納し、変換に失敗した場合は 0 を格納します。 このパラメーターは初期化されていない状態で渡されます。 result で最初に指定された値は上書きされます。
戻り値
適用対象
TryParse(String, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
数値の文字列形式を等価の Byte に変換し、変換が成功したかどうかを示す値を返します。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse(string s, out byte result);
public static bool TryParse(string? s, out byte result);
static member TryParse : string * byte -> bool
Public Shared Function TryParse (s As String, ByRef result As Byte) As Boolean
パラメーター
- s
- String
変換する数値を含む文字列。
- result
- Byte
このメソッドから制御が戻るときに、変換に成功した場合は s に含まれる数値に相当するByte値を格納し、変換に失敗した場合は 0 を格納します。 このパラメーターは初期化されていない状態で渡されます。 result で最初に指定された値は上書きされます。
戻り値
例
次の例では、さまざまな文字列値を使用して TryParse(String, Byte) メソッドを呼び出します。
using System;
public class ByteConversion
{
public static void Main()
{
string[] byteStrings = { null, string.Empty, "1024",
"100.1", "100", "+100", "-100",
"000000000000000100", "00,100",
" 20 ", "FF", "0x1F" };
foreach (var byteString in byteStrings)
{
CallTryParse(byteString);
}
}
private static void CallTryParse(string stringToConvert)
{
byte byteValue;
bool success = Byte.TryParse(stringToConvert, out byteValue);
if (success)
{
Console.WriteLine("Converted '{0}' to {1}",
stringToConvert, byteValue);
}
else
{
Console.WriteLine("Attempted conversion of '{0}' failed.",
stringToConvert);
}
}
}
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Attempted conversion of '' failed.
// Attempted conversion of '1024' failed.
// Attempted conversion of '100.1' failed.
// Converted '100' to 100
// Converted '+100' to 100
// Attempted conversion of '-100' failed.
// Converted '000000000000000100' to 100
// Attempted conversion of '00,100' failed.
// Converted ' 20 ' to 20
// Attempted conversion of 'FF' failed.
// Attempted conversion of '0x1F' failed.
open System
let callTryParse (stringToConvert: string) =
match Byte.TryParse stringToConvert with
| true, byteValue ->
printfn $"Converted '{stringToConvert}' to {byteValue}"
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
let byteStrings =
[ null; String.Empty; "1024"
"100.1"; "100"; "+100"; "-100"
"000000000000000100"; "00,100"
" 20 "; "FF"; "0x1F" ]
for byteString in byteStrings do
callTryParse byteString
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Attempted conversion of '' failed.
// Attempted conversion of '1024' failed.
// Attempted conversion of '100.1' failed.
// Converted '100' to 100
// Converted '+100' to 100
// Attempted conversion of '-100' failed.
// Converted '000000000000000100' to 100
// Attempted conversion of '00,100' failed.
// Converted ' 20 ' to 20
// Attempted conversion of 'FF' failed.
// Attempted conversion of '0x1F' failed.
Module ByteConversion
Public Sub Main()
Dim byteStrings() As String = { Nothing, String.Empty, "1024",
"100.1", "100", "+100", "-100",
"000000000000000100", "00,100",
" 20 ", "FF", "0x1F"}
For Each byteString As String In byteStrings
CallTryParse(byteString)
Next
End Sub
Private Sub CallTryParse(stringToConvert As String)
Dim byteValue As Byte
Dim success As Boolean = Byte.TryParse(stringToConvert, byteValue)
If success Then
Console.WriteLine("Converted '{0}' to {1}", _
stringToConvert, byteValue)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.", _
stringToConvert)
End If
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '' failed.
' Attempted conversion of '' failed.
' Attempted conversion of '1024' failed.
' Attempted conversion of '100.1' failed.
' Converted '100' to 100
' Converted '+100' to 100
' Attempted conversion of '-100' failed.
' Converted '000000000000000100' to 100
' Attempted conversion of '00,100' failed.
' Converted ' 20 ' to 20
' Attempted conversion of 'FF' failed.
' Attempted conversion of '0x1F' failed.
注釈
変換が失敗し、s パラメーターが正しい形式でない場合、nullまたはString.Emptyの場合、またはMinValue未満またはMaxValueより大きい数値を表す場合、メソッドはfalseを返します。
Byte.TryParse(String, Byte) メソッドは、Byte.Parse(String) メソッドに似ていますが、変換が失敗してもTryParse(String, Byte)が例外をスローしない点が異なります。
s パラメーターは、次の形式の数値の文字列形式にする必要があります。
[ws][sign]digits[ws]
角かっこ ([ と ]) の要素は省略可能です。 次の表は、それぞれの要素の説明です。
| 要素 | 説明 |
|---|---|
| ws | 省略可能な空白。 |
| サイン | 現在のカルチャの NumberFormatInfo.PositiveSign プロパティで指定された、省略可能な正符号。 |
| 桁 | 0 から 9 の範囲の 10 進数のシーケンス。 |
s パラメーターは、Integer スタイルを使用して解釈されます。 バイト値の 10 進数字に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 (符号が存在する場合は、正符号であるか、メソッドが OverflowExceptionをスローする必要があります)。 sに存在できるカルチャ固有の書式設定情報と共にスタイル要素を明示的に定義するには、 Byte.Parse(String, NumberStyles, IFormatProvider) メソッドを使用します。
s パラメーターは、現在のカルチャのNumberFormatInfo オブジェクトの書式設定情報を使用して解析されます。 詳細については、NumberFormatInfo.CurrentInfoを参照してください。
Byte.TryParse(String, Byte) メソッドのこのオーバーロードは、s パラメーター内のすべての数字を 10 進数として解釈します。 16 進数の文字列形式を解析するには、 Byte.TryParse(String, NumberStyles, IFormatProvider, Byte) オーバーロードを呼び出します。
こちらもご覧ください
- サンプル: .NET Core WinForms Formatting Utility (C#)
- サンプル: .NET Core WinForms Formatting Utility (Visual Basic)
適用対象
TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = ISpanParsable<System::Byte>::TryParse;
public static bool TryParse(ReadOnlySpan<char> s, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Byte) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
解析する文字のスパン。
- provider
- IFormatProvider
sに関するカルチャ固有の書式設定情報を提供するオブジェクト。
- result
- Byte
このメソッドから制御が戻るときに、 sの解析に成功した結果、または失敗した場合は未定義の値が格納されます。
戻り値
true
sが正常に解析された場合は。それ以外の場合はfalse。
適用対象
TryParse(String, IFormatProvider, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
文字列を値に解析しようとします。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = IParsable<System::Byte>::TryParse;
public static bool TryParse(string? s, IFormatProvider? provider, out byte result);
static member TryParse : string * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Byte) As Boolean
パラメーター
- s
- String
解析する文字列。
- provider
- IFormatProvider
sに関するカルチャ固有の書式設定情報を提供するオブジェクト。
- result
- Byte
このメソッドから制御が戻るときに、正常に s 解析された結果または失敗した場合の未定義の値が格納されます。
戻り値
true
sが正常に解析された場合は。それ以外の場合はfalse。
適用対象
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- style
- NumberStyles
utf8Textに存在できる数値スタイルのビットごとの組み合わせ。
- provider
- IFormatProvider
utf8Textに関するカルチャ固有の書式設定情報を提供するオブジェクト。
- result
- Byte
戻り値には、 utf8Text が正常に解析された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
utf8Textが正常に解析された場合は。それ以外の場合はfalse。
適用対象
TryParse(ReadOnlySpan<Byte>, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
数値の文字列形式を含む UTF-8 文字スパンを、等価の 8 ビット符号なし整数に変換しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse(ReadOnlySpan<byte> utf8Text, out byte result);
static member TryParse : ReadOnlySpan<byte> * byte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Byte) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
変換する数値を表す UTF-8 文字を含むスパン。
- result
- Byte
このメソッドから制御が戻るときに、変換に成功した場合は utf8Text に含まれる数値に相当する 8 ビット符号なし整数値を格納し、変換に失敗した場合は 0 を格納します。 このパラメーターは初期化されていない状態で渡されます。結果で最初に指定された値は上書きされます。
戻り値
適用対象
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、同等の Byte に変換します。 戻り値は、変換が成功したか失敗したかを示します。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result);
public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
public static bool TryParse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out byte result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を含むスパン。 スパンは、 Integer スタイルを使用して解釈されます。
- style
- NumberStyles
sに存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は Integerです。
- provider
- IFormatProvider
sに関するカルチャ固有の書式設定情報を提供するオブジェクト。
providerがnullされている場合は、スレッドの現在のカルチャが使用されます。
- result
- Byte
このメソッドから制御が戻るときに、変換に成功した場合は s に含まれる数値に相当する 8 ビット符号なし整数値を格納し、変換に失敗した場合は 0 を格納します。
s パラメーターがnullまたはEmpty、正しい形式ではない場合、または Byte.MinValue より小さい数値または Byte.MaxValue より大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。 result で最初に指定された値は上書きされます。
戻り値
適用対象
TryParse(String, NumberStyles, IFormatProvider, Byte)
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
- ソース:
- Byte.cs
指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、同等の Byte に変換します。 戻り値は、変換が成功したか失敗したかを示します。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::Byte % result) = System::Numerics::INumberBase<System::Byte>::TryParse;
public static bool TryParse(string s, System.Globalization.NumberStyles style, IFormatProvider provider, out byte result);
public static bool TryParse(string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out byte result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * byte -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Byte) As Boolean
パラメーター
- s
- String
変換する数値を含む文字列。 文字列は、 styleで指定されたスタイルを使用して解釈されます。
- style
- NumberStyles
sに存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は Integerです。
- provider
- IFormatProvider
sに関するカルチャ固有の書式設定情報を提供するオブジェクト。
providerがnullされている場合は、スレッドの現在のカルチャが使用されます。
- result
- Byte
このメソッドから制御が戻るときに、変換に成功した場合は s に含まれる数値に相当する 8 ビット符号なし整数値を格納し、変換に失敗した場合は 0 を格納します。
s パラメーターがnullまたはEmpty、正しい形式ではない場合、または Byte.MinValue より小さい数値または Byte.MaxValue より大きい数値を表す場合、変換は失敗します。 このパラメーターは初期化されていない状態で渡されます。 result で最初に指定された値は上書きされます。
戻り値
例外
例
次の例では、さまざまな文字列値を使用して TryParse(String, NumberStyles, IFormatProvider, Byte) メソッドを呼び出します。
using System;
using System.Globalization;
public class ByteConversion2
{
public static void Main()
{
string byteString;
NumberStyles styles;
byteString = "1024";
styles = NumberStyles.Integer;
CallTryParse(byteString, styles);
byteString = "100.1";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(byteString, styles);
byteString = "100.0";
CallTryParse(byteString, styles);
byteString = "+100";
styles = NumberStyles.Integer | NumberStyles.AllowLeadingSign
| NumberStyles.AllowTrailingSign;
CallTryParse(byteString, styles);
byteString = "-100";
CallTryParse(byteString, styles);
byteString = "000000000000000100";
CallTryParse(byteString, styles);
byteString = "00,100";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(byteString, styles);
byteString = "2E+3 ";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(byteString, styles);
byteString = "FF";
styles = NumberStyles.HexNumber;
CallTryParse(byteString, styles);
byteString = "0x1F";
CallTryParse(byteString, styles);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
Byte byteValue;
bool result = Byte.TryParse(stringToConvert, styles,
null as IFormatProvider, out byteValue);
if (result)
Console.WriteLine("Converted '{0}' to {1}",
stringToConvert, byteValue);
else
Console.WriteLine("Attempted conversion of '{0}' failed.",
stringToConvert.ToString());
}
}
// The example displays the following output to the console:
// Attempted conversion of '1024' failed.
// Attempted conversion of '100.1' failed.
// Converted '100.0' to 100
// Converted '+100' to 100
// Attempted conversion of '-100' failed.
// Converted '000000000000000100' to 100
// Converted '00,100' to 100
// Attempted conversion of '2E+3 ' failed.
// Converted 'FF' to 255
// Attempted conversion of '0x1F' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match Byte.TryParse(stringToConvert, styles, null) with
| true, byteValue ->
printfn $"Converted '{stringToConvert}' to {byteValue}"
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
let byteString = "1024"
let styles = NumberStyles.Integer
callTryParse byteString styles
let byteString = "100.1"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse byteString styles
let byteString = "100.0"
callTryParse byteString styles
let byteString = "+100"
let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign
callTryParse byteString styles
let byteString = "-100"
callTryParse byteString styles
let byteString = "000000000000000100"
callTryParse byteString styles
let byteString = "00,100"
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
callTryParse byteString styles
let byteString = "2E+3 "
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse byteString styles
let byteString = "FF"
let styles = NumberStyles.HexNumber
callTryParse byteString styles
let byteString = "0x1F"
callTryParse byteString styles
0
// The example displays the following output to the console:
// Attempted conversion of '1024' failed.
// Attempted conversion of '100.1' failed.
// Converted '100.0' to 100
// Converted '+100' to 100
// Attempted conversion of '-100' failed.
// Converted '000000000000000100' to 100
// Converted '00,100' to 100
// Attempted conversion of '2E+3 ' failed.
// Converted 'FF' to 255
// Attempted conversion of '0x1F' failed.
Imports System.Globalization
Module ByteConversion2
Public Sub Main()
Dim byteString As String
Dim styles As NumberStyles
byteString = "1024"
styles = NumberStyles.Integer
CallTryParse(byteString, styles)
byteString = "100.1"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(byteString, styles)
byteString = "100.0"
CallTryParse(byteString, styles)
byteString = "+100"
styles = NumberStyles.Integer Or NumberStyles.AllowLeadingSign _
Or NumberStyles.AllowTrailingSign
CallTryParse(byteString, styles)
byteString = "-100"
CallTryParse(byteString, styles)
byteString = "000000000000000100"
CallTryParse(byteString, styles)
byteString = "00,100"
styles = NumberStyles.Integer Or NumberStyles.AllowThousands
CallTryParse(byteString, styles)
byteString = "2E+3 "
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(byteString, styles)
byteString = "FF"
styles = NumberStyles.HexNumber
CallTryParse(byteString, styles)
byteString = "0x1F"
CallTryParse(byteString, styles)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles As NumberStyles)
Dim byteValue As Byte
Dim result As Boolean = Byte.TryParse(stringToConvert, styles, Nothing, _
byteValue)
If result Then
Console.WriteLine("Converted '{0}' to {1}", _
stringToConvert, byteValue)
Else
If stringToConvert Is Nothing Then stringToConvert = ""
Console.WriteLine("Attempted conversion of '{0}' failed.", _
stringToConvert.ToString())
End If
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '1024' failed.
' Attempted conversion of '100.1' failed.
' Converted '100.0' to 100
' Converted '+100' to 100
' Attempted conversion of '-100' failed.
' Converted '000000000000000100' to 100
' Converted '00,100' to 100
' Attempted conversion of '2E+3 ' failed.
' Converted 'FF' to 255
' Attempted conversion of '0x1F' failed.
注釈
TryParse メソッドはParse メソッドに似ていますが、TryParse メソッドは変換に失敗しても例外をスローしません。
s パラメーターは、provider パラメーターによって指定されたNumberFormatInfo オブジェクトの書式設定情報を使用して解析されます。
style パラメーターは、解析操作を成功させるために s パラメーターで許可されるスタイル要素 (空白や正の符号など) を定義します。
NumberStyles列挙型のビット フラグの組み合わせである必要があります。
styleの値によっては、s パラメーターに次の要素が含まれる場合があります。
[ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]
または、 style パラメーターに AllowHexSpecifierが含まれている場合は、次のようにします。
[ws]hexdigits[ws]
角かっこ ( [ および ] ) の要素は省略可能です。 次の表は、それぞれの要素の説明です。
| 要素 | 説明 |
|---|---|
| ws | 省略可能な空白。
styleにNumberStyles.AllowLeadingWhite フラグが含まれている場合はsの先頭に空白が表示され、スタイルに NumberStyles.AllowTrailingWhite フラグが含まれている場合は s の末尾に空白が表示されます。 |
| $ | カルチャ固有の通貨記号。 文字列内での位置は、NumberFormatInfo.CurrencyPositivePattern パラメーターのNumberFormatInfo メソッドによって返されるGetFormat オブジェクトのprovider プロパティによって定義されます。
sにstyleフラグが含まれている場合、通貨記号はNumberStyles.AllowCurrencySymbolに表示されます。 |
| サイン | 省略可能な正符号。 (sに負の符号が存在する場合、解析操作は失敗します)。styleにNumberStyles.AllowLeadingSign フラグが含まれている場合はsの先頭に、NumberStyles.AllowTrailingSign フラグが含まれている場合はsの最後style表示されます。 |
| 桁 | 0 から 9 までの数字のシーケンス。 |
| . | カルチャ固有の小数点記号。
providerにs フラグが含まれている場合、styleで指定されたカルチャの小数点記号をNumberStyles.AllowDecimalPointに表示できます。 |
| fractional_digits | 数字 0 の 1 つ以上の出現。 小数部の数字は、sにstyle フラグが含まれている場合にのみ、NumberStyles.AllowDecimalPointに表示できます。 |
| e | e または E 文字。値が指数表記で表されることを示します。
s パラメーターは、style フラグが含まれている場合NumberStyles.AllowExponent指数表記で数値を表すことができます。 |
| 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 以外の小数部を含まないByteデータ型の範囲内の整数を表す必要があります。 |
| NumberStyles.AllowLeadingWhite |
sの先頭にある ws 要素。 |
| NumberStyles.AllowTrailingWhite |
の末尾にある s 要素。 |
| NumberStyles.AllowLeadingSign | 正の符号は 数字の前に表示できます。 |
| NumberStyles.AllowTrailingSign | 正の符号は 数字の後に表示されます。 |
| NumberStyles.AllowParentheses | このフラグはサポートされていますが、かっこがsに存在する場合、メソッドはfalseを返します。 |
| NumberStyles.AllowThousands | グループ区切り記号は sに表示できますが、先頭には 1 桁以上の 0 桁のみを付けることができます。 |
| NumberStyles.AllowCurrencySymbol | $要素。 |
NumberStyles.AllowHexSpecifier フラグを使用する場合、sはプレフィックスのない 16 進数の値である必要があります。 たとえば、"F3" は正常に解析されますが、"0xF3" では解析されません。
styleに存在できるその他のフラグは、NumberStyles.AllowLeadingWhiteとNumberStyles.AllowTrailingWhiteだけです。 ( NumberStyles 列挙型には、両方の空白フラグを含む複合数値スタイル ( NumberStyles.HexNumber) があります。
provider パラメーターは、CultureInfo オブジェクトやNumberFormatInfo オブジェクトなどのIFormatProvider実装であり、GetFormat メソッドはNumberFormatInfo オブジェクトを返します。
NumberFormatInfo オブジェクトは、sの形式に関するカルチャ固有の情報を提供します。