UInt16.Parse 方法

定义

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

重载

Parse(String, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(String, IFormatProvider)

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

Parse(String, NumberStyles)

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

此方法不符合 CLS。 符合 CLS 的替代方法是 Parse(String, NumberStyles)

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符的范围分析为值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(String)

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

Parse(String, NumberStyles, IFormatProvider)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

此 API 不符合 CLS。

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

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

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

参数

s
String

一个表示要转换的数字的字符串。 使用 style 参数指定的样式来解释字符串。

style
NumberStyles

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

provider
IFormatProvider

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

返回

s中指定的数字等效的 16 位无符号整数。

实现

属性

例外

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的组合。

s 的格式不符合 style

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

-或-

s 包括非零的小数位数。

示例

以下示例使用 Parse(String, NumberStyles, IFormatProvider) 方法将数字的各种字符串表示形式转换为 16 位无符号整数值。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] cultureNames = { "en-US", "fr-FR" };
      NumberStyles[] styles= { NumberStyles.Integer, 
                               NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
      string[] values = { "1702", "+1702.0", "+1702,0", "-1032.00",
                          "-1032,00", "1045.1", "1045,1" };
      
      // Parse strings using each culture
      foreach (string cultureName in cultureNames)
      {
         CultureInfo ci = new CultureInfo(cultureName);
         Console.WriteLine("Parsing strings using the {0} culture", 
                           ci.DisplayName);
         // Use each style.
         foreach (NumberStyles style in styles)
         {
            Console.WriteLine("   Style: {0}", style.ToString());
            // Parse each numeric string.
            foreach (string value in values)
            {
               try {
                  Console.WriteLine("      Converted '{0}' to {1}.", value, 
                                    UInt16.Parse(value, style, ci));
               }                                    
               catch (FormatException) {
                  Console.WriteLine("      Unable to parse '{0}'.", value);   
               }
               catch (OverflowException) {
                  Console.WriteLine("      '{0}' is out of range of the UInt16 type.", 
                                    value);
               }
            }
         }
      }   
   }
}
// The example displays the following output:
//       Parsing strings using the English (United States) culture
//          Style: Integer
//             Converted '1702' to 1702.
//             Unable to parse '+1702.0'.
//             Unable to parse '+1702,0'.
//             Unable to parse '-1032.00'.
//             Unable to parse '-1032,00'.
//             Unable to parse '1045.1'.
//             Unable to parse '1045,1'.
//          Style: Integer, AllowDecimalPoint
//             Converted '1702' to 1702.
//             Converted '+1702.0' to 1702.
//             Unable to parse '+1702,0'.
//             '-1032.00' is out of range of the UInt16 type.
//             Unable to parse '-1032,00'.
//             '1045.1' is out of range of the UInt16 type.
//             Unable to parse '1045,1'.
//       Parsing strings using the French (France) culture
//          Style: Integer
//             Converted '1702' to 1702.
//             Unable to parse '+1702.0'.
//             Unable to parse '+1702,0'.
//             Unable to parse '-1032.00'.
//             Unable to parse '-1032,00'.
//             Unable to parse '1045.1'.
//             Unable to parse '1045,1'.
//          Style: Integer, AllowDecimalPoint
//             Converted '1702' to 1702.
//             Unable to parse '+1702.0'.
//             Converted '+1702,0' to 1702.
//             Unable to parse '-1032.00'.
//             '-1032,00' is out of range of the UInt16 type.
//             Unable to parse '1045.1'.
//             '1045,1' is out of range of the UInt16 type.

注解

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

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

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

方括号 ([ 和 ]) 中的元素是可选的。 如果 style 包含 NumberStyles.AllowHexSpecifiers 参数可能包含以下元素:

[ws]hexdigits[ws]

下表描述了每个元素。

