SByte.Parse 方法

定义

将数字的字符串表示形式转换为其等效的 8 位带符号整数。

重载

Parse(String, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(String, IFormatProvider)

将指定区域性特定格式的数字的字符串表示形式转换为等效的 8 位带符号整数。

Parse(String)

将数字的字符串表示形式转换为其等效的 8 位带符号整数。

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符的范围分析为值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(String, NumberStyles)

将指定样式中的数字的字符串表示形式转换为其等效的 8 位带符号整数。

Parse(String, NumberStyles, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Int16.Parse(String, NumberStyles, IFormatProvider)

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

C#
[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
C#
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
C#
[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);

参数

s
String

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

style
NumberStyles

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

provider
IFormatProvider

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

返回

s 参数中指定的数字等效的 8 位有符号字节值。

实现

属性

例外

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber的组合。

s 的格式不符合 style

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

-或-

s 包括非零的小数位数。

示例

下面的示例演示如何使用 Parse(String, NumberStyles, IFormatProvider) 方法将数字的各种字符串表示形式转换为带符号整数值。

C#
using System;
using System.Globalization;

public class SByteConversion
{
   NumberFormatInfo provider = NumberFormatInfo.CurrentInfo;

   public static void Main()
   {
      string stringValue;
      NumberStyles style;

      stringValue = "   123   ";
      style = NumberStyles.None;     
      CallParseOperation(stringValue, style);
      
      stringValue = "000,000,123";
      style = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallParseOperation(stringValue, style);
      
      stringValue = "-100";
      style = NumberStyles.AllowLeadingSign;
      CallParseOperation(stringValue, style);
      
      stringValue = "100-";
      style = NumberStyles.AllowLeadingSign;
      CallParseOperation(stringValue, style);
      
      stringValue = "100-";
      style = NumberStyles.AllowTrailingSign;
      CallParseOperation(stringValue, style);
      
      stringValue = "$100";
      style = NumberStyles.AllowCurrencySymbol;
      CallParseOperation(stringValue, style);
      
      style = NumberStyles.Integer;
      CallParseOperation(stringValue, style);
      
      style = NumberStyles.AllowDecimalPoint;
      CallParseOperation("100.0", style);
      
      stringValue = "1e02";
      style = NumberStyles.AllowExponent;
      CallParseOperation(stringValue, style);
      
      stringValue = "(100)";
      style = NumberStyles.AllowParentheses;
      CallParseOperation(stringValue, style);
   }
   
   private static void CallParseOperation(string stringValue, 
                                          NumberStyles style)
   {                                          
      sbyte number;
      
      if (stringValue == null)
         Console.WriteLine("Cannot parse a null string...");
         
      try
      {
         number = sbyte.Parse(stringValue, style);
         Console.WriteLine("SByte.Parse('{0}', {1})) = {2}", 
                           stringValue, style, number);   
      }
      catch (FormatException)
      {
         Console.WriteLine("'{0}' and {1} throw a FormatException", 
                           stringValue, style);   
      }      
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is outside the range of a signed byte",
                           stringValue);
      }
   }
}
// The example displays the following information to the console:
//       '   123   ' and None throw a FormatException
//       SByte.Parse('000,000,123', Integer, AllowThousands)) = 123
//       SByte.Parse('-100', AllowLeadingSign)) = -100
//       '100-' and AllowLeadingSign throw a FormatException
//       SByte.Parse('100-', AllowTrailingSign)) = -100
//       SByte.Parse('$100', AllowCurrencySymbol)) = 100
//       '$100' and Integer throw a FormatException
//       SByte.Parse('100.0', AllowDecimalPoint)) = 100
//       SByte.Parse('1e02', AllowExponent)) = 100
//       SByte.Parse('(100)', AllowParentheses)) = -100

注解

style 参数定义允许 s 参数中允许的样式元素(如空格或正符号或负号符号),以便分析操作成功。 它必须是 NumberStyles 枚举中的位标志的组合。

根据 style的值,s 参数可能包括以下元素:

[ws][$][符号]数字[.fractional_digits][E[sign]exponential_digits][ws]

如果 style 包含 AllowHexSpecifiers 参数可能包含以下元素:

[ws]hexdigits[ws]

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

元素 描述
ws 可选空格。 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空格可能出现在 s 的开头,如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则它将显示在 s 末尾。
$ 区域性特定的货币符号。 字符串中的位置由当前区域性的 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 s 中。
签名 可选符号。 如果 style 包含 NumberStyles.AllowLeadingSign 标志,则符号可以出现在 s 的开头,如果 style 包含 NumberStyles.AllowTrailingSign 标志,则它将显示 s 的末尾。 如果 style 包含 NumberStyles.AllowParentheses 标志,则可以在 s 中使用括号来指示负值。
从 0 到 9 的数字序列。
区域性特定的小数点符号。 如果 style 包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 s 中。
fractional_digits 如果 style 包含 NumberStyles.AllowExponent 标志,则数字 0-9 的一个或多个匹配项;如果未包含数字 0,则为一个或多个匹配项。 仅当 style 包含 NumberStyles.AllowDecimalPoint 标志时,小数位数才会显示在 s 中。
E “e”或“E”字符,指示该值以指数(科学)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 标志,s 参数可以表示指数表示法中的数字。
exponential_digits 从 0 到 9 的数字序列。 如果 style 包含 NumberStyles.AllowExponent 标志,s 参数可以表示指数表示法中的数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

备注

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

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

非复合 NumberStyles 除数字外,s 允许的元素
NumberStyles.None 仅十进制数字。
NumberStyles.AllowDecimalPoint 小数点(.)和 fractional_digits 元素。 但是,如果样式不包含 NumberStyles.AllowExponent 标志,fractional_digits 必须仅包含一个或多个数字;否则,将引发 OverflowException
NumberStyles.AllowExponent 指示指数表示法的“e”或“E”字符以及 exponential_digits
NumberStyles.AllowLeadingWhite s开头的 ws 元素。
NumberStyles.AllowTrailingWhite s末尾的 ws 元素。
NumberStyles.AllowLeadingSign 前的正号。
NumberStyles.AllowTrailingSign 数字后的正号。
NumberStyles.AllowParentheses 位前后的括号 指示负值。
NumberStyles.AllowThousands 组分隔符()元素。 尽管组分隔符可以出现在 s中,但它的前面必须只有一个或多个数字。
NumberStyles.AllowCurrencySymbol 货币 ($) 元素。

如果使用 NumberStyles.AllowHexSpecifier 标志,s 必须是十六进制值。 有效的十六进制数字为 0-9、a-f 和 A-F。 唯一可与它组合的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 枚举包括包含两个空格标志的复合数字样式 NumberStyles.HexNumber

备注

如果 s 参数是十六进制数的字符串表示形式,则它不能前面有任何修饰(如 0x&h),将其区分为十六进制数。 这会导致分析操作引发异常。

如果 s 表示十六进制数,Parse(String, NumberStyles) 方法将字节的高阶位解释为符号位。

provider 参数是一个 IFormatProvider 实现,GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 s格式的区域性特定信息。 可通过三种方法使用 provider 参数向分析操作提供自定义格式设置信息:

如果 providernull,则使用当前区域性的 NumberFormatInfo 对象。

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

重要

此 API 不符合 CLS。

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

C#
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
C#
[System.CLSCompliant(false)]
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
C#
[System.CLSCompliant(false)]
public static sbyte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);

