Byte.Parse メソッド

定義

数値の文字列形式を、それと等価の Byte に変換します。

オーバーロード

Parse(String, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャ固有の書式の数値の文字列形式を、それと等価の Byte に変換します。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャ固有の書式の数値のスパン表現を、それと等価の Byte に変換します。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(String, IFormatProvider)

指定されたカルチャ固有の書式で表現された文字列形式の数値を、それと等価の Byte に変換します。

Parse(String, NumberStyles)

指定のスタイルで表現された数値の文字列形式を、それと等価な Byte に変換します。

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(String)

数値の文字列形式を、それと等価の Byte に変換します。

Parse(String, NumberStyles, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

指定したスタイルおよびカルチャ固有の書式の数値の文字列形式を、それと等価の Byte に変換します。

public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Byte>::Parse;
public static byte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static byte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Byte

パラメーター

s
String

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

style
NumberStyles

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

provider
IFormatProvider

s の書式設定に関するカルチャ固有の情報を提供するオブジェクト。 providernull の場合は、スレッドの現在のカルチャが使用されます。

戻り値

s に格納されている数値と等価のバイト値。

実装

例外

snullです。

s が正しい形式ではありません。

s は、 Byte.MinValue より小さい数値または Byte.MaxValue より大きい数値を表します。

- または -

s に 0 以外の小数部の桁が含まれています。

styleNumberStyles 値ではありません。

- または -

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

次のコード例では、 メソッドのこのオーバーロードを使用して、値の Byte 文字列表現を Byte.Parse(String, NumberStyles, IFormatProvider) 解析します。

NumberStyles style;
CultureInfo^ culture;
String^ value;
Byte number;

// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles::Float;     
culture = CultureInfo::CreateSpecificCulture("fr-FR");
value = "12,000";

number = Byte::Parse(value, style, culture);
Console::WriteLine("Converted '{0}' to {1}.", value, number);

culture = CultureInfo::CreateSpecificCulture("en-GB");
try
{
   number = Byte::Parse(value, style, culture);
   Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", value); }   

value = "12.000";
number = Byte::Parse(value, style, culture);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
NumberStyles style;
CultureInfo culture;
string value;
byte number;

// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
value = "12,000";

number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);

culture = CultureInfo.CreateSpecificCulture("en-GB");
try
{
   number = Byte.Parse(value, style, culture);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", value); }

value = "12.000";
number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
let style = NumberStyles.Float
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
let value = "12,000"

let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

let culture = CultureInfo.CreateSpecificCulture "en-GB"
try
    let number = Byte.Parse(value, style, culture)
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."

let value = "12.000"
let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
Dim style As NumberStyles
Dim culture As CultureInfo
Dim value As String
Dim number As Byte

' Parse number with decimals.
' NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float       
culture = CultureInfo.CreateSpecificCulture("fr-FR")
value = "12,000"

number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)

culture = CultureInfo.CreateSpecificCulture("en-GB")
Try
   number = Byte.Parse(value, style, culture)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)   
End Try      

value = "12.000"
number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' The example displays the following output to the console:
'       Converted '12,000' to 12.
'       Unable to parse '12,000'.
'       Converted '12.000' to 12.

注釈

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

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

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

[ws]hexdigits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

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

注意

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

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

非複合 NumberStyles 値 数字に加えて、 で許可される要素
NumberStyles.None 10 進数のみ。
NumberStyles.AllowDecimalPoint 要素fractional_digits 要素。 ただし、 fractional_digits は 1 つ以上の 0 桁のみで構成する必要があります。または が OverflowException スローされます。
NumberStyles.AllowExponent パラメーターでは s 指数表記を使用することもできます。
NumberStyles.AllowLeadingWhite の先頭sにある ws 要素。
NumberStyles.AllowTrailingWhite の末尾sにある ws 要素。
NumberStyles.AllowLeadingSign 正符号は 数字の前に表示できます。
NumberStyles.AllowTrailingSign 正符号は 数字の後に表示できます。
NumberStyles.AllowParentheses このフラグはサポートされていますが、 で s かっこを使用すると、 になります OverflowException
NumberStyles.AllowThousands グループ区切り記号は に s表示できますが、先頭には 1 桁以上の 0 桁のみを指定できます。
NumberStyles.AllowCurrencySymbol $ 要素。

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

パラメーターはproviderIFormatProviderCultureInfo オブジェクトなどのNumberFormatInfo実装です。 パラメーターは provider 、解析で使用されるカルチャ固有の情報を提供します。 providernull の場合は、スレッドの現在のカルチャが使用されます。

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

指定したスタイルおよびカルチャ固有の書式の数値のスパン表現を、それと等価の Byte に変換します。

public static byte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static byte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Byte

パラメーター

s
ReadOnlySpan<Char>

変換する値を表す文字を含むスパン。

style
NumberStyles

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

provider
IFormatProvider

s の書式設定に関するカルチャ固有の情報を提供するオブジェクト。 providernull の場合は、スレッドの現在のカルチャが使用されます。

戻り値

s に格納されている数値と等価のバイト値。

実装

適用対象

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs

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

public static byte Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Byte

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

戻り値

を解析した utf8Text結果。

実装

適用対象

Parse(String, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

指定されたカルチャ固有の書式で表現された文字列形式の数値を、それと等価の Byte に変換します。

public:
 static System::Byte Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::Byte Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::Byte>::Parse;
public static byte Parse (string s, IFormatProvider provider);
public static byte Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> byte
Public Shared Function Parse (s As String, provider As IFormatProvider) As Byte

パラメーター

s
String

変換する数値を含んだ文字列。 文字列は、Integer スタイルを使用して解釈されます。

provider
IFormatProvider

s に関するカルチャ固有の解析情報を提供するオブジェクト。 providernull の場合は、スレッドの現在のカルチャが使用されます。

戻り値

s に格納されている数値と等価のバイト値。

実装

例外

snullです。

s が正しい形式ではありません。

s は、 Byte.MinValue より小さいか 、Byte.MaxValue より大きい数値を表します。

次の例では、 メソッドを使用して値の Byte 文字列表現を Parse 解析します。

String^ stringToConvert; 
Byte byteValue;

stringToConvert = " 214 ";
try {
   byteValue = Byte::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException^) {
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue); }

stringToConvert = " + 214 ";
try {
   byteValue = Byte::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException^) {
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue); }

