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);
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.

傳回

Byte

位元組值,該值相當於 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 Byte.Parse(String, NumberStyles, IFormatProvider)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. 它必須是來自列舉的位旗標組合 NumberStylesIt must be a combination of bit flags from the NumberStyles enumeration. 視的值而定 styles 參數可能包含下列元素: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. s如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或 s 如果 style 包含旗標, NumberStyles.AllowTrailingWhite 則空格的開頭可能會出現空白字元。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 參數方法所傳回之物件的屬性所定義 NumberFormatInfo GetFormat providerIts position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern property of the NumberFormatInfo object returned by the GetFormat method of the provider parameter. s如果包含旗標,貨幣符號可以出現在中 style NumberStyles.AllowCurrencySymbolThe currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正號。An optional positive sign. (如果中有負號,方法會擲回 OverflowException s ) 。如果 s style 包含旗標 NumberStyles.AllowLeadingSigns style NumberStyles.AllowTrailingSign 則此方法會在開頭出現,如果包含旗標,則會出現在結尾。(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.
數字digits 從0到9的數位序列。A sequence of digits from 0 through 9.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果包含旗標,則所指定之文化特性的小數點符號 provider 可以出現在中 s style NumberStyles.AllowDecimalPointThe 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. 只有在包含旗標時,小數位數才可以出現在中 s style NumberStyles.AllowDecimalPointFractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
pci-ee 表示值以指數標記法表示的 e 或 E 字元。The e or E character, which indicates that the value is represented in exponential notation. 如果包含旗標,s 參數可以表示指數標記法中的數位 style NumberStyles.AllowExponentThe 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.

注意

s無論引數的值為何,剖析作業都會忽略中的任何終止 NUL (U + 0000) 字元 styleAny 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 影響可能存在的元素 sThe following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合 NumberStyles 值Non-composite NumberStyles values 除了數位之外,還允許的元素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位數,否則 OverflowException 會擲回。However, 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 開頭的 ws 元素 sThe ws element at the beginning of s.
NumberStyles.AllowTrailingWhite 結尾的 ws 元素 sThe 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. 中唯一可以存在的其他旗標 styleNumberStyles.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 = 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>

範圍,其包含代表所要轉換值的字元。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.

傳回

Byte

位元組值,該值相當於 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.

傳回

Byte

位元組值,該值相當於 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 Byte.Parse(String, NumberStyles)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. 它必須是來自列舉的位旗標組合 NumberStylesIt must be a combination of bit flags from the NumberStyles enumeration. 視的值而定 styles 參數可能包含下列元素: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. s如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或如果樣式包含旗標,則空格的開頭可能會出現空白字元 NumberStyles.AllowTrailingWhiteWhite 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. s如果包含旗標,則目前文化特性的貨幣符號可以出現在中 style NumberStyles.AllowCurrencySymbolThe current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正號。An optional positive sign. (如果中有負號,方法會擲回 OverflowException s ) 。如果 s style 包含旗標 NumberStyles.AllowLeadingSigns style NumberStyles.AllowTrailingSign 則此方法會在開頭出現,如果包含旗標,則會出現在結尾。(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.
數字digits 從0到9的數位序列。A sequence of digits from 0 through 9.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. s如果包含旗標,則目前文化特性的小數點符號可以出現在中 style NumberStyles.AllowDecimalPointThe 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. 只有在包含旗標時,小數位數才可以出現在中 s style NumberStyles.AllowDecimalPointFractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
pci-ee 表示值以指數標記法表示的 e 或 E 字元。The e or E character, which indicates that the value is represented in exponential notation. s如果包含旗標,則參數可以表示指數標記法中的數位 style NumberStyles.AllowExponentThe 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.

注意

s無論引數的值為何,剖析作業都會忽略中的任何終止 NUL (U + 0000) 字元 styleAny 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 影響可能存在的元素 sThe following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合 NumberStyles 值Non-composite NumberStyles values 除了數位之外,還允許的元素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位數,否則 OverflowException 會擲回。However, 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 開頭的 ws 元素 sThe ws element at the beginning of s.
NumberStyles.AllowTrailingWhite 結尾的 ws 元素 sThe 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.

傳回

Byte

位元組值,該值相當於 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)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.
數字digits 範圍從0到9的數位序列。A sequence of digits ranging from 0 to 9.

s使用樣式來解讀參數 NumberStyles.IntegerThe 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);
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.

傳回

Byte

位元組值,該值相當於 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 ParseThe 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.
數字digits 範圍從0到9的數位序列。A sequence of digits ranging from 0 to 9.

s使用樣式來解讀參數 IntegerThe 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參數會使用提供的物件中的格式資訊進行剖析 NumberFormatInfo providerThe s parameter is parsed using the formatting information in a NumberFormatInfo object supplied by provider. provider參數是 IFormatProvider 或物件這類的實 NumberFormatInfo 作為 CultureInfoThe 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.

另請參閱

適用於