参数

s
ReadOnlySpan<Char>

一个范围,包含表示要转换的数字的字符。 使用 style指定的样式来解释范围。

style
NumberStyles

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

provider
IFormatProvider

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

返回

s 参数中指定的数字等效的 8 位有符号字节值。

实现

属性

适用于

.NET 9 和其他版本
产品 版本
.NET Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Standard 2.1

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs

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

C#
public static sbyte Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);

参数

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

返回

分析 utf8Text的结果。

实现

适用于

.NET 9 和 .NET 8
产品 版本
.NET 8, 9

Parse(String, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Int16.Parse(String)

将指定区域性特定格式的数字的字符串表示形式转换为等效的 8 位带符号整数。

C#
[System.CLSCompliant(false)]
public static sbyte Parse (string s, IFormatProvider provider);
C#
public static sbyte Parse (string s, IFormatProvider? provider);
C#
[System.CLSCompliant(false)]
public static sbyte Parse (string s, IFormatProvider? provider);

参数

s
String

一个表示要转换的数字的字符串。 使用 Integer 样式解释字符串。

provider
IFormatProvider

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

返回

s中指定的数字等效的 8 位有符号整数。

实现

属性

例外

s 格式不正确。

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

示例

以下示例定义一个自定义 NumberFormatInfo 对象,该对象将波形符(~)定义为负号。 然后,它使用此自定义 NumberFormatInfo 对象以及表示固定区域性的 CultureInfo 对象分析多个数值字符串。

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberFormatInfo nf = new NumberFormatInfo();
      nf.NegativeSign = "~"; 
      
      string[] values = { "-103", "+12", "~16", "  1", "~255" };
      IFormatProvider[] providers = { nf, CultureInfo.InvariantCulture };
      
      foreach (IFormatProvider provider in providers)
      {
         Console.WriteLine("Conversions using {0}:", ((object) provider).GetType().Name);
         foreach (string value in values)
         {
            try {
               Console.WriteLine("   Converted '{0}' to {1}.", 
                                 value, SByte.Parse(value, provider));
            }                     
            catch (FormatException) {
               Console.WriteLine("   Unable to parse '{0}'.", value);   
            }
            catch (OverflowException) {
               Console.WriteLine("   '{0}' is out of range of the SByte type.", value);         
            }
         }
      }      
   }
}
// The example displays the following output:
//       Conversions using NumberFormatInfo:
//          Unable to parse '-103'.
//          Converted '+12' to 12.
//          Converted '~16' to -16.
//          Converted '  1' to 1.
//          '~255' is out of range of the SByte type.
//       Conversions using CultureInfo:
//          Converted '-103' to -103.
//          Converted '+12' to 12.
//          Unable to parse '~16'.
//          Converted '  1' to 1.
//          Unable to parse '~255'.