stringToConvert = " +214 ";
try {
   byteValue = Byte::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException^) {
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue); }
// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
string stringToConvert;
byte byteValue;

stringToConvert = " 214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }

stringToConvert = " + 214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }

stringToConvert = " +214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }
// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
let stringToConvert = " 214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

let stringToConvert = " + 214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

let stringToConvert = " +214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
Dim stringToConvert As String 
Dim byteValue As Byte

stringToConvert = " 214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  

stringToConvert = " + 214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  

stringToConvert = " +214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try
' The example displays the following output to the console:
'       Converted ' 214 ' to 214.
'       Unable to parse ' + 214 '.
'       Converted ' +214 ' to 214.

注釈

パラメーターには s 、次の形式の数値が含まれています。

[ws][sign]digits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

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

パラメーターは s 、 スタイルを Integer 使用して解釈されます。 バイト値の 10 進数に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 (符号が存在する場合は、正符号である必要があります。または、メソッドによって が OverflowExceptionスローされます)。に存在できるカルチャ固有の書式設定情報と共にスタイル要素を明示的に s定義するには、 メソッドを Byte.Parse(String, NumberStyles, IFormatProvider) 使用します。

パラメーターはs、 によってprovider提供される オブジェクトの書式設定情報をNumberFormatInfo使用して解析されます。 パラメーターはproviderIFormatProviderCultureInfo オブジェクトなどのNumberFormatInfo実装です。 パラメーターは provider 、解析で使用されるカルチャ固有の情報を提供します。 providernull の場合は、スレッドの現在のカルチャが使用されます。

