Byte.Parse Byte.Parse Byte.Parse Byte.Parse Method

定义

将数字的字符串表示形式转换为它的等效 Byte 表示形式。Converts the string representation of a number to its Byte equivalent.

重载

Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) 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) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

将指定样式的数字的字符串表示形式转换为它的等效 ByteConverts the string representation of a number in a specified style to its Byte equivalent.

Parse(String) Parse(String) Parse(String) Parse(String)

将数字的字符串表示形式转换为它的等效 Byte 表示形式。Converts the string representation of a number to its Byte equivalent.

Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider) Parse(String, IFormatProvider)

将指定的区域性特定格式的数字字符串表示形式转换为它的等效 ByteConverts the string representation of a number in a specified culture-specific format to its Byte equivalent.

Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) Parse(String, NumberStyles, IFormatProvider) 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 String String String

包含要转换的数字的字符串。A string that contains a number to convert. 该字符串使用由 style 指定的样式来进行解释。The string is interpreted using the style specified by style.

style
NumberStyles NumberStyles NumberStyles 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 IFormatProvider IFormatProvider 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.

异常

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.

注解

参数定义允许s在参数中执行的样式元素(如空格或正号),以便分析操作成功。 styleThe 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参数包括AllowHexSpecifierOr, 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. s s style NumberStyles.AllowTrailingWhite如果包含style 标志,则空白可以出现在开头;如果包含标志,NumberStyles.AllowLeadingWhite则为末尾处。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位置由provider参数GetFormat方法返回的NumberFormatInfo对象的属性定义。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. 如果s style包含标志,则货币符号可以出现在中。NumberStyles.AllowCurrencySymbolThe currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 可选正号。An optional positive sign. OverflowException如果中s存在负号,该方法将引发。)s s style NumberStyles.AllowTrailingSign如果包含style 标志,则符号可以出现在开头;如果包含标志,NumberStyles.AllowLeadingSign则为末尾处。(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. provider s如果包含NumberStyles.AllowDecimalPoint标志, 则指定的区域性的小数点符号可以出现在中。 styleThe 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.
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位数字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 开头sws元素。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.

如果使用s标志,则必须是不带前缀的十六进制值。 NumberStyles.AllowHexSpecifierIf 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.HexNumber其中包含两个空格标志。) NumberStyles(The NumberStyles enumeration has a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

参数是一个CultureInfo实现,如或对象。 NumberFormatInfo IFormatProvider providerThe 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) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

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

参数

返回

Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) 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 String String String

包含要转换的数字的字符串。A string that contains a number to convert. 该字符串使用由 style 指定的样式来进行解释。The string is interpreted using the style specified by style.

style
NumberStyles NumberStyles NumberStyles 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.

异常

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.            

注解

参数定义允许s在参数中执行的样式元素(如空格或正号),以便分析操作成功。 styleThe 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包括AllowHexSpecifierOr, 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. s NumberStyles.AllowTrailingWhite如果包含style标志,或者如果style包含标志,则空格可以NumberStyles.AllowLeadingWhite出现在的开头。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. 如果s style包含标志,则当前区域性的货币符号可以出现在中。NumberStyles.AllowCurrencySymbolThe current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 可选正号。An optional positive sign. OverflowException如果中s存在负号,该方法将引发。)s s style NumberStyles.AllowTrailingSign如果包含style 标志,则符号可以出现在开头;如果包含标志,NumberStyles.AllowLeadingSign则为末尾处。(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. 如果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.
ee E 或 E 字符,指示值以指数表示法表示。The e or E character, which indicates that the value is represented in exponential notation. 如果s 包含标志NumberStyles.AllowExponent , 则参数可以表示指数表示法中的数字。 styleThe 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位数字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 开头sws元素。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.

如果使用s标志,则必须是不带前缀的十六进制值。 NumberStyles.AllowHexSpecifierIf 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.AllowLeadingWhite和。 NumberStyles.AllowTrailingWhiteThe only other flags that can be combined with it are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (枚举包含复合数字样式, NumberStyles.HexNumber其中包含两个空格标志。) NumberStyles(The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

使用为当前系统区域性初始化的NumberFormatInfo对象中的格式设置信息分析参数。sThe 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) Parse(String) Parse(String) 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 String String 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.

异常

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.

使用样式对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.

使用为当前系统区域性初始化的NumberFormatInfo对象中的格式设置信息分析参数。sThe 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) Parse(String, IFormatProvider) Parse(String, IFormatProvider) 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 String String String

包含要转换的数字的字符串。A string that contains a number to convert. 该字符串使用 Integer 样式来进行解释。The string is interpreted using the Integer style.

provider
IFormatProvider IFormatProvider IFormatProvider 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.

异常

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.

使用样式对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.

使用由NumberFormatInfo s 提供的对象中的格式设置provider信息分析参数。The s parameter is parsed using the formatting information in a NumberFormatInfo object supplied by provider. CultureInfo参数是一个NumberFormatInfo实现,如或对象。 provider IFormatProviderThe 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.

另请参阅

适用于