注解

s 参数包含多种形式:

[ws][符号][ws]

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

元素 描述
ws 可选空格。
签名 可选符号。
一系列数字,范围从 0 到 9。

s 参数使用 Integer 样式进行解释。 除了字节值的十进制数字外,还允许使用前导符号的前导和尾随空格。 若要显式定义具有区域性特定的格式设置信息的样式元素,可以在 s中显示,请使用 Parse(String, NumberStyles, IFormatProvider) 方法。

provider 参数是一个 IFormatProvider 实现,GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 s格式的区域性特定信息。 可通过三种方法使用 provider 参数向分析操作提供自定义格式设置信息:

如果 providernull,则使用当前区域性的 NumberFormatInfo 对象。

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(String)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Int16.Parse(String)

将数字的字符串表示形式转换为其等效的 8 位带符号整数。

C#
[System.CLSCompliant(false)]
public static sbyte Parse (string s);
C#
public static sbyte Parse (string s);

参数

s
String

一个表示要转换的数字的字符串。 使用 Integer 样式解释字符串。

返回

s 参数中包含的数字等效的 8 位有符号整数。

属性

例外

s null

s 不包含可选符号,后跟数字序列(零到 9)。

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

示例

以下示例演示如何使用 Parse 方法将字符串值转换为带符号字节值。 生成的有符号字节值随后会显示到控制台。

C#
// Define an array of numeric strings.
string[] values = { "-16", "  -3", "+ 12", " +12 ", "  12  ",
                    "+120", "(103)", "192", "-160" };
                           
