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)

來源:
Byte.cs
來源:
Byte.cs
來源:
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 包含非零的小數數字。

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

範例

下列程式碼範例會使用 方法的 ByteByte.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 位旗標組合。 根據 的值 styles 參數可能包含下列元素:

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

或者,如果 style 參數包含 AllowHexSpecifier

[ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。 如果包含 旗標,則空白字元可以出現在 的 s 開頭,如果包含 旗標, NumberStyles.AllowTrailingWhite 則會出現 style 在 結尾 sNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 字串中的位置是由 NumberFormatInfo.CurrencyPositivePattern 參數的 方法 provider 所傳回之 物件的 屬性 NumberFormatInfoGetFormat 定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 貨幣符號就可以出現在 中 s
簽署 選擇性正負號。 (如果 .) 中存在負號,則方法會擲 OverflowException 回 。如果 style 包含 旗標,則表示此符號可以出現在 的開頭 s ,或如果 style 包含 NumberStyles.AllowLeadingSignNumberStyles.AllowTrailingSign 旗標則出現在 結尾 ss
數字 從 0 到 9 的數位序列。
. 特定文化特性的小數點符號。 如果包含 旗標,則 所 provider 指定文化特性的小數點符號可以出現在 中 sNumberStyles.AllowDecimalPointstyle
fractional_digits 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s
e e 或 E 字元,表示值是以指數標記法表示。 如果 style 包含 旗標,s 參數可以表示指數標記法的數位 NumberStyles.AllowExponent
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論引數的值 style 為何,剖析作業都會忽略 中 s 任何終止的 NUL (U+0000) 字元。

只有十進位數的字串, (對應至 NumberStyles.None 樣式) 一律會成功剖析。 大部分剩餘 NumberStyles 的成員控制項元素可能存在,但不需要存在於此輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s 可能存在的專案。

非複合 NumberStyles 值 除了數位之外,也允許的專案
NumberStyles.None 僅限十進位數。
NumberStyles.AllowDecimalPoint fractional_digits專案。 不過, fractional_digits 必須只包含一或多個 0 位數或 OverflowException 擲回 。
NumberStyles.AllowExponent 參數 s 也可以使用指數標記法。
NumberStyles.AllowLeadingWhite 開頭的 sws元素。
NumberStyles.AllowTrailingWhite 結尾處的 sws元素。
NumberStyles.AllowLeadingSign 正負號可以出現在 數位之前。
NumberStyles.AllowTrailingSign 正負號可以出現在 數位之後。
NumberStyles.AllowParentheses 雖然支援此旗標,但是使用括弧 s 會導致 。 OverflowException
NumberStyles.AllowThousands 雖然群組分隔符號符號可以出現在 中 s ,但前面只能有一或多個 0 位數。
NumberStyles.AllowCurrencySymbol $ 項目。

NumberStyles.AllowHexSpecifier如果使用旗標, s 必須是不含前置詞的十六進位值。 例如,「F3」 會成功剖析,但 「0xF3」 則不會。 唯一可以出現在 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 具有複合編號樣式, NumberStyles.HexNumber 其中包含兩個空白字元旗標。)

參數 provider 是實作 IFormatProvider ,例如 NumberFormatInfoCultureInfo 物件。 參數 provider 提供剖析中使用的文化特性特定資訊。 如果 providernull,則會使用執行緒目前的文化特性。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
Byte.cs
來源:
Byte.cs
來源:
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)

來源:
Byte.cs
來源:
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)

來源:
Byte.cs
來源:
Byte.cs
來源:
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的數位。

範例

下列範例會使用 Parse 方法剖析值的字串表示 Byte

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 選擇性空白字元。
簽署 選擇性的正負號。
數字 範圍從 0 到 9 的數位序列。

參數 s 會使用 Integer 樣式來解譯。 除了位元組值的十進位數之外,只允許前置和尾端空格與前置符號。 (如果符號存在,它必須是正負號,否則方法會擲回 OverflowException .) 若要明確地定義樣式專案以及可以存在於 中的 s 特定文化特性格式資訊,請使用 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

