通过


Byte.Parse 方法

定义

将数字的字符串表示形式转换为其 Byte 等效形式。

重载

名称 说明
Parse(String, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的字符串表示形式转换为其 Byte 等效格式。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

将指定样式和区域性特定格式的数字的跨度表示形式转换为其 Byte 等效格式。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

将 UTF-8 字符的范围分析为值。

Parse(String, IFormatProvider)

将指定区域性特定格式的数字的字符串表示形式转换为其 Byte 等效格式。

Parse(String, NumberStyles)

将指定样式中的数字的字符串表示形式转换为其 Byte 等效形式。

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符的范围分析为值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

将 UTF-8 字符的范围分析为值。

Parse(String)

将数字的字符串表示形式转换为其 Byte 等效形式。

Parse(String, NumberStyles, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将指定样式和区域性特定格式的数字的字符串表示形式转换为其 Byte 等效格式。

public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Byte>::Parse;
public static byte Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static byte Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Byte

参数

s
String

包含要转换的数字的字符串。 使用指定的 style样式解释字符串。

style
NumberStyles

枚举值的按位组合,指示可在其中 s存在的样式元素。 要指定 Integer的典型值为 。

provider
IFormatProvider

一个对象,提供有关格式 s的特定于区域性的信息。 null如果是provider,则使用线程当前区域性。

返回

与中包含的 s数字等效的字节值。

实现

例外

snull

s 格式不正确。

s 表示小于 Byte.MinValue 或大于 Byte.MaxValue 的数字。

-或-

s 包括非零的小数位数。

style 不是值 NumberStyles

-或-

style不是值的组合AllowHexSpecifierHexNumber

示例

下面的代码示例使用此方法重Byte.Parse(String, NumberStyles, IFormatProvider)载分析值的字符串表示形式Byte

NumberStyles style;
CultureInfo culture;
string value;
byte number;

// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
value = "12,000";

number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);

culture = CultureInfo.CreateSpecificCulture("en-GB");
try
{
   number = Byte.Parse(value, style, culture);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", value); }

value = "12.000";
number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
let style = NumberStyles.Float
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
let value = "12,000"

let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

let culture = CultureInfo.CreateSpecificCulture "en-GB"
try
    let number = Byte.Parse(value, style, culture)
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."

let value = "12.000"
let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
Dim style As NumberStyles
Dim culture As CultureInfo
Dim value As String
Dim number As Byte

' Parse number with decimals.
' NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float       
culture = CultureInfo.CreateSpecificCulture("fr-FR")
value = "12,000"

number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)

culture = CultureInfo.CreateSpecificCulture("en-GB")
Try
   number = Byte.Parse(value, style, culture)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)   
End Try      

value = "12.000"
number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' The example displays the following output to the console:
'       Converted '12,000' to 12.
'       Unable to parse '12,000'.
'       Converted '12.000' to 12.

注解

style 参数定义允许在 s 参数中成功执行分析操作的样式元素(如空格或正号)。 它必须是枚举中的 NumberStyles 位标志的组合。 参数可能包含以下元素,style具体取决于其值s

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

或者,如果 style 参数包括 AllowHexSpecifier

[ws]hexdigits[ws]