// Parse each string and display the result.
foreach (string value in values)
{
   try {
      Console.WriteLine("Converted '{0}' to the SByte value {1}.",
                        value, SByte.Parse(value));
   }
   catch (FormatException) {
      Console.WriteLine("'{0}' cannot be parsed successfully by SByte type.",
                        value);
   }                              
   catch (OverflowException) {
      Console.WriteLine("'{0}' is out of range of the SByte type.",
                        value);
   }                                                                        
}
// The example displays the following output:
//       Converted '-16' to the SByte value -16.
//       Converted '  -3' to the SByte value -3.
//       '+ 12' cannot be parsed successfully by SByte type.
//       Converted ' +12 ' to the SByte value 12.
//       Converted '  12  ' to the SByte value 12.
//       Converted '+120' to the SByte value 120.
//       '(103)' cannot be parsed successfully by SByte type.
//       '192' is out of range of the SByte type.
//       '-160' is out of range of the SByte type.

注解

s 参数包含多种形式:

[ws][符号][ws]

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

元素 描述
ws 可选空格。
签名 可选符号。
一系列数字,范围从 0 到 9。

s 参数使用 NumberStyles.Integer 样式进行解释。 除了字节值的十进制数字外,还允许使用前导正号或负号的前导空格和尾随空格。 若要显式定义 s中可以存在的样式元素,请使用 Parse(String, NumberStyles)Parse(String, NumberStyles, IFormatProvider) 方法。

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

将字符的范围分析为值。

C#
public static sbyte Parse (ReadOnlySpan<char> s, IFormatProvider? provider);

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

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

返回

分析 s的结果。

实现

适用于

.NET 9 和其他版本
产品 版本
.NET 7, 8, 9

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Source:
SByte.cs
Source:
SByte.cs

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

C#
public static sbyte Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);

参数

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

返回

分析 utf8Text的结果。

实现

适用于

.NET 9 和 .NET 8
产品 版本
.NET 8, 9

Parse(String, NumberStyles)

Source:
SByte.cs
Source:
SByte.cs
Source:
SByte.cs

重要

此 API 不符合 CLS。

符合 CLS 的替代方案
System.Int16.Parse(String)

将指定样式中的数字的字符串表示形式转换为其等效的 8 位带符号整数。

C#
[System.CLSCompliant(false)]
public static sbyte Parse (string s, System.Globalization.NumberStyles style);
C#
public static sbyte Parse (string s, System.Globalization.NumberStyles style);

参数

s
String

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

style
NumberStyles

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

返回

s中指定的数字等效的 8 位有符号整数。

属性

例外

s 的格式不符合 style

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

-或-

s 包括非零的小数位数。

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的组合。

示例

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

C#
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberStyles style;
      sbyte number;

      // Parse value with no styles allowed.
      string[] values1 = { " 121 ", "121", "-121" };
      style = NumberStyles.None;
      Console.WriteLine("Styles: {0}", style.ToString());
      foreach (string value in values1)
      {
         try {
            number = SByte.Parse(value, style);
            Console.WriteLine("   Converted '{0}' to {1}.", value, number);
         }   
         catch (FormatException) {
            Console.WriteLine("   Unable to parse '{0}'.", value);
         }
      }
      Console.WriteLine();
            
      // Parse value with trailing sign.
      style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
      string[] values2 = { " 103+", " 103 +", "+103", "(103)", "   +103  " };
      Console.WriteLine("Styles: {0}", style.ToString());
      foreach (string value in values2)
      {
         try {
            number = SByte.Parse(value, style);
            Console.WriteLine("   Converted '{0}' to {1}.", value, number);
         }   
         catch (FormatException) {
            Console.WriteLine("   Unable to parse '{0}'.", value);
         }      
         catch (OverflowException) {
            Console.WriteLine("   '{0}' is out of range of the SByte type.", value);         
         }
      }      
      Console.WriteLine();
   }
}
// The example displays the following output:
//       Styles: None
//          Unable to parse ' 121 '.
//          Converted '121' to 121.
//          Unable to parse '-121'.
//       
//       Styles: Integer, AllowTrailingSign
//          Converted ' 103+' to 103.
//          Converted ' 103 +' to 103.
//          Converted '+103' to 103.
//          Unable to parse '(103)'.
//          Converted '   +103  ' to 103.

