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)

重要

此 API 不符合 CLS。

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

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

public:
 static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::UInt16>::Parse;
[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);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint16
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As UShort

参数

s
String

表示要转换的数字的字符串。 该字符串使用由 style 参数指定的样式来进行解释。

style
NumberStyles

枚举值的按位组合,用于指示可出现在 s 中的样式元素。 要指定的一个典型值为 Integer

provider
IFormatProvider

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

返回

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

实现

属性

例外

snull

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.
open System
open System.Globalization

let cultureNames = [| "en-US"; "fr-FR" |]
let styles = 
    [| NumberStyles.Integer; NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint |]
let values =
    [| "1702"; "+1702.0"; "+1702,0"; "-1032.00"; "-1032,00"; "1045.1"; "1045,1" |]

// Parse strings using each culture
for cultureName in cultureNames do
    let ci = CultureInfo cultureName
    printfn $"Parsing strings using the {ci.DisplayName} culture"
    // Use each style.
    for style in styles do
        printfn $"   Style: {style}"
        // Parse each numeric string.
        for value in values do
            try
                printfn $"      Converted '{value}' to {UInt16.Parse(value, style, ci)}."
            with
            | :? FormatException ->
                printfn $"      Unable to parse '{value}'."
            | :? OverflowException ->
                printfn $"      '{value}' is out of range of the UInt16 type."
// 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.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim cultureNames() As String = { "en-US", "fr-FR" }
      Dim styles() As NumberStyles = { NumberStyles.Integer, _
                                       NumberStyles.Integer Or NumberStyles.AllowDecimalPoint }
      Dim values() As String = { "1702", "+1702.0", "+1702,0", "-1032.00", _
                                 "-1032,00", "1045.1", "1045,1" }
      
      ' Parse strings using each culture
      For Each cultureName As String In cultureNames
         Dim ci As New CultureInfo(cultureName)
         Console.WriteLine("Parsing strings using the {0} culture", ci.DisplayName)
         ' Use each style.
         For Each style As NumberStyles In styles
            Console.WriteLine("   Style: {0}", style.ToString())
            ' Parse each numeric string.
            For Each value As String In values
               Try
                  Console.WriteLine("      Converted '{0}' to {1}.", value, _
                                    UInt16.Parse(value, style, ci))
               Catch e As FormatException
                  Console.WriteLine("      Unable to parse '{0}'.", value)   
               Catch e As OverflowException
                  Console.WriteLine("      '{0}' is out of range of the UInt16 type.", _
                                    value)         
               End Try
            Next
         Next
      Next                                    
   End Sub
End Module
' 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 的组合。

根据 的值 styles 参数可能包含以下元素:

[ws][$][sign]digits[.fractional_digits][E[sign]exponential_digits][ws]

方括号 ([ and ]) 中的元素是可选的。 如果 style 包含 NumberStyles.AllowHexSpecifier,则 s 参数可以包含以下元素:

[ws]hexdigits[ws]

下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果 style 包含标志,则空格可以出现在 开头s,如果style包含NumberStyles.AllowTrailingWhite标志,则它可能显示在 sNumberStyles.AllowLeadingWhite 末尾。
$ 区域性特定的货币符号。 它在字符串中的位置由 CurrencyPositivePattern 参数的 方法provider返回GetFormatNumberFormatInfo 对象的 属性定义。 如果style包含 标志,NumberStyles.AllowCurrencySymbol则可以显示s货币符号。
sign 可选符号。 (如果s包含负号并表示非零数字,则方法将引发 OverflowException 。) 如果style包含 NumberStyles.AllowLeadingSign 标志,则符号可以出现在 的s开头,如果style包含 NumberStyles.AllowTrailingSign 标志,则它可能显示为 的s末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在 中使用s括号来指示负值。
位数 从 0 到 9 的数字序列。
. 区域性特定的小数点符号。 如果style包含 标志,则当前区域性的小数点符号会显示在 NumberStyles.AllowDecimalPoints
fractional_digits 如果包含 NumberStyles.AllowExponent 标志,则出现一个或多个数字 0-9;如果style不包含标志,则出现一个或多个数字 0。 仅当包含 NumberStyles.AllowDecimalPoint 标志时style,小数位数才能显示在 中s
E “e”或“E”字符,指示值以指数 (科学) 表示法表示。 如果style包含 标志,参数s可以表示指数表示法的数字NumberStyles.AllowExponent
exponential_digits 从 0 到 9 的数字序列。 如果style包含 标志,参数s可以表示指数表示法的数字NumberStyles.AllowExponent
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑参数的值 style

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

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

如果使用标志 NumberStyles.AllowHexSpecifiers 则必须是十六进制值。 有效的十六进制数字是 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 使用当前区域性的 对象。

另请参阅

适用于

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

重要

此 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);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint16
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As UShort

参数

s
ReadOnlySpan<Char>

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

style
NumberStyles

枚举值的按位组合,用于指示可出现在 s 中的样式元素。 要指定的一个典型值为 Integer

provider
IFormatProvider

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

返回

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

实现