參數 s 是使用 所 provider 提供之 物件中的 NumberFormatInfo 格式資訊進行剖析。 參數 providerIFormatProviderCultureInfo 物件之類的 NumberFormatInfo 實作。 參數 provider 提供剖析中使用的特定文化特性資訊。 如果 providernull,則會使用執行緒目前的文化特性。

另請參閱

適用於

Parse(String, NumberStyles)

來源:
Byte.cs
來源:
Byte.cs
來源:
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 包含非零的小數數字。

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

範例

下列範例會使用 Byte.Parse(String, NumberStyles) 方法剖析值的字串表示 Byte 。 此範例的目前文化特性為 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 位旗標組合。 根據 的值 styles 參數可能包含下列元素:

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

或者,如果 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,則會在 開頭 s 出現空白字元,如果樣式包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由 NumberFormatInfo.CurrencyPositivePattern 目前文化特性的 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可能會出現在 中 s
簽署 選擇性的正負號。 (如果 .) 中 s 出現負號,則方法會擲 OverflowException 回 ;如果 style 包含 旗標,則表示符號可能會出現在 的開頭 s ,如果 sstyle 包含 NumberStyles.AllowLeadingSignNumberStyles.AllowTrailingSign 旗標,則會在 結尾擲回 。
數字 從 0 到 9 的數位序列。
. 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前的文化特性小數點符號可能會出現在 中 s
fractional_digits 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s
e e 或 E 字元,表示以指數標記法表示值。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 s 可以代表指數標記法的數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

中任何終止的 NUL (U+0000) 字元 s 都會被剖析作業忽略,不論引數的值 style 為何。

只有十進位數的字串 (對應至 NumberStyles.None 樣式) 一律會成功剖析。 大部分的其餘 NumberStyles 成員控制項專案可能存在,但不需要出現在這個輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s 可能存在的專案。

非複合 NumberStyles 值 除了數位之外,還允許的專案
NumberStyles.None 僅限十進位數。
NumberStyles.AllowDecimalPoint fractional_digits專案。 不過, fractional_digits 必須只包含一或多個 0 位數,否則 OverflowException 會擲回 。
NumberStyles.AllowExponent 參數 s 也可以使用指數標記法。
NumberStyles.AllowLeadingWhite 開頭的 sws元素。
NumberStyles.AllowTrailingWhite 結尾處的 sws專案。
NumberStyles.AllowLeadingSign 正負號可能會出現在 數位之前。
NumberStyles.AllowTrailingSign 正負號可能會出現在 數位之後。
NumberStyles.AllowParentheses 雖然支援此旗標,但使用括弧 s 會導致 OverflowException
NumberStyles.AllowThousands 雖然群組分隔符號符號可以出現在 中 s ,但前面只能有一或多個 0 位數。
NumberStyles.AllowCurrencySymbol $ 項目。

NumberStyles.AllowHexSpecifier如果使用 旗標, s 必須是不含前置詞的十六進位值。 例如,「F3」 會成功剖析,但 「0xF3」 則不會。 唯一可以與其結合的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 包含複合編號樣式 , NumberStyles.HexNumber 其中包含兩個空白字元旗標。)

參數 s 會使用針對目前系統文化特性初始化的 物件中的 NumberFormatInfo 格式資訊進行剖析。 若要使用其他文化特性的格式資訊,請呼叫 Byte.Parse(String, NumberStyles, IFormatProvider) 多載。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
Byte.cs
來源:
Byte.cs
來源:
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)

來源:
Byte.cs
來源:
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)

來源:
Byte.cs
來源:
Byte.cs
來源:
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 選擇性空白字元。
簽署 選擇性的正負號。
數字 範圍從 0 到 9 的數位序列。

參數 s 會使用 NumberStyles.Integer 樣式來解譯。 除了位元組值的十進位數之外,只允許前置和尾端空格與前置符號。 (如果符號存在,它必須是正負號,否則方法會擲回 OverflowException .) 若要明確定義可以存在於 中的 s 樣式專案,請使用 Byte.Parse(String, NumberStyles)Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

參數 s 會使用針對目前系統文化特性初始化的 物件中的 NumberFormatInfo 格式資訊進行剖析。 如需詳細資訊,請參閱CurrentInfo。 若要使用其他文化特性的格式資訊剖析字串,請使用 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

另請參閱

適用於