元素 描述
ws 可选空格。 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空格可能出现在 s 的开头,如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则它将显示在 s 末尾。
$ 区域性特定的货币符号。 字符串中的位置由 provider 参数的 GetFormat 方法返回的 NumberFormatInfo 对象的 CurrencyPositivePattern 属性定义。 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,货币符号可以出现在 s 中。
签名 可选符号。 (如果 s 包含负号并表示非零数字,该方法将引发 OverflowException。如果 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 组分隔符()元素。
NumberStyles.AllowCurrencySymbol 货币 ($) 元素。

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

备注

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

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:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

此 API 不符合 CLS。

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

public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static ushort 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的区域性特定格式设置信息。

返回

s中指定的数字等效的 16 位无符号整数。

实现

属性

适用于

.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:
UInt16.cs
Source:
UInt16.cs

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

public static ushort 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:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

此 API 不符合 CLS。

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

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

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

参数

s
String

一个表示要转换的数字的字符串。

provider
IFormatProvider

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

返回

s中指定的数字等效的 16 位无符号整数。

实现

属性

例外

s 格式不正确。

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

示例

以下示例实例化使用两个加号(++)作为正符号的自定义区域性。 然后,它调用 Parse(String, IFormatProvider) 方法,以使用表示此自定义区域性和固定区域性的 CultureInfo 对象分析字符串数组。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      // Define a custom culture that uses "++" as a positive sign. 
      CultureInfo ci = new CultureInfo("");
      ci.NumberFormat.PositiveSign = "++";
      // Create an array of cultures.
      CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
      // Create an array of strings to parse.
      string[] values = { "++1403", "-0", "+0", "+16034", 
                          Int16.MinValue.ToString(), "14.0", "18012" };
      // Parse the strings using each culture.
      foreach (CultureInfo culture in cultures)
      {
         Console.WriteLine("Parsing with the '{0}' culture.", culture.Name);
         foreach (string value in values)
         {
            try {
               ushort number = UInt16.Parse(value, culture);
               Console.WriteLine("   Converted '{0}' to {1}.", value, number);
            }
            catch (FormatException) {
               Console.WriteLine("   The format of '{0}' is invalid.", value);
            }
            catch (OverflowException) {
               Console.WriteLine("   '{0}' is outside the range of a UInt16 value.", value);
            }               
         }
      }
   }
}
// The example displays the following output:
//       Parsing with the  culture.
//          Converted '++1403' to 1403.
//          Converted '-0' to 0.
//          The format of '+0' is invalid.
//          The format of '+16034' is invalid.
//          '-32768' is outside the range of a UInt16 value.
//          The format of '14.0' is invalid.
//          Converted '18012' to 18012.
//       Parsing with the '' culture.
//          The format of '++1403' is invalid.
//          Converted '-0' to 0.
//          Converted '+0' to 0.
//          Converted '+16034' to 16034.
//          '-32768' is outside the range of a UInt16 value.
//          The format of '14.0' is invalid.
//          Converted '18012' to 18012.

注解

s 参数包含多种形式:

[ws][符号][ws]

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

元素 描述
ws 可选空格。
标志 可选符号;如果 s 表示值零,则为负号。
数字 一系列数字,范围从 0 到 9。

s 参数使用 NumberStyles.Integer 样式进行解释。 除了字节值的十进制数字外,还允许前导和尾随空格以及前导符号。 (如果存在负号,s 必须表示值为零,否则该方法将引发 OverflowException。)若要将样式元素与 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, NumberStyles)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

此 API 不符合 CLS。

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

此方法不符合 CLS。 符合 CLS 的替代方法是 Parse(String, NumberStyles)

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

参数

s
String

一个表示要转换的数字的字符串。 使用 style 参数指定的样式来解释字符串。

style
NumberStyles

指定允许的 s格式的枚举值的按位组合。 要指定的典型值为 Integer

返回

s中指定的数字等效的 16 位无符号整数。

属性

例外

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的组合。

s 的格式不符合 style

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

-或-

s 包括非零的小数位数。

示例