属性

适用于

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

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

参数

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

返回

分析 utf8Text的结果。

实现

适用于

Parse(String, IFormatProvider)

重要

此 API 不符合 CLS。

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

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

public:
 static System::UInt16 Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::UInt16 Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::UInt16>::Parse;
[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);
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> uint16
static member Parse : string * IFormatProvider -> uint16
Public Shared Function Parse (s As String, provider As IFormatProvider) As UShort

参数

s
String

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

provider
IFormatProvider

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

返回

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

实现

属性

例外

snull

s 的格式不正确。

示例

以下示例实例化一个自定义区域性,该区域性使用两个加号 (++) 作为正号。 然后, 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.
open System
open System.Globalization

// Define a custom culture that uses "++" as a positive sign. 
let ci = CultureInfo ""
ci.NumberFormat.PositiveSign <- "++"
// Create an array of cultures.
let cultures = [| ci; CultureInfo.InvariantCulture |]
// Create an array of strings to parse.
let values = 
    [| "++1403"; "-0"; "+0"; "+16034" 
       string Int16.MinValue; "14.0"; "18012" |]
// Parse the strings using each culture.
for culture in cultures do
    printfn $"Parsing with the '{culture.Name}' culture."
    for value in values do
        try
            let number = UInt16.Parse(value, culture)
            printfn $"   Converted '{value}' to {number}."
        with
        | :? FormatException ->
            printfn $"   The format of '{value}' is invalid."
        | :? OverflowException ->
            printfn $"   '{value}' is outside the range of a UInt16 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.
Imports System.Globalization

Module Example
   Public Sub Main()
      ' Define a custom culture that uses "++" as a positive sign. 
      Dim ci As CultureInfo = New CultureInfo("")
      ci.NumberFormat.PositiveSign = "++"
      ' Create an array of cultures.
      Dim cultures() As CultureInfo = { ci, CultureInfo.InvariantCulture }
      ' Create an array of strings to parse.
      Dim values() As String = { "++1403", "-0", "+0", "+16034", _
                                 Int16.MinValue.ToString(), "14.0", "18012" }
      ' Parse the strings using each culture.
      For Each culture As CultureInfo In cultures
         Console.WriteLine("Parsing with the '{0}' culture.", culture.Name)
         For Each value As String In values
            Try
               Dim number As UShort = UInt16.Parse(value, culture)
               Console.WriteLine("   Converted '{0}' to {1}.", value, number)
            Catch e As FormatException
               Console.WriteLine("   The format of '{0}' is invalid.", value)
            Catch e As OverflowException
               Console.WriteLine("   '{0}' is outside the range of a UInt16 value.", value)
            End Try               
         Next
      Next
   End Sub
End Module
' 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][sign]digits[ws]

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

元素 说明
ws 可选空格。
签名 可选符号,如果 表示值零,则 s 为负号。
位数 从 0 到 9 的数字序列。

s 参数使用 NumberStyles.Integer 样式进行解释。 除了字节值的十进制数字外,只允许前导空格和尾随空格以及前导符号。 (如果负号存在, s 必须表示值零,否则方法将 OverflowException引发 .) 若要显式定义样式元素以及中可能存在 s的区域性特定格式设置信息,请使用 Parse(String, NumberStyles, IFormatProvider) 方法。

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

如果 providernullNumberFormatInfo 则使用当前区域性的 。

另请参阅

适用于

Parse(String, NumberStyles)

重要

此 API 不符合 CLS。

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

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

public:
 static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style);