こちらもご覧ください

適用対象

Parse(String, NumberStyles)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

指定のスタイルで表現された数値の文字列形式を、それと等価な Byte に変換します。

public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static byte Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> byte
Public Shared Function Parse (s As String, style As NumberStyles) As Byte

パラメーター

s
String

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

style
NumberStyles

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

戻り値

s に格納されている数値と等価のバイト値。

例外

snullです。

s が正しい形式ではありません。

s は、 Byte.MinValue より小さいか 、Byte.MaxValue より大きい数値を表します。

- または -

s に 0 以外の小数部の桁が含まれています。

styleNumberStyles 値ではありません。

- または -

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

次の例では、 メソッドを使用して値の Byte 文字列表現を Byte.Parse(String, NumberStyles) 解析します。 この例の現在のカルチャは en-US です。

String^ value;
NumberStyles style;
Byte number;

// Parse value with no styles allowed.
style = NumberStyles::None;
value = " 241 ";
try
{
   number = Byte::Parse(value, style);
   Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", value); }   

// Parse value with trailing sign.
style = NumberStyles::Integer | NumberStyles::AllowTrailingSign;
value = " 163+";
number = Byte::Parse(value, style);
Console::WriteLine("Converted '{0}' to {1}.", value, number);

// Parse value with leading sign.
value = "   +253  ";
number = Byte::Parse(value, style);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
string value;
NumberStyles style;
byte number;

// Parse value with no styles allowed.
style = NumberStyles.None;
value = " 241 ";
try
{
   number = Byte.Parse(value, style);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", value); }

// Parse value with trailing sign.
style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
value = " 163+";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);

// Parse value with leading sign.
value = "   +253  ";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
// Parse value with no styles allowed.
let style = NumberStyles.None
let value = " 241 "
try
    let number = Byte.Parse(value, style);
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."

// Parse value with trailing sign.
let style = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
let value = " 163+"
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// Parse value with leading sign.
let value = "   +253  "
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
Dim value As String
Dim style As NumberStyles
Dim number As Byte

' Parse value with no styles allowed.
style = NumberStyles.None
value = " 241 "
Try
   number = Byte.Parse(value, style)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)   
End Try
  
' Parse value with trailing sign.
style = NumberStyles.Integer Or NumberStyles.AllowTrailingSign
value = " 163+"
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)

' Parse value with leading sign.
value = "   +253  "
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' This example displays the following output to the console:
'       Unable to parse ' 241 '.
'       Converted ' 163+' to 163.
'       Converted '   +253  ' to 253.

注釈

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

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

または、 が含まれているAllowHexSpecifier場合styleは 、

[ws]hexdigits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。 空白は、 が フラグを含むNumberStyles.AllowLeadingWhite場合styleは のs先頭に表示され、style に フラグが含まれている場合は s の末尾にNumberStyles.AllowTrailingWhite表示されます。
$ カルチャ固有の通貨記号。 文字列内での位置は、現在の NumberFormatInfo.CurrencyPositivePattern カルチャの プロパティによって定義されます。 に フラグが含まれている場合styleは、現在のカルチャの通貨記号を NumberStyles.AllowCurrencySymbols表示できます。
sign 省略可能な正符号。 (負のOverflowException符号が に存在する場合、 メソッドは をsスローします)。記号は、 が フラグを含むNumberStyles.AllowLeadingSign場合styleは のs先頭に表示され、 の末尾には フラグが含まれている場合styleは の末尾sNumberStyles.AllowTrailingSign表示されます。
数値 0 から 9 までの数字のシーケンス。
. カルチャ固有の小数点記号。 に フラグが含まれている場合styleは、現在のカルチャの小数点記号を NumberStyles.AllowDecimalPoints表示できます。
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 桁のみで構成する必要があります。または が OverflowException スローされます。
NumberStyles.AllowExponent パラメーターでは s 、指数表記を使用することもできます。
NumberStyles.AllowLeadingWhite の先頭sにある ws 要素。
NumberStyles.AllowTrailingWhite の末尾sにある ws 要素。
NumberStyles.AllowLeadingSign 正の符号は 数字の前に表示されます。
NumberStyles.AllowTrailingSign 正符号は 数字の後に表示されます。
NumberStyles.AllowParentheses このフラグはサポートされていますが、 で s かっこを使用すると、 が発生します OverflowException
NumberStyles.AllowThousands ではグループ区切り記号を使用 sできますが、先頭には 1 桁以上の 0 桁しか付けできません。
NumberStyles.AllowCurrencySymbol $ 要素。

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

