Byte.Parse 方法

定义

将数字的字符串表示形式转换为它的等效 Byte 表示形式。Converts 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)

将数字的字符串表示形式转换为它的等效 Byte 表示形式。Converts 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:

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

如果 style 参数包括 AllowHexSpecifier,则为; 否则为:Or, if the style parameter includes AllowHexSpecifier:

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

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,s 或者如果 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. 其在字符串中的位置由 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.
signsign 可选正号。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 = 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 = 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:

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

如果 style 包括 AllowHexSpecifier,则为; 否则为:Or, if style includes AllowHexSpecifier:

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

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,或者如果样式包含 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.
signsign 可选正号。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)

将数字的字符串表示形式转换为它的等效 Byte 表示形式。Converts 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) 方法将字符串值转换为字节值。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:

[ws][sign]digits[ws][ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space.
signsign 可选的正号或负号。An optional positive or negative sign.
digitsdigits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9.

使用 NumberStyles.Integer 样式对 s 参数进行解释。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:

[ws][sign]digits[ws][ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space.
signsign 可选正号。An optional positive sign.
digitsdigits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9.

使用 Integer 样式对 s 参数进行解释。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.

另请参阅

适用于