public static ushort Parse (string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint16
static member Parse : string * System.Globalization.NumberStyles -> uint16
Public Shared Function Parse (s As String, style As NumberStyles) As UShort

参数

s
String

表示要转换的数字的字符串。 该字符串使用由 style 参数指定的样式来进行解释。

style
NumberStyles

枚举值的按位组合,这些枚举值指定 s 所允许的格式。 要指定的一个典型值为 Integer

返回

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

属性

例外

snull

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
open System
open System.Globalization

let values = [| " 214 "; "1,064"; "(0)"; "1241+"; " + 214 "; " +214 "; "2153.0"; "1e03"; "1300.0e-2" |]
let whitespace =  NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
let 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.
for value in values do
    printfn $"Attempting to convert '{value}':"
    for style in styles do
        try
            let number = UInt16.Parse(value, style)
            printfn $"   {style}: {number}"
        with :? FormatException ->
            printfn $"   {style}: Bad Format"
    printfn ""
// 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
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim values() As String = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.0e-2" }
      Dim whitespace As NumberStyles =  NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite
      Dim styles() As NumberStyles = { NumberStyles.None, _
                                       whitespace, _
                                       NumberStyles.AllowLeadingSign Or NumberStyles.AllowTrailingSign Or whitespace, _
                                       NumberStyles.AllowThousands Or NumberStyles.AllowCurrencySymbol, _
                                       NumberStyles.AllowExponent Or NumberStyles.AllowDecimalPoint }

      ' Attempt to convert each number using each style combination.
      For Each value As String In values
         Console.WriteLine("Attempting to convert '{0}':", value)
         For Each style As NumberStyles In styles
            Try
               Dim number As UShort = UInt16.Parse(value, style)
               Console.WriteLine("   {0}: {1}", style, number)
            Catch e As FormatException
               Console.WriteLine("   {0}: Bad Format", style)
            End Try         
         Next
         Console.WriteLine()
      Next
   End Sub
End Module
' The example displays 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符号符号时,参数使此方法重载非常有用。

根据 的值 styles 参数可能包括以下元素:

[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

方括号 ([ and ]) 中的元素是可选的。 如果 style 包含 NumberStyles.AllowHexSpecifier,则 s 参数可以包含以下元素:

[ws]hexdigits[ws]

下表对每个元素进行了描述。

元素 说明
ws 可选空格。 如果包含 标志,s则开头会出现空格;如果stylestyle包含 NumberStyles.AllowTrailingWhite 标志,则它可能出现在 末尾sNumberStyles.AllowLeadingWhite
$ 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyNegativePattern 当前区域性的 和 NumberFormatInfo.CurrencyPositivePattern 属性定义。 如果style包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 中s
sign 可选符号。 如果包含 标志,则符号可以出现在 的s开头;如果stylestyle包含 NumberStyles.AllowTrailingSign 标志,则它可能显示在 的末尾sNumberStyles.AllowLeadingSign 如果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包含 标志,则 s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

分析操作将忽略中 s 任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style

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

NumberStyles 除数字外允许的 s 元素
None 仅限 digits 元素。
AllowDecimalPoint 小数点 (.) 和 小数位数 元素。
AllowExponent “e”或“E”字符,指示指数表示法以及 exponential_digits
AllowLeadingWhite 开头的 sws 元素。
AllowTrailingWhite 位于 末尾的 sws 元素。
AllowLeadingSign 开头的s符号元素。
AllowTrailingSign 末尾的s符号元素。
AllowParentheses 用括号括住数值的 符号 元素。
AllowThousands 组分隔符 (,) 元素。
AllowCurrencySymbol $) 元素 (货币。
Currency 所有元素。 但是, s 不能表示十六进制数或指数表示法中的数字。
Float 开头或结尾处的 sws元素,符号位于 的s开头,小数点 (.) 符号。 参数 s 还可以使用指数表示法。
Number 、、 组分隔符 () 和小数点 (.) 元素。 signws
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) 调用 重载。

另请参阅

适用于

Parse(ReadOnlySpan<Char>, IFormatProvider)

将字符范围分析为值。

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

参数

s
ReadOnlySpan<Char>

要分析的字符范围。

provider
IFormatProvider

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

返回

分析 s的结果。

实现

适用于

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

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

参数

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

返回

分析 utf8Text的结果。

实现

适用于

Parse(String)

重要

此 API 不符合 CLS。

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

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

public:
 static System::UInt16 Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static ushort Parse (string s);
public static ushort Parse (string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> uint16
static member Parse : string -> uint16
Public Shared Function Parse (s As String) As UShort

参数

s
String

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

返回

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

属性

例外

snull

s 的格式不正确。

示例

以下示例调用 方法, 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
open System

let values = 
    [| "-0"; "17"; "-12"; "185"; "66012"; "+0" 
       ""; null; "16.1"; "28.0"; "1,034" |]
for value in values do
    try
        let number = UInt16.Parse value
        printfn $"'{value}' --> {number}"
    with
    | :? FormatException ->
        printfn $"'{value}' --> Bad Format"
    | :? OverflowException ->
        printfn $"'{value}' --> OverflowException"
    | :? ArgumentNullException ->
        printfn $"'{value}' --> Null"
// 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
Module Example
   Public Sub Main()
      Dim values() As String = { "-0", "17", "-12", "185", "66012", _ 
                                 "+0", "", Nothing, "16.1", "28.0", _
                                 "1,034" }
      For Each value As String In values
         Try
            Dim number As UShort = UInt16.Parse(value)
            Console.WriteLine("'{0}' --> {1}", value, number)
         Catch e As FormatException
            Console.WriteLine("'{0}' --> Bad Format", value)
         Catch e As OverflowException   
            Console.WriteLine("'{0}' --> OverflowException", value)
         Catch e As ArgumentNullException
            Console.WriteLine("'{0}' --> Null", value)
         End Try
      Next                                 
   End Sub
End Module
' 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][sign]digits[ws]

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

元素 说明
ws 可选空格。
sign 可选符号。 有效的符号字符由 NumberFormatInfo.NegativeSign 当前区域性的 和 NumberFormatInfo.PositiveSign 属性确定。 但是,负号符号只能与零一起使用;否则, 方法将 OverflowException引发 。
位数 从 0 到 9 的数字序列。 忽略任何前导零。

注意

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

通过使用 s 针对当前系统区域性初始化的 对象中的 System.Globalization.NumberFormatInfo 格式设置信息来分析 参数。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要使用特定区域性的格式设置信息分析字符串,请使用 Parse(String, IFormatProvider) 方法。

另请参阅

适用于