パラメーターは s 、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 他のカルチャの書式設定情報を使用するには、 オーバーロードを Byte.Parse(String, NumberStyles, IFormatProvider) 呼び出します。

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

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

public:
 static System::Byte Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Byte>::Parse;
public static byte Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> byte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Byte

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

戻り値

を解析した s結果。

実装

適用対象

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs

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

public:
 static System::Byte Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::Byte>::Parse;
public static byte Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> byte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Byte

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

戻り値

を解析した utf8Text結果。

実装

適用対象

Parse(String)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

数値の文字列形式を、それと等価の Byte に変換します。

public:
 static System::Byte Parse(System::String ^ s);
public static byte Parse (string s);
static member Parse : string -> byte
Public Shared Function Parse (s As String) As Byte

パラメーター

s
String

変換する数値を含んだ文字列。 文字列は、Integer スタイルを使用して解釈されます。

戻り値

s に格納されている数値と等価のバイト値。

例外

snullです。

s が正しい形式ではありません。

s は、 Byte.MinValue より小さいか 、Byte.MaxValue より大きい数値を表します。

次の例では、 メソッドを使用して文字列値をバイト値に変換する方法を Byte.Parse(String) 示します。 その後、結果のバイト値がコンソールに表示されます。

String^ stringToConvert = " 162";
Byte byteValue;
try
{
   byteValue = Byte::Parse(stringToConvert);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}   
catch (FormatException^)
{
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException^)
{
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue);
}  
// The example displays the following output to the console:
//       Converted ' 162' to 162.
string stringToConvert = " 162";
byte byteValue;
try
{
   byteValue = Byte.Parse(stringToConvert);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue);
}
// The example displays the following output to the console:
//       Converted ' 162' to 162.
let stringToConvert = " 162"
try
    let byteValue = Byte.Parse stringToConvert
    printfn $"Converted '{stringToConvert}' to {byteValue}."
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."

// The example displays the following output to the console:
//       Converted ' 162' to 162.
Dim stringToConvert As String = " 162"
Dim byteValue As Byte
Try
   byteValue = Byte.Parse(stringToConvert)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  
' The example displays the following output to the console:
'       Converted ' 162' to 162.

注釈

パラメーターには s 、次の形式の数値が含まれています。

[ws][sign]digits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

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

パラメーターは s 、 スタイルを NumberStyles.Integer 使用して解釈されます。 バイト値の 10 進数に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 (符号が存在する場合は、正符号である必要があります。または、メソッドによって が OverflowExceptionスローされます)。に存在できるスタイル要素を明示的に s定義するには、 Byte.Parse(String, NumberStyles) メソッドまたは メソッドを Byte.Parse(String, NumberStyles, IFormatProvider) 使用します。

パラメーターは s 、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 詳細については、「CurrentInfo」を参照してください。 他のカルチャの書式設定情報を使用して文字列を解析するには、 メソッドを使用します Byte.Parse(String, NumberStyles, IFormatProvider)

こちらもご覧ください

適用対象