以下示例尝试使用多个 NumberStyles 值分析字符串数组中的每个元素。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string[] values = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.0e-2" };
      NumberStyles whitespace =  NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
      NumberStyles[] styles = { NumberStyles.None, whitespace, 
                                NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace, 
                                NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol, 
                                NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };

      // Attempt to convert each number using each style combination.
      foreach (string value in values)
      {
         Console.WriteLine("Attempting to convert '{0}':", value);
         foreach (NumberStyles style in styles)
         {
            try {
               ushort number = UInt16.Parse(value, style);
               Console.WriteLine("   {0}: {1}", style, number);
            }   
            catch (FormatException) {
               Console.WriteLine("   {0}: Bad Format", style);
            }
         }
         Console.WriteLine();
      }
   }
}
// The example display the following output:
//    Attempting to convert ' 214 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: 214
//       Integer, AllowTrailingSign: 214
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '1,064':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: 1064
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '(0)':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '1241+':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 1241
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' + 214 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert ' +214 ':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: 214
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: Bad Format
//    
//    Attempting to convert '2153.0':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 2153
//    
//    Attempting to convert '1e03':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 1000
//    
//    Attempting to convert '1300.0e-2':
//       None: Bad Format
//       AllowLeadingWhite, AllowTrailingWhite: Bad Format
//       Integer, AllowTrailingSign: Bad Format
//       AllowThousands, AllowCurrencySymbol: Bad Format
//       AllowDecimalPoint, AllowExponent: 13

注解

style 参数定义样式元素(如空格、正号或负号符号、组分隔符或小数点符号),这些符号在 s 参数中允许分析操作成功。 style 必须是 NumberStyles 枚举中的位标志的组合。 style 参数使此方法重载在 s 包含十六进制值的字符串表示形式、当 s 表示的数字系统(十进制或十六进制)仅在运行时已知,或者当你想要禁止空格或 s中的符号时,此方法重载非常有用。

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

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

方括号 ([ 和 ]) 中的元素是可选的。 如果 style 包括 NumberStyles.AllowHexSpecifiers 参数可能包含以下元素:

[ws]hexdigits[ws]

下表描述了每个元素。

元素 描述
ws 可选空格。 如果 style 包含 NumberStyles.AllowLeadingWhite 标志,则空白可以出现在 s 的开头,如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则可以显示在 s 末尾。
$ 区域性特定的货币符号。 字符串中的位置由当前区域性的 NumberFormatInfo.CurrencyNegativePatternNumberFormatInfo.CurrencyPositivePattern 属性定义。 如果 style 包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 s 中。
签名 可选符号。 如果 style 包含 NumberStyles.AllowLeadingSign 标志,则符号可以在 s 开始时显示,如果 style 包含 NumberStyles.AllowTrailingSign 标志,则它将显示在 s 末尾。 如果 style 包含 NumberStyles.AllowParentheses 标志,则可以在 s 中使用括号来指示负值。 但是,负号符号只能与零一起使用;否则,该方法将引发 OverflowException


fractional_digits

exponential_digits
从 0 到 9 的数字序列。 对于 fractional_digits,只有数字 0 有效。
区域性特定的组分隔符符号。 如果 style 包含 NumberStyles.AllowThousands 标志,则当前区域性的组分隔符可以出现在 s 中。
区域性特定的小数点符号。 如果 style 包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在 s 中。 只有数字 0 可以显示为分析操作成功的小数位数;如果 fractional_digits 包含任何其他数字,则会引发 FormatException
E “e”或“E”字符,指示该值以指数(科学)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 标志,s 参数可以表示指数表示法中的数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

备注

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

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

