共用方式為


Byte.Parse 方法

定義

將數字的字串表示轉換為其 Byte 等價物。

多載

名稱 Description
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.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的文化特定資訊的物件。 若 provider 為 , null則使用執行緒當前文化。

傳回

一個位元組值,等同於 所 s包含的數字。

實作

例外狀況

snull

s 格式不正確。

s 代表一個小於 Byte.MinValue 或大於 Byte.MaxValue 的數字。

-或-

s 包含非零的分數數字。

style 不是一個 NumberStyles 數值。

-或-

style不是 和 HexNumber 的組合AllowHexSpecifier

範例

以下程式碼範例解析了利用此方法過載Byte.Parse(String, NumberStyles, IFormatProvider)的字串表示Byte值。

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]六角數[ws]

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

元素 說明
ws 選擇性的空格符。 如果包含旗幟,則可在 的s開頭出現空白,或在包含旗幟時出現在結尾NumberStyles.AllowTrailingWhitestylesNumberStyles.AllowLeadingWhitestyle
$ 一種文化特定的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyPositivePattern參數方法NumberFormatInfo回傳的GetFormat物件性質provider所定義。 貨幣s符號可出現style在包含NumberStyles.AllowCurrencySymbol旗幟時。
簽署 一個可選的正面訊號。 (若 中s存在負號,則拋出 。OverflowException)標誌可以出現在 如果s包含旗幟的話開頭,或在包含旗幟的話結尾stylesNumberStyles.AllowTrailingSignNumberStyles.AllowLeadingSignstyle
數字 一串從0到9的數字序列。
. 一種文化特定的小數點符號。 provider若包含s旗幟,則可出現在 中styleNumberStyles.AllowDecimalPoint
fractional_digits 數字 0 出現一次或多次。 只有當 s 包含該旗幟時style,小數數字才能出現NumberStyles.AllowDecimalPoint在 中。
e e 或 E 字元,表示該值以指數符號表示。 若 style 包含 NumberStyles.AllowExponent 旗標,s 參數可用指數符號表示數字。
六角位數 一串從0到f,或0到F的十六進位數字序列。

備註

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有十進位數字的字串(對應 NumberStyles.None 於樣式)總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不強制存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

非合成 NumberStyles 值 除了數字外,還允許在 s 中加入元素
NumberStyles.None 只用小數數字。
NumberStyles.AllowDecimalPoint 以及 以及 fractional_digits 元素。 然而, fractional_digits 必須只包含一個或多個0,否則會擲出一個 OverflowException
NumberStyles.AllowExponent 參數 s 也可以使用指數符號。
NumberStyles.AllowLeadingWhite 開頭sw 元素。
NumberStyles.AllowTrailingWhite 末尾s 元素。
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 實作,例如 a NumberFormatInfoCultureInfo 物件。 參數 provider 提供解析中所需的特定文化資訊。 若 provider 為 , null則使用執行緒當前文化。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
Byte.cs
來源:
Byte.cs
來源:
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的文化特定資訊的物件。 若 provider 為 , null則使用執行緒當前文化。

傳回

一個位元組值,等同於 所 s包含的數字。

實作

適用於

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

一個包含一個需要轉換的數字的字串。 字串會依 style Integer 來詮釋。

provider
IFormatProvider

一個提供針對 文化特定解析資訊的 s物件。 若 provider 為 , null則使用執行緒當前文化。

傳回

一個位元組值,等同於 所 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.
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][手語]數字[ws]

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

元素 說明
ws 選擇性的空格符。
簽署 一個可選的正面訊號。
數字 一串從0到9的數字序列。

s參數是透過 Integer style 來解釋的。 除了位元組值的十進位數字外,僅允許前置與後置空格及引號。 (若符號存在,則必須為正符號,否則方法會拋出 。 OverflowException)若要明確定義風格元素及可存在 s的文化特定格式資訊,請使用該 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

s參數是利用物件中的格式資訊NumberFormatInfo進行解析,該資訊由 provider提供。 參數 provider 是一個 IFormatProvider 實作,例如 a NumberFormatInfoCultureInfo 物件。 參數 provider 提供解析中所需的特定文化資訊。 若 provider 為 , null則使用執行緒當前文化。

另請參閱

適用於

Parse(String, NumberStyles)

來源:
Byte.cs
來源:
Byte.cs
來源:
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不是 和 HexNumber 的組合AllowHexSpecifier

範例

以下範例解析使用此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.
// 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]

或,若 style 包含 AllowHexSpecifier

[ws]六角數[ws]

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

元素 說明
ws 選擇性的空格符。 如果style包含NumberStyles.AllowLeadingWhite旗幟,則可在 的s開頭顯示空白;若風格包含NumberStyles.AllowTrailingWhite旗幟,則可在 的結尾出現空白。
$ 一種文化特定的貨幣符號。 它在字串中的位置由 NumberFormatInfo.CurrencyPositivePattern 當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。
簽署 一個可選的正面訊號。 (若 中s存在負號,則拋出 。OverflowException)標誌可以出現在 如果s包含旗幟的話開頭,或在包含旗幟的話結尾stylesNumberStyles.AllowTrailingSignNumberStyles.AllowLeadingSignstyle
數字 一串從0到9的數字序列。
. 一種文化特定的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。
fractional_digits 數字 0 出現一次或多次。 只有當 s 包含該旗幟時style,小數數字才能出現NumberStyles.AllowDecimalPoint在 中。
e e 或 E 字元,表示該值以指數符號表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
六角位數 一串從0到f,或0到F的十六進位數字序列。

備註

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有十進位數字的字串(對應 NumberStyles.None 於樣式)總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不強制存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

非合成 NumberStyles 值 除了數字外,還允許在 s 中加入元素
NumberStyles.None 只用小數數字。
NumberStyles.AllowDecimalPoint 以及 以及 fractional_digits 元素。 然而, fractional_digits 必須只包含一個或多個0,否則會擲出一個 OverflowException
NumberStyles.AllowExponent 參數 s 也可以使用指數符號。
NumberStyles.AllowLeadingWhite 開頭sw 元素。
NumberStyles.AllowTrailingWhite 末尾s 元素。
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) overload。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

一個包含一個需要轉換的數字的字串。 字串會依 style 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.
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][手語]數字[ws]

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

元素 說明
ws 選擇性的空格符。
簽署 一個可選的正面或負面符號。
數字 一串從0到9的數字序列。

s參數是透過 NumberStyles.Integer style 來解釋的。 除了位元組值的十進位數字外,僅允許前置與後置空格及引號。 (若符號存在,則必須為正符號,否則方法會拋出 。 OverflowException)要明確定義可以存在 s於 中的樣式元素,請使用 該 Byte.Parse(String, NumberStyles) 方法或 方法 Byte.Parse(String, NumberStyles, IFormatProvider)

s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 如需詳細資訊,請參閱CurrentInfo。 若要解析字串,使用其他文化的格式資訊,請使用該 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

另請參閱

適用於