Byte.Parse 方法

定義

將數字的字串表示,轉換為其相等的 ByteConverts the string representation of a number to its Byte equivalent.

多載

Parse(String, NumberStyles, IFormatProvider)

將數字的字串表示 (使用指定樣式和特定文化特性的格式) 轉換為其相等的 ByteConverts the string representation of a number in a specified style and culture-specific format to its Byte equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式的數字範圍,轉換為其對等的 ByteConverts the span representation of a number in a specified style and culture-specific format to its Byte equivalent.

Parse(String, NumberStyles)

將指定樣式中數字的字串表示轉換為其相等的 ByteConverts the string representation of a number in a specified style to its Byte equivalent.

Parse(String)

將數字的字串表示,轉換為其相等的 ByteConverts the string representation of a number to its Byte equivalent.

Parse(String, IFormatProvider)

將使用指定特定文化特性格式之數字的字串表示轉換為其相等的 ByteConverts the string representation of a number in a specified culture-specific format to its Byte equivalent.

Parse(String, NumberStyles, IFormatProvider)

將數字的字串表示 (使用指定樣式和特定文化特性的格式) 轉換為其相等的 ByteConverts the string representation of a number in a specified style and culture-specific format to its Byte equivalent.

public:
 static System::Byte Parse(System::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

字串,包含要轉換的數字。A string that contains a number to convert. 這個字串使用 style 指定的樣式來解譯。The string is interpreted using the style specified by style.

style
NumberStyles

列舉值的位元組合,表示 s 中可以存在的樣式項目。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 一般會指定的值是 IntegerA typical value to specify is Integer.

provider
IFormatProvider

物件,提供 s 格式的相關特定文化特性資訊。An object that supplies culture-specific information about the format of s. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

傳回

位元組值,該值相當於 s 中包含的數字。A byte value that is equivalent to the number contained in s.

例外狀況

snulls is null.

s 的格式不正確。s is not of the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

-或--or- s 包含非零的小數數字。s includes non-zero, fractional digits.

style 不是 NumberStyles 值。style is not a NumberStyles value.

-或--or- style 不是 AllowHexSpecifierHexNumber 值的組合。style is not a combination of AllowHexSpecifier and HexNumber values.

範例

下列程式碼範例會使用 Byte.Parse(String, NumberStyles, IFormatProvider) 方法的這個多載,剖析 Byte 值的字串表示。The following code example parses string representations of Byte values with this overload of the Byte.Parse(String, NumberStyles, IFormatProvider) method.

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.
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 參數中允許的樣式元素(例如空白字元或正號),使剖析作業成功。The style parameter defines the style elements (such as white space or the positive sign) that are allowed in the s parameter for the parse operation to succeed. 它必須是來自 NumberStyles 列舉的位旗標組合。It must be a combination of bit flags from the NumberStyles enumeration. 根據 style的值,s 參數可能包含下列元素:Depending on the value of style, the s parameter may include the following elements:

atl-ws-01[$][sign] 數位 [. fractional_digits] [e [sign] 數位] [ws][ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

或者,如果 style 參數包含 AllowHexSpecifierOr, if the style parameter includes AllowHexSpecifier:

[ws] hexdigits [ws][ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

項目Element 說明Description
wsws 選擇性的空白字元。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則 ss 開頭會出現空白字元。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 其在字串中的位置是由 provider 參數的 GetFormat 方法所傳回之 NumberFormatInfo 物件的 NumberFormatInfo.CurrencyPositivePattern 屬性所定義。Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern property of the NumberFormatInfo object returned by the GetFormat method of the provider parameter. 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,貨幣符號可能會出現在 s 中。The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正號。An optional positive sign. (如果 s中有負號,方法會擲回 OverflowException)。如果 style 包含 NumberStyles.AllowLeadingSign 旗標,s 或如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則符號會出現在 s 的開頭。(The method throws an OverflowException if a negative sign is present in s.) The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, or at the end of s if style includes the NumberStyles.AllowTrailingSign flag.
digitsdigits 從0到9的數位序列。A sequence of digits from 0 through 9.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,provider 所指定文化特性的小數點符號會出現在 s 中。The decimal point symbol of the culture specified by provider can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digitsfractional_digits 數位0的一或多個出現次數。One or more occurrences of the digit 0. 只有在 style 包含 NumberStyles.AllowDecimalPoint 旗標時,小數位數才會出現在 s 中。Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
ee E 或 E 字元,表示值是以指數標記法表示。The e or E character, which indicates that the value is represented in exponential notation. 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數可以表示指數標記法中的數位。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 從0到 f 的十六進位數位序列,或0到 F。A sequence of hexadecimal digits from 0 through f, or 0 through F.

注意

不論 style 引數的值為何,剖析作業都會忽略 s 中的任何終止 NUL (U + 0000)字元。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只有十進位數(對應至 NumberStyles.None 樣式)的字串一律會成功剖析。A string with decimal digits only (which corresponds to the NumberStyles.None style) always parses successfully. 大部分的其餘 NumberStyles 成員控制元素,這些專案可能是但不需要出現在此輸入字串中。Most of the remaining NumberStyles members control elements that may be but are not required to be present in this input string. 下表指出個別 NumberStyles 成員如何影響 s中可能存在的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合 NumberStyles 值Non-composite NumberStyles values 除了數位之外,s 中允許的元素Elements permitted in s in addition to digits
NumberStyles.None 僅限十進位數。Decimal digits only.
NumberStyles.AllowDecimalPoint .The . fractional_digits元素。and fractional_digits elements. 不過, fractional_digits只能包含一或多個0位數,或擲回 OverflowExceptionHowever, fractional_digits must consist of only one or more 0 digits or an OverflowException is thrown.
NumberStyles.AllowExponent s 參數也可以使用指數標記法。The s parameter can also use exponential notation.
NumberStyles.AllowLeadingWhite s開頭的ws元素。The ws element at the beginning of s.
NumberStyles.AllowTrailingWhite s結尾的ws元素。The ws element at the end of s.
NumberStyles.AllowLeadingSign 正號可以出現在數位前面。A positive sign can appear before digits.
NumberStyles.AllowTrailingSign 正號可以在數位之後出現。A positive sign can appear after digits.
NumberStyles.AllowParentheses 雖然支援此旗標,但在 s 中使用括弧會導致 OverflowExceptionAlthough this flag is supported, the use of parentheses in s results in an OverflowException.
NumberStyles.AllowThousands 雖然群組分隔符號可以出現在 s中,但前面只能加上一或多個0位數。Although the group separator symbol can appear in s, it can be preceded by only one or more 0 digits.
NumberStyles.AllowCurrencySymbol $ 元素。The $ element.

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是不含前置詞的十六進位值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value without a prefix. 例如,"F3" 會成功剖析,但 "0xF3" 則不會。For example, "F3" parses successfully, but "0xF3" does not. style 中唯一可以出現的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be present in style are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. NumberStyles 列舉具有複合數位樣式,NumberStyles.HexNumber,其中同時包含空白字元旗標)。(The NumberStyles enumeration has a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

provider 參數是 IFormatProvider 的執行,例如 NumberFormatInfoCultureInfo 物件。The provider parameter is an IFormatProvider implementation, such as a NumberFormatInfo or CultureInfo object. provider 參數提供用於剖析的特定文化特性資訊。The provider parameter supplies culture-specific information used in parsing. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

另請參閱

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式的數字範圍,轉換為其對等的 ByteConverts the span representation of a number in a specified style and culture-specific format to its Byte equivalent.

public static byte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = null);
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 = null) As Byte

參數

s
ReadOnlySpan<Char>

範圍,包含代表要轉換之值的字元。A span containing the characters representing the value to convert.

style
NumberStyles

列舉值的位元組合,表示 s 中可以存在的樣式項目。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 一般會指定的值是 IntegerA typical value to specify is Integer.

provider
IFormatProvider

物件,提供 s 格式的相關特定文化特性資訊。An object that supplies culture-specific information about the format of s. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

傳回

位元組值,該值相當於 s 中包含的數字。A byte value that is equivalent to the number contained in s.

Parse(String, NumberStyles)

將指定樣式中數字的字串表示轉換為其相等的 ByteConverts the string representation of a number in a specified style to its Byte equivalent.

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

字串,包含要轉換的數字。A string that contains a number to convert. 這個字串使用 style 指定的樣式來解譯。The string is interpreted using the style specified by style.

style
NumberStyles

列舉值的位元組合,表示 s 中可以存在的樣式項目。A bitwise combination of enumeration values that indicates the style elements that can be present in s. 一般會指定的值是 IntegerA typical value to specify is Integer.

傳回

位元組值,該值相當於 s 中包含的數字。A byte value that is equivalent to the number contained in s.

例外狀況

snulls is null.

s 的格式不正確。s is not of the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

-或--or- s 包含非零的小數數字。s includes non-zero, fractional digits.

style 不是 NumberStyles 值。style is not a NumberStyles value.

-或--or- style 不是 AllowHexSpecifierHexNumber 值的組合。style is not a combination of AllowHexSpecifier and HexNumber values.

範例

下列範例會使用 Byte.Parse(String, NumberStyles) 方法來剖析 Byte 值的字串表示。The following example parses string representations of Byte values with the Byte.Parse(String, NumberStyles) method. 此範例的目前文化特性為 en-us。The current culture for the example is 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.            
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 參數中允許的樣式元素(例如空白字元或正號),使剖析作業成功。The style parameter defines the style elements (such as white space or the positive sign) that are allowed in the s parameter for the parse operation to succeed. 它必須是來自 NumberStyles 列舉的位旗標組合。It must be a combination of bit flags from the NumberStyles enumeration. 根據 style的值,s 參數可能包含下列元素:Depending on the value of style, the s parameter may include the following elements:

atl-ws-01[$][sign] 數位 [. fractional_digits] [e [sign] 數位] [ws][ws][$][sign]digits[.fractional_digits][e[sign]digits][ws]

或者,如果 style 包含 AllowHexSpecifierOr, if style includes AllowHexSpecifier:

[ws] hexdigits [ws][ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

項目Element 說明Description
wsws 選擇性的空白字元。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則空白字元會出現在 s 的開頭。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 其在字串中的位置是由目前文化特性的 NumberFormatInfo.CurrencyPositivePattern 屬性所定義。Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern property of the current culture. 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可能會出現在 s 中。The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正號。An optional positive sign. (如果 s中有負號,方法會擲回 OverflowException)。如果 style 包含 NumberStyles.AllowLeadingSign 旗標,s 或如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則符號會出現在 s 的開頭。(The method throws an OverflowException if a negative sign is present in s.) The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, or at the end of s if style includes the NumberStyles.AllowTrailingSign flag.
digitsdigits 從0到9的數位序列。A sequence of digits from 0 through 9.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digitsfractional_digits 數位0的一或多個出現次數。One or more occurrences of the digit 0. 只有在 style 包含 NumberStyles.AllowDecimalPoint 旗標時,小數位數才會出現在 s 中。Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
ee E 或 E 字元,表示值是以指數標記法表示。The e or E character, which indicates that the value is represented in exponential notation. 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數可以表示指數標記法中的數位。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 從0到 f 的十六進位數位序列,或0到 F。A sequence of hexadecimal digits from 0 through f, or 0 through F.

注意

不論 style 引數的值為何,剖析作業都會忽略 s 中的任何終止 NUL (U + 0000)字元。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只有十進位數(對應至 NumberStyles.None 樣式)的字串一律會成功剖析。A string with decimal digits only (which corresponds to the NumberStyles.None style) always parses successfully. 大部分的其餘 NumberStyles 成員控制元素,這些專案可能是但不需要出現在此輸入字串中。Most of the remaining NumberStyles members control elements that may be but are not required to be present in this input string. 下表指出個別 NumberStyles 成員如何影響 s中可能存在的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合 NumberStyles 值Non-composite NumberStyles values 除了數位之外,s 中允許的元素Elements permitted in s in addition to digits
NumberStyles.None 僅限十進位數。Decimal digits only.
NumberStyles.AllowDecimalPoint .The . fractional_digits元素。and fractional_digits elements. 不過, fractional_digits只能包含一或多個0位數,或擲回 OverflowExceptionHowever, fractional_digits must consist of only one or more 0 digits or an OverflowException is thrown.
NumberStyles.AllowExponent s 參數也可以使用指數標記法。The s parameter can also use exponential notation.
NumberStyles.AllowLeadingWhite s開頭的ws元素。The ws element at the beginning of s.
NumberStyles.AllowTrailingWhite s結尾的ws元素。The ws element at the end of s.
NumberStyles.AllowLeadingSign 正號可以出現在數位前面。A positive sign can appear before digits.
NumberStyles.AllowTrailingSign 正號可以在數位之後出現。A positive sign can appear after digits.
NumberStyles.AllowParentheses 雖然支援此旗標,但在 s 中使用括弧會導致 OverflowExceptionAlthough this flag is supported, the use of parentheses in s results in an OverflowException.
NumberStyles.AllowThousands 雖然群組分隔符號可以出現在 s中,但前面只能加上一或多個0位數。Although the group separator symbol can appear in s, it can be preceded by only one or more 0 digits.
NumberStyles.AllowCurrencySymbol $ 元素。The $ element.

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是不含前置詞的十六進位值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value without a prefix. 例如,"F3" 會成功剖析,但 "0xF3" 則不會。For example, "F3" parses successfully, but "0xF3" does not. 唯一可以搭配使用的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with it are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. NumberStyles 列舉包含複合數位樣式,NumberStyles.HexNumber,其中同時包含空白字元旗標)。(The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

s 參數會使用針對目前系統文化特性初始化之 NumberFormatInfo 物件中的格式設定資訊來進行剖析。The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. 若要使用一些其他文化特性的格式資訊,請呼叫 Byte.Parse(String, NumberStyles, IFormatProvider) 多載。To use the formatting information of some other culture, call the Byte.Parse(String, NumberStyles, IFormatProvider) overload.

另請參閱

Parse(String)

將數字的字串表示,轉換為其相等的 ByteConverts the string representation of a number to its Byte equivalent.

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

字串,包含要轉換的數字。A string that contains a number to convert. 這個字串使用 Integer 樣式來解譯。The string is interpreted using the Integer style.

傳回

位元組值,該值相當於 s 中包含的數字。A byte value that is equivalent to the number contained in s.

例外狀況

snulls is null.

s 的格式不正確。s is not of the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

範例

下列範例示範如何使用 Byte.Parse(String) 方法,將字串值轉換成 byte 值。The following example demonstrates how to convert a string value into a byte value using the Byte.Parse(String) method. 產生的位元組值接著會顯示在主控台中。The resulting byte value is then displayed to the console.

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.         
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 參數包含下列格式的數位:The s parameter contains a number of the form:

atl-ws-01[sign] 數位 [ws][ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

項目Element 說明Description
wsws 選擇性的空白字元。Optional white space.
簽署sign 選擇性的正或負號。An optional positive or negative sign.
digitsdigits 範圍從0到9的數位序列。A sequence of digits ranging from 0 to 9.

s 參數會使用 NumberStyles.Integer 樣式來加以解讀。The s parameter is interpreted using the NumberStyles.Integer style. 除了位元組值的十進位數以外,只允許開頭和尾端空格加上前置正負號。In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (如果符號存在,則必須是正號,否則方法會擲回 OverflowException)。若要明確定義 s中可以存在的樣式元素,請使用 Byte.Parse(String, NumberStyles)Byte.Parse(String, NumberStyles, IFormatProvider) 方法。(If the sign is present, it must be a positive sign or the method throws an OverflowException.) To explicitly define the style elements that can be present in s, use either the Byte.Parse(String, NumberStyles) or the Byte.Parse(String, NumberStyles, IFormatProvider) method.

s 參數會使用針對目前系統文化特性初始化之 NumberFormatInfo 物件中的格式設定資訊來進行剖析。The s parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. 如需詳細資訊,請參閱CurrentInfoFor more information, see CurrentInfo. 若要使用一些其他文化特性的格式資訊來剖析字串,請使用 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。To parse a string using the formatting information of some other culture, use the Byte.Parse(String, NumberStyles, IFormatProvider) method.

另請參閱

Parse(String, IFormatProvider)

將使用指定特定文化特性格式之數字的字串表示轉換為其相等的 ByteConverts the string representation of a number in a specified culture-specific format to its Byte equivalent.

public:
 static System::Byte Parse(System::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

字串,包含要轉換的數字。A string that contains a number to convert. 這個字串使用 Integer 樣式來解譯。The string is interpreted using the Integer style.

provider
IFormatProvider

物件,提供 s 的相關特定文化特性剖析資訊。An object that supplies culture-specific parsing information about s. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

傳回

位元組值,該值相當於 s 中包含的數字。A byte value that is equivalent to the number contained in s.

例外狀況

snulls is null.

s 的格式不正確。s is not of the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

範例

下列範例會使用 Parse 方法來剖析 Byte 值的字串表示。The following example parses string representations of Byte values with the Parse method.

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.
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 參數包含下列格式的數位:The s parameter contains a number of the form:

atl-ws-01[sign] 數位 [ws][ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

項目Element 說明Description
wsws 選擇性的空白字元。Optional white space.
簽署sign 選擇性的正號。An optional positive sign.
digitsdigits 範圍從0到9的數位序列。A sequence of digits ranging from 0 to 9.

s 參數會使用 Integer 樣式來加以解讀。The s parameter is interpreted using the Integer style. 除了位元組值的十進位數以外,只允許開頭和尾端空格加上前置正負號。In addition to the byte value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. (如果符號存在,則必須是正號,否則方法會擲回 OverflowException)。若要明確定義樣式專案,以及可在 s中顯示的特定文化特性格式資訊,請使用 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。(If the sign is present, it must be a positive sign or the method throws an OverflowException.) To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Byte.Parse(String, NumberStyles, IFormatProvider) method.

s 參數會使用 provider所提供之 NumberFormatInfo 物件中的格式資訊進行剖析。The s parameter is parsed using the formatting information in a NumberFormatInfo object supplied by provider. provider 參數是 IFormatProvider 的執行,例如 NumberFormatInfoCultureInfo 物件。The provider parameter is an IFormatProvider implementation such as a NumberFormatInfo or CultureInfo object. provider 參數提供用於剖析的特定文化特性資訊。The provider parameter supplies culture-specific information used in parsing. 如果 providernull,則會使用執行緒目前的文化特性。If provider is null, the thread current culture is used.

另請參閱

適用於