NumberStyles 除数字外,s 允许的元素
None 仅 元素 位数。
AllowDecimalPoint 小数点(.)和 小数位数 元素。
AllowExponent 指示指数表示法的“e”或“E”字符以及 exponential_digits
AllowLeadingWhite s开头的 ws 元素。
AllowTrailingWhite s末尾的 ws 元素。
AllowLeadingSign s开头的 符号 元素。
AllowTrailingSign s末尾的 符号 元素。
AllowParentheses 符号 元素,以括住数值的括号形式。
AllowThousands 组分隔符 (,) 元素。
AllowCurrencySymbol currency ($) 元素。
Currency 所有元素。 但是,s 不能表示十六进制数或指数表示法中的数字。
Float ws 元素位于 s开头或末尾,符号s开头,以及小数点(.)符号。 s 参数也可以使用指数表示法。
Number wssign、组分隔符()和小数点(.)元素。
Any 所有元素。 但是,s 不能表示十六进制数。

与其他允许但不需要的其他 NumberStyles 值不同,s中存在特定样式元素,NumberStyles.AllowHexSpecifier 样式值意味着 s 中的单个数字字符始终被解释为十六进制字符。 有效的十六进制字符为 0-9、A-F 和 a-f。 不支持前缀(如“0x”),并导致分析操作失败。 唯一可与 style 参数组合的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 枚举包括包含两个空格标志的复合数字样式 NumberStyles.HexNumber

备注

如果 s 是十六进制数的字符串表示形式,则任何修饰(如 0x&h)都不能将其区分为十六进制数。 这会导致转换失败。

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

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

将字符的范围分析为值。

public static ushort Parse (ReadOnlySpan<char> s, IFormatProvider? provider);

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

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

返回

分析 s的结果。

实现

适用于

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

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Source:
UInt16.cs
Source:
UInt16.cs

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

public static ushort Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);

参数

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

返回

分析 utf8Text的结果。

实现

适用于

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

Parse(String)

Source:
UInt16.cs
Source:
UInt16.cs
Source:
UInt16.cs

重要

此 API 不符合 CLS。

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

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

[System.CLSCompliant(false)]
public static ushort Parse (string s);
public static ushort Parse (string s);

参数

s
String

一个表示要转换的数字的字符串。

返回

s中包含的数字等效的 16 位无符号整数。

属性

例外

s 格式不正确。

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

示例

以下示例调用 Parse(String) 方法,将字符串数组中的每个元素转换为无符号 16 位整数。

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "-0", "17", "-12", "185", "66012", "+0", 
                          "", null, "16.1", "28.0", "1,034" };
      foreach (string value in values)
      {
         try {
            ushort number = UInt16.Parse(value);
            Console.WriteLine("'{0}' --> {1}", value, number);
         }
         catch (FormatException) {
            Console.WriteLine("'{0}' --> Bad Format", value);
         }
         catch (OverflowException) {   
            Console.WriteLine("'{0}' --> OverflowException", value);
         }
         catch (ArgumentNullException) {
            Console.WriteLine("'{0}' --> Null", value);
         }
      }                                 
   }
}
// The example displays the following output:
//       '-0' --> 0
//       '17' --> 17
//       '-12' --> OverflowException
//       '185' --> 185
//       '66012' --> OverflowException
//       '+0' --> 0
//       '' --> Bad Format
//       '' --> Null
//       '16.1' --> Bad Format
//       '28.0' --> Bad Format
//       '1,034' --> Bad Format

注解

s 参数应为以下格式的数字的字符串表示形式。

[ws][符号][ws]

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

元素 描述
ws 可选空格。
签名 可选符号。 有效符号字符由当前区域性的 NumberFormatInfo.NegativeSignNumberFormatInfo.PositiveSign 属性决定。 但是,负号符号只能与零一起使用;否则,该方法将引发 OverflowException
一系列数字,范围从 0 到 9。 将忽略任何前导零。

备注

s 参数指定的字符串通过使用 NumberStyles.Integer 样式进行解释。 它不能包含任何组分隔符或小数分隔符,并且不能包含小数部分。

s 参数通过使用为当前系统区域性初始化的 System.Globalization.NumberFormatInfo 对象中的格式设置信息进行分析。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要使用特定区域性的格式设置信息分析字符串,请使用 Parse(String, 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