方括号 ([ 和 ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果style包含标志,则空白显示在标志的s开头,或者在包含styleNumberStyles.AllowTrailingWhite标志的s末尾NumberStyles.AllowLeadingWhite显示。
$ 区域性特定的货币符号。 字符串中的位置由NumberFormatInfo.CurrencyPositivePattern参数方法NumberFormatInfo返回GetFormat的对象的属性provider定义。 如果s包含标志,则货币符号可以出现在style其中NumberStyles.AllowCurrencySymbol
签名 可选正号。 (如果存在负号,s该方法将OverflowException引发 。)如果包含标志,则符号可以出现在标志的s开头,也可以出现在标志的stylesNumberStyles.AllowTrailingSign末尾。NumberStyles.AllowLeadingSignstyle
数字 从 0 到 9 的数字序列。
. 区域性特定的小数点符号。 如果provider包含s标志,则可以显示style由指定的NumberStyles.AllowDecimalPoint区域性的小数点符号。
fractional_digits 数字 0 的一个或多个匹配项。 仅当包含s标志时style,小数位数才会显示NumberStyles.AllowDecimalPoint
e e 或 E 字符,指示该值以指数表示法表示。 如果 style 包含标志,则 s 参数可以表示指数表示法中的 NumberStyles.AllowExponent 数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注释

无论参数的值s如何,分析作都会忽略任何终止 NUL (U+0000) 字符style

仅包含十进制数字(对应于样式)的 NumberStyles.None 字符串始终成功分析。 大多数剩余 NumberStyles 成员控制可能但不需要在此输入字符串中存在的元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s的元素。

非复合 NumberStyles 值 除数字外允许的元素
NumberStyles.None 仅十进制数字。
NumberStyles.AllowDecimalPoint fractional_digits元素。 但是, fractional_digits 必须仅包含一个或多个数字或引发数字 OverflowException
NumberStyles.AllowExponent s 参数还可以使用指数表示法。
NumberStyles.AllowLeadingWhite 开头的 sws 元素。
NumberStyles.AllowTrailingWhite 末尾的 s 元素
NumberStyles.AllowLeadingSign 正号可以在 数字之前显示。
NumberStyles.AllowTrailingSign 正号可以在 数字之后显示。
NumberStyles.AllowParentheses 尽管支持此标志,但使用括号 s 会导致一个 OverflowException
NumberStyles.AllowThousands 虽然组分隔符可以出现在其中 s,但它只能有一个或多个 0 位数字。
NumberStyles.AllowCurrencySymbol 元素 $

NumberStyles.AllowHexSpecifier如果使用标志,s则必须是没有前缀的十六进制值。 例如,“F3”已成功分析,但“0xF3”不会。 唯一可以存在于 style 其中的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteNumberStyles(枚举具有复合数字样式,NumberStyles.HexNumber包括两个空格标志。

参数 provider 是实现 IFormatProvider ,如 NumberFormatInfoCultureInfo 对象。 该 provider 参数提供分析中使用的区域性特定信息。 null如果是provider,则使用线程当前区域性。

另请参阅

适用于

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将指定样式和区域性特定格式的数字的跨度表示形式转换为其 Byte 等效格式。

public static byte Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static byte Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Byte

参数

s
ReadOnlySpan<Char>

一个范围,包含表示要转换的值的字符。

style
NumberStyles

枚举值的按位组合,指示可在其中 s存在的样式元素。 要指定 Integer的典型值为 。

provider
IFormatProvider

一个对象,提供有关格式 s的特定于区域性的信息。 null如果是provider,则使用线程当前区域性。

返回

与中包含的 s数字等效的字节值。

实现

适用于

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将 UTF-8 字符的范围分析为值。

public static byte Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Byte

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符的范围。

style
NumberStyles

可以存在的 utf8Text数字样式的按位组合。

provider
IFormatProvider

一个对象,提供有关区域性特定的格式设置信息 utf8Text

返回

分析 utf8Text的结果。

实现

适用于

Parse(String, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将指定区域性特定格式的数字的字符串表示形式转换为其 Byte 等效格式。

public:
 static System::Byte Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::Byte Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::Byte>::Parse;
public static byte Parse(string s, IFormatProvider provider);
public static byte Parse(string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> byte
Public Shared Function Parse (s As String, provider As IFormatProvider) As Byte

参数

s
String

包含要转换的数字的字符串。 该字符串使用 Integer 样式进行解释。

provider
IFormatProvider

一个对象,它提供有关 s区域性的特定分析信息。 null如果是provider,则使用线程当前区域性。

返回

与中包含的 s数字等效的字节值。

实现

例外

snull

s 格式不正确。

s 表示小于 Byte.MinValue 或大于 Byte.MaxValue 的数字。

示例

以下示例使用Parse该方法分析值的字符串表示形式Byte

string stringToConvert;
byte byteValue;

stringToConvert = " 214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }

stringToConvert = " + 214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }

stringToConvert = " +214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }
// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
let stringToConvert = " 214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

let stringToConvert = " + 214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

let stringToConvert = " +214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
Dim stringToConvert As String 
Dim byteValue As Byte

stringToConvert = " 214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  

stringToConvert = " + 214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  

stringToConvert = " +214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try
' The example displays the following output to the console:
'       Converted ' 214 ' to 214.
'       Unable to parse ' + 214 '.
'       Converted ' +214 ' to 214.

注解

s 参数包含多种形式:

[ws][sign]digits[ws]

方括号 ([ 和 ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。
签名 可选正号。
数字 一系列数字,范围从 0 到 9。

参数 s 使用 Integer 样式进行解释。 除了字节值的十进制数字外,还允许将前导和尾随空格与前导符号一起。 (如果存在符号,则它必须是正号或方法引发 . OverflowException)若要显式定义样式元素以及可存在于区域性特定的格式信息中 s,请使用该方法 Byte.Parse(String, NumberStyles, IFormatProvider)

s参数使用对象providerNumberFormatInfo提供的格式设置信息进行分析。 参数provider是一个IFormatProvider实现,如或NumberFormatInfoCultureInfo对象。 该 provider 参数提供分析中使用的区域性特定信息。 null如果是provider,则使用线程当前区域性。

另请参阅

适用于

Parse(String, NumberStyles)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将指定样式中的数字的字符串表示形式转换为其 Byte 等效形式。

public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static byte Parse(string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> byte
Public Shared Function Parse (s As String, style As NumberStyles) As Byte

参数

s
String

包含要转换的数字的字符串。 使用指定的 style样式解释字符串。

style
NumberStyles

枚举值的按位组合,指示可在其中 s存在的样式元素。 要指定 Integer的典型值为 。

返回

等效于包含在中的 s数字的字节值。

例外

snull

s 格式不正确。

s 表示小于 Byte.MinValue 或大于 Byte.MaxValue 的数字。

-或-

s 包括非零的小数位数。

style 不是值 NumberStyles

-或-

style不是值的组合AllowHexSpecifierHexNumber

示例

以下示例使用Byte.Parse(String, NumberStyles)该方法分析值的字符串表示形式Byte。 本示例的当前区域性为 en-US。

string value;
NumberStyles style;
byte number;

// Parse value with no styles allowed.
style = NumberStyles.None;
value = " 241 ";
try
{
   number = Byte.Parse(value, style);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", value); }

// Parse value with trailing sign.
style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
value = " 163+";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);

// Parse value with leading sign.
value = "   +253  ";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
// Parse value with no styles allowed.
let style = NumberStyles.None
let value = " 241 "
try
    let number = Byte.Parse(value, style);
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."

// Parse value with trailing sign.
let style = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
let value = " 163+"
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// Parse value with leading sign.
let value = "   +253  "
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
Dim value As String
Dim style As NumberStyles
Dim number As Byte

' Parse value with no styles allowed.
style = NumberStyles.None
value = " 241 "
Try
   number = Byte.Parse(value, style)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)   
End Try
  
' Parse value with trailing sign.
style = NumberStyles.Integer Or NumberStyles.AllowTrailingSign
value = " 163+"
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)

' Parse value with leading sign.
value = "   +253  "
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' This example displays the following output to the console:
'       Unable to parse ' 241 '.
'       Converted ' 163+' to 163.
'       Converted '   +253  ' to 253.

注解

style 参数定义允许在 s 参数中成功执行分析操作的样式元素(如空格或正号)。 它必须是枚举中的 NumberStyles 位标志的组合。 参数可以包含以下元素,s具体取决于其值style

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

或者,如果 style 包括 AllowHexSpecifier

[ws]hexdigits[ws]

方括号 ([ 和 ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果style包含NumberStyles.AllowLeadingWhite标志,则可以在开头s显示空格;如果样式包含标志,NumberStyles.AllowTrailingWhite则空格显示在结尾处。
$ 区域性特定的货币符号。 字符串中的位置由 NumberFormatInfo.CurrencyPositivePattern 当前区域性的属性定义。 如果s包含标志,则当前区域性的货币符号可以出现在style其中NumberStyles.AllowCurrencySymbol
签名 可选正号。 (如果存在负号,s该方法将OverflowException引发 。)如果包含标志,则符号可以出现在标志的s开头,也可以出现在标志的stylesNumberStyles.AllowTrailingSign末尾。NumberStyles.AllowLeadingSignstyle
数字 从 0 到 9 的数字序列。
. 区域性特定的小数点符号。 如果包含标志,则当前区域性的小数点符号可以出现在sstyle其中NumberStyles.AllowDecimalPoint
fractional_digits 数字 0 的一个或多个匹配项。 仅当包含s标志时style,小数位数才会显示NumberStyles.AllowDecimalPoint
e e 或 E 字符,指示该值以指数表示法表示。 如果s包含标志,则参数style可以表示指数表示法中的NumberStyles.AllowExponent数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注释

无论参数的值s如何,分析作都会忽略任何终止 NUL (U+0000) 字符style

仅包含十进制数字(对应于样式)的 NumberStyles.None 字符串始终成功分析。 大多数剩余 NumberStyles 成员控制可能但不需要在此输入字符串中存在的元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s的元素。

非复合 NumberStyles 值 除数字外允许的元素
NumberStyles.None 仅十进制数字。
NumberStyles.AllowDecimalPoint fractional_digits元素。 但是, fractional_digits 必须仅包含一个或多个数字或引发数字 OverflowException
NumberStyles.AllowExponent s 参数还可以使用指数表示法。
NumberStyles.AllowLeadingWhite 开头的 sws 元素。
NumberStyles.AllowTrailingWhite 末尾的 s 元素
NumberStyles.AllowLeadingSign 正号可以在 数字之前显示。
NumberStyles.AllowTrailingSign 正号可以在 数字之后显示。
NumberStyles.AllowParentheses 尽管支持此标志,但使用括号 s 会导致一个 OverflowException
NumberStyles.AllowThousands 虽然组分隔符可以出现在其中 s,但它只能有一个或多个 0 位数字。
NumberStyles.AllowCurrencySymbol 元素 $

NumberStyles.AllowHexSpecifier如果使用标志,s则必须是没有前缀的十六进制值。 例如,“F3”已成功分析,但“0xF3”不会。 唯一可与它组合的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (枚举 NumberStyles 包括复合数字样式, NumberStyles.HexNumber包括两个空格标志。

s 参数使用为当前系统区域性初始化的对象中的 NumberFormatInfo 格式设置信息进行分析。 若要使用其他区域性的格式设置信息,请调用 Byte.Parse(String, NumberStyles, IFormatProvider) 重载。

另请参阅

适用于

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将字符的范围分析为值。

public:
 static System::Byte Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Byte>::Parse;
public static byte Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> byte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Byte

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

一个对象,提供有关区域性特定的格式设置信息 s

返回

分析 s的结果。

实现

适用于

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将 UTF-8 字符的范围分析为值。

public:
 static System::Byte Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::Byte>::Parse;
public static byte Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> byte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Byte

参数

utf8Text
ReadOnlySpan<Byte>

要分析的 UTF-8 字符的范围。

provider
IFormatProvider

一个对象,提供有关区域性特定的格式设置信息 utf8Text

返回

分析 utf8Text的结果。

实现

适用于

Parse(String)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

将数字的字符串表示形式转换为其 Byte 等效形式。

public:
 static System::Byte Parse(System::String ^ s);
public static byte Parse(string s);
static member Parse : string -> byte
Public Shared Function Parse (s As String) As Byte

参数

s
String

包含要转换的数字的字符串。 该字符串使用 Integer 样式进行解释。

返回

与中包含的 s数字等效的字节值。

例外

snull

s 格式不正确。

s 表示小于 Byte.MinValue 或大于 Byte.MaxValue 的数字。

示例

以下示例演示如何使用 Byte.Parse(String) 该方法将字符串值转换为字节值。 然后,生成的字节值会显示到控制台。

string stringToConvert = " 162";
byte byteValue;
try
{
   byteValue = Byte.Parse(stringToConvert);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue);
}
// The example displays the following output to the console:
//       Converted ' 162' to 162.
let stringToConvert = " 162"
try
    let byteValue = Byte.Parse stringToConvert
    printfn $"Converted '{stringToConvert}' to {byteValue}."
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."

// The example displays the following output to the console:
//       Converted ' 162' to 162.
Dim stringToConvert As String = " 162"
Dim byteValue As Byte
Try
   byteValue = Byte.Parse(stringToConvert)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  
' The example displays the following output to the console:
'       Converted ' 162' to 162.

注解

s 参数包含多种形式:

[ws][sign]digits[ws]

方括号 ([ 和 ]) 中的元素是可选的。 下表对每个元素进行了描述。

元素 说明
ws 可选空格。
签名 可选的正数或负号。
数字 一系列数字,范围从 0 到 9。

参数 s 使用 NumberStyles.Integer 样式进行解释。 除了字节值的十进制数字外,还允许将前导和尾随空格与前导符号一起。 (如果存在符号,则它必须是正号或方法引发 . OverflowException)若要显式定义可以存在的 s样式元素,请使用 Byte.Parse(String, NumberStyles)Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

s 参数使用为当前系统区域性初始化的对象中的 NumberFormatInfo 格式设置信息进行分析。 有关详细信息,请参阅 CurrentInfo。 若要使用其他区域性的格式设置信息分析字符串,请使用 Byte.Parse(String, NumberStyles, IFormatProvider) 该方法。

另请参阅

适用于