注解

style 参数定义允许 s 参数中允许的样式元素(如空格或正符号或负号符号),以便分析操作成功。 它必须是 NumberStyles 枚举中的位标志的组合。

根据 style的值,s 参数可能包括以下元素:

[ws][$][符号]数字[.fractional_digits][E[sign]exponential_digits][ws]

如果 style 包括 NumberStyles.AllowHexSpecifiers 参数可能包含以下元素:

[ws]hexdigits[ws]

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

元素 描述
ws 可选空格。 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空格可以出现在 s 的开头,如果样式包含 NumberStyles.AllowTrailingWhite 标志,则可以显示在 s 末尾。
$ 区域性特定的货币符号。 字符串中的位置由当前区域性的 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 s 中。
签名 可选符号。 如果 style 包含 NumberStyles.AllowLeadingSign 标志,则符号可以在 s 开头显示,如果 style 包含 NumberStyles.AllowTrailingSign 标志,则它将显示在 s 末尾。 如果 style 包含 NumberStyles.AllowParentheses 标志,则可以在 s 中使用括号来指示负值。
从 0 到 9 的数字序列。
区域性特定的小数点符号。 如果 style 包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 s 中。
fractional_digits 如果 style 包含 NumberStyles.AllowExponent 标志,则数字 0-9 的一个或多个匹配项;如果未包含数字 0,则为一个或多个匹配项。 仅当 style 包含 NumberStyles.AllowDecimalPoint 标志时,小数位数才会显示在 s 中。
E “e”或“E”字符,指示该值以指数(科学)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 标志,s 参数可以表示指数表示法中的数字。
exponential_digits 数字 0-9 的一个或多个匹配项。 如果 style 包含 NumberStyles.AllowExponent 标志,s 参数可以表示指数表示法中的数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

备注

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

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

非复合 NumberStyles 值 除数字以外, 中允许的元素
NumberStyles.None 仅十进制数字。
NumberStyles.AllowDecimalPoint 小数点(.)和 fractional_digits 元素。 但是,如果 style 不包含 NumberStyles.AllowExponent 标志,fractional_digits 必须仅包含一个或多个 0 位数字;否则,将引发 OverflowException
NumberStyles.AllowExponent 指示指数表示法的“e”或“E”字符以及 exponential_digits
NumberStyles.AllowLeadingWhite s开头的 ws 元素。
NumberStyles.AllowTrailingWhite s末尾的 ws 元素。
NumberStyles.AllowLeadingSign 前的正号。
NumberStyles.AllowTrailingSign 数字后的正号。
NumberStyles.AllowParentheses 符号 元素,以括住数值的括号形式。
NumberStyles.AllowThousands 组分隔符 (,) 元素。 尽管组分隔符可以出现在 s中,但它的前面必须只有一个或多个数字。
NumberStyles.AllowCurrencySymbol 货币 ($) 元素。

如果使用 NumberStyles.AllowHexSpecifier 标志,s 必须是十六进制值。 有效的十六进制数字为 0-9、a-f 和 A-F。 不支持“0x”等前缀,导致分析操作失败。 style 中可以组合的唯一其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 枚举包括包含两个空格标志的复合数字样式 NumberStyles.HexNumber

备注

如果 s 参数是十六进制数的字符串表示形式,则它不能前面有任何修饰(如 0x&h),将其区分为十六进制数。 这会导致分析操作引发异常。

如果 s 表示十六进制数,Parse(String, NumberStyles) 方法将字节的高阶位解释为符号位。

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

另请参阅

适用于

.NET 9 和其他版本
产品 版本
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0