UInt16.Parse UInt16.Parse UInt16.Parse UInt16.Parse Method

定義

將數字的字串表示轉換成它的對等 16 位元不帶正負號的整數。Converts the string representation of a number to its 16-bit unsigned integer equivalent.

多載

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

將指定樣式和特定文化特性格式之數字的字串表示轉換成其對等 16 位元不帶正負號的整數。Converts the string representation of a number in a specified style and culture-specific format to its 16-bit unsigned integer equivalent.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

將指定樣式之數字的字串表示轉換成其對等 16 位元不帶正負號的整數。Converts the string representation of a number in a specified style to its 16-bit unsigned integer equivalent.

此方法不符合 CLS 標準。This method is not CLS-compliant. 符合 CLS 標準的替代項目為 Parse(String, NumberStyles)The CLS-compliant alternative is Parse(String, NumberStyles).

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

將數字的字串表示轉換成它的對等 16 位元不帶正負號的整數。Converts the string representation of a number to its 16-bit unsigned integer equivalent.

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

將指定之特定文化特性格式數字的字串表示轉換成它的對等 16 位元不帶正負號的整數。Converts the string representation of a number in a specified culture-specific format to its 16-bit unsigned integer equivalent.

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

重要

此 API 不符合 CLS 規範。

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

將指定樣式和特定文化特性格式之數字的字串表示轉換成其對等 16 位元不帶正負號的整數。Converts the string representation of a number in a specified style and culture-specific format to its 16-bit unsigned integer equivalent.

public:
 static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
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 String String String

字串,表示要轉換的數字。A string that represents the number to convert. 這個字串使用 style 參數指定的樣式來解譯。The string is interpreted by using the style specified by the style parameter.

style
NumberStyles NumberStyles NumberStyles NumberStyles

列舉值的位元組合,表示 s 中可以存在的樣式項目。A bitwise combination of enumeration values that indicate the style elements that can be present in s. 一般會指定的值是 IntegerA typical value to specify is Integer.

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

物件,提供關於 s 的特定文化特性格式資訊。An object that supplies culture-specific formatting information about s.

傳回

16 位元不帶正負號的整數,與 s 中所指定的數字相等。A 16-bit unsigned integer equivalent to the number specified in s.

例外狀況

style 不是 NumberStyles 值。style is not a NumberStyles value.

-或--or- style 不是 AllowHexSpecifierHexNumber 值的組合。style is not a combination of AllowHexSpecifier and HexNumber values.

s 的格式與 style 不相容。s is not in a format compliant with style.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number that is less than MinValue or greater than MaxValue.

-或--or- s 包含非零的小數數字。s includes non-zero, fractional digits.

範例

下列範例會使用Parse(String, NumberStyles, IFormatProvider)方法,將各種數位的字串表示轉換為16位不帶正負號的整數值。The following example uses the Parse(String, NumberStyles, IFormatProvider) method to convert various string representations of numbers to 16-bit unsigned integer values.

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

備註

參數會定義s參數中允許的樣式元素(例如空白字元或正數或負號符號),使剖析作業成功。 styleThe style parameter defines the style elements (such as white space or the positive or negative sign symbol) that are allowed in the s parameter for the parse operation to succeed. 它必須是NumberStyles列舉中的位旗標組合。It must be a combination of bit flags from the NumberStyles enumeration.

視的值style s而定, 參數可能包含下列元素:Depending on the value of style, the s parameter may include the following elements:

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

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 如果style包含NumberStyles.AllowHexSpecifier,則s參數可能包含下列元素:If style includes NumberStyles.AllowHexSpecifier, the s parameter may include the following elements:

[ws]hexdigits[ws][ws]hexdigits[ws]

下表說明每個元素。The following table describes each element.

元素Element 說明Description
wsws 選擇性的空白字元。Optional white space. s s style NumberStyles.AllowTrailingWhite如果包含style 旗標,空白字元可能會出現在開頭,而且如果包含旗標,它可以NumberStyles.AllowLeadingWhite出現在的結尾。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 其在字串中的位置CurrencyPositivePattern是由provider參數的GetFormat方法所NumberFormatInfo傳回之物件的屬性所定義。Its position in the string is defined by the CurrencyPositivePattern property of the NumberFormatInfo object that is returned by the GetFormat method of the provider parameter. 如果s style包含旗標,貨幣符號可能會出現在中。NumberStyles.AllowCurrencySymbolThe currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正負號。An optional sign. OverflowException 如果s包含負號且代表非零的數位,則方法會擲回。)s如果style NumberStyles.AllowTrailingSign包含旗標s ,則正負號可能會出現在開頭,而且如果包含旗標,它可以出現在的結尾。 style NumberStyles.AllowLeadingSign(The method throws an OverflowException if s includes a negative sign and represents a non-zero number.) The sign can appear at the beginning of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear the end of s if style includes the NumberStyles.AllowTrailingSign flag. 如果包含s style 旗標,則可以在中使用括弧來NumberStyles.AllowParentheses表示負數值。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
digitsdigits 從0到9的數位序列。A sequence of digits from 0 through 9.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果s style包含旗標,則目前文化特性的小數點符號可能會出現在中。NumberStyles.AllowDecimalPointThe current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digitsfractional_digits style 如果NumberStyles.AllowExponent包含旗標,則為數字0-9 的一個或多個專案,如果沒有的話,則出現一或多個數位0。One or more occurrences of the digit 0-9 if style includes the NumberStyles.AllowExponent flag, or one or more occurrences of the digit 0 if it does not. 只有在包含s style 旗標時,小數位數才會出現在中。NumberStyles.AllowDecimalPointFractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag.
EE "E" 或 "E" 字元, 表示該值是以指數 (科學) 標記法表示。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. 如果s 包含旗NumberStyles.AllowExponent標, 參數可以表示指數標記法中的數位。 styleThe s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
exponential_digitsexponential_digits 從0到9的數位序列。A sequence of digits from 0 through 9. 如果s 包含旗NumberStyles.AllowExponent標, 參數可以表示指數標記法中的數位。 styleThe s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 從0到 f 的十六進位數位序列, 或0到 F。A sequence of hexadecimal digits from 0 through f, or 0 through F.

注意

不論style引數的值為何, 剖析作業都會s忽略中的任何終止 NUL (U + 0000) 字元。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只有十進位數(對應至NumberStyles.None樣式)的字串一律會成功剖析。A string with decimal digits only (which corresponds to the NumberStyles.None style) always parses successfully. 在此輸入字串NumberStyles中, 大多數可能存在但不需要存在的其餘成員控制項元素。Most of the remaining NumberStyles members control elements that may be present, but are not required to be present, in this input string. 下表指出個別NumberStyles成員如何影響中s可能存在的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in s.

非複合NumberStylesNon-composite NumberStyles values 除了數位之外s , 中允許的元素Elements permitted in s in addition to digits
NumberStyles.None 僅限十進位數。Decimal digits only.
NumberStyles.AllowDecimalPoint 小數點(.)和fractional_digits元素。The decimal point (.) and fractional_digits elements. 不過,如果 style 不包含NumberStyles.AllowExponent旗標,則fractional_digits只能包含一或多個0位數,否則OverflowException會擲回。However, if style does not include the NumberStyles.AllowExponent flag, fractional_digits must consist of only one or more 0 digits; otherwise, an OverflowException is thrown.
NumberStyles.AllowExponent "E" 或 "E" 字元,表示指數標記法,以及exponential_digitsThe "e" or "E" character, which indicates exponential notation, along with exponential_digits.
NumberStyles.AllowLeadingWhite 開頭sws元素。The ws element at the beginning of s.
NumberStyles.AllowTrailingWhite 結尾sws元素。The ws element at the end of s.
NumberStyles.AllowLeadingSign 數位的正負號。A sign before digits.
NumberStyles.AllowTrailingSign 數位後的正負號。A sign after digits.
NumberStyles.AllowParentheses 數位前後的括弧,表示負數值。Parentheses before and after digits to indicate a negative value.
NumberStyles.AllowThousands 群組分隔符號 (,) 元素。The group separator (,) element.
NumberStyles.AllowCurrencySymbol Currency ($) 元素。The currency ($) element.

s如果使用NumberStyles.AllowHexSpecifier旗標, 必須是十六進位值。If the NumberStyles.AllowHexSpecifier flag is used, s must be a hexadecimal value. 有效的十六進位數位為0到9、a 到 f,以及 A 到 F。不支援前置詞(例如 "0x"),而且會導致剖析作業失敗。Valid hexadecimal digits are 0 through 9, a through f, and A through F. A prefix, such as "0x", is not supported and causes the parse operation to fail. 唯一可以合併NumberStyles.AllowHexSpecifier的其他旗標是NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with NumberStyles.AllowHexSpecifier are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (列舉包含複合數位樣式, NumberStyles.HexNumber其中同時包含空白字元旗標)。 NumberStyles(The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white-space flags.)

注意

如果參數是十六進位數位的字串表示,其前面不能加上與十六進位數位區別的任何裝飾0x&h例如或)。 sIf the s parameter is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. 這會導致剖析作業擲回例外狀況。This causes the parse operation to throw an exception.

IFormatProvider 參數是GetFormatsNumberFormatInfo執行方式,其方法會傳回物件,以提供有關格式的特定文化特性資訊。 providerThe provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of s. 有三種方式可使用provider參數, 將自訂格式資訊提供給剖析作業:There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

如果providernull ,NumberFormatInfo則會使用目前文化特性的物件。If provider is null, the NumberFormatInfo object for the current culture is used.

另請參閱

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

重要

此 API 不符合 CLS 規範。

[System.CLSCompliant(false)]
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = null);
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 = null) As UShort

參數

傳回

Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles) Parse(String, NumberStyles)

重要

此 API 不符合 CLS 規範。

將指定樣式之數字的字串表示轉換成其對等 16 位元不帶正負號的整數。Converts the string representation of a number in a specified style to its 16-bit unsigned integer equivalent.

此方法不符合 CLS 標準。This method is not CLS-compliant. 符合 CLS 標準的替代項目為 Parse(String, NumberStyles)The CLS-compliant alternative is 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);
static member Parse : string * System.Globalization.NumberStyles -> uint16
Public Shared Function Parse (s As String, style As NumberStyles) As UShort

參數

s
String String String String

字串,表示要轉換的數字。A string that represents the number to convert. 這個字串使用 style 參數指定的樣式來解譯。The string is interpreted by using the style specified by the style parameter.

style
NumberStyles NumberStyles NumberStyles NumberStyles

指定 s 可以使用的格式之列舉值的位元組合。A bitwise combination of the enumeration values that specify the permitted format of s. 一般會指定的值是 IntegerA typical value to specify is Integer.

傳回

16 位元不帶正負號的整數,與 s 中所指定的數字相等。A 16-bit unsigned integer equivalent to the number specified in s.

例外狀況

style 不是 NumberStyles 值。style is not a NumberStyles value.

-或--or- style 不是 AllowHexSpecifierHexNumber 值的組合。style is not a combination of AllowHexSpecifier and HexNumber values.

s 的格式與 style 不相容。s is not in a format compliant with style.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

-或--or- s 包含非零的小數數字。s includes non-zero, fractional digits.

範例

下列範例會嘗試使用多個NumberStyles值來剖析字串陣列中的每個元素。The following example tries to parse each element in a string array by using a number of NumberStyles values.

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
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

備註

參數會定義可讓剖析作業成功的s參數中所允許的樣式元素 (例如空白字元、正數或負號符號、群組分隔符號符號, 或小數點符號)。 styleThe style parameter defines the style elements (such as white space, the positive or negative sign symbol, the group separator symbol, or the decimal point symbol) that are allowed in the s parameter for the parse operation to succeed. style必須是NumberStyles列舉中的位旗標組合。style must be a combination of bit flags from the NumberStyles enumeration. 當包含十六進位值的字串表示時, 如果只有在執行時間才知道所表示s的數字系統 (十進位或十六進位), 或當您s style想要在中s不允許空白字元或符號符號。The style parameter makes this method overload useful when s contains the string representation of a hexadecimal value, when the number system (decimal or hexadecimal) represented by s is known only at run time, or when you want to disallow white space or a sign symbol in s.

視的值style s而定, 參數可能包含下列元素:Depending on the value of style, the s parameter may include the following elements:

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

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 如果style包含NumberStyles.AllowHexSpecifier ,s參數可能會包含下列元素:If style includes NumberStyles.AllowHexSpecifier, the s parameter may contain the following elements:

[ws]hexdigits[ws][ws]hexdigits[ws]

下表說明每個元素。The following table describes each element.

元素Element 說明Description
wsws 選擇性的空白字元。Optional white space. s s style NumberStyles.AllowTrailingWhite如果包含style 旗標,空白字元可能會出現在開頭,而且如果包含旗標,它可以NumberStyles.AllowLeadingWhite出現在的結尾。White space can appear at the start of s if style includes the NumberStyles.AllowLeadingWhite flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 其在字串中的位置是由NumberFormatInfo.CurrencyNegativePattern目前文化特性的和NumberFormatInfo.CurrencyPositivePattern屬性所定義。Its position in the string is defined by the NumberFormatInfo.CurrencyNegativePattern and NumberFormatInfo.CurrencyPositivePattern properties of the current culture. 如果s style包含旗標,則目前文化特性的貨幣符號可能會出現在中。NumberStyles.AllowCurrencySymbolThe current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正負號。An optional sign. s s style NumberStyles.AllowTrailingSign如果包含style 旗標,則正負號可能會出現在開頭,而且如果包含旗標,它可以NumberStyles.AllowLeadingSign出現在的結尾。The sign can appear at the start of s if style includes the NumberStyles.AllowLeadingSign flag, and it can appear at the end of s if style includes the NumberStyles.AllowTrailingSign flag. 如果包含s style 旗標,則可以在中使用括弧來NumberStyles.AllowParentheses表示負數值。Parentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag. 不過,負號符號只能搭配零使用;否則,方法OverflowException會擲回。However, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.
digitsdigits

fractional_digitsfractional_digits

exponential_digitsexponential_digits
從0到9的數位序列。A sequence of digits from 0 through 9. 針對fractional_digits,只有數位0是有效的。For fractional_digits, only the digit 0 is valid.
, 文化特性特定的群組分隔符號符號。A culture-specific group separator symbol. 如果s style包含旗標,則目前文化特性的群組分隔符號可能會出現在中。NumberStyles.AllowThousandsThe current culture's group separator can appear in s if style includes the NumberStyles.AllowThousands flag.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果s style包含旗標,則目前文化特性的小數點符號可能會出現在中。NumberStyles.AllowDecimalPointThe current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. 只有數位0可以顯示為小數位數,剖析作業才會成功;如果fractional_digits包含任何其他數位, FormatException則會擲回。Only the digit 0 can appear as a fractional digit for the parse operation to succeed; if fractional_digits includes any other digit, a FormatException is thrown.
EE "E" 或 "E" 字元, 表示該值是以指數 (科學) 標記法表示。The "e" or "E" character, which indicates that the value is represented in exponential (scientific) notation. 如果s 包含旗NumberStyles.AllowExponent標, 參數可以表示指數標記法中的數位。 styleThe s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag.
hexdigitshexdigits 從0到 f 的十六進位數位序列, 或0到 F。A sequence of hexadecimal digits from 0 through f, or 0 through F.

注意

不論style引數的值為何, 剖析作業都會s忽略中的任何終止 NUL (U + 0000) 字元。Any terminating NUL (U+0000) characters in s are ignored by the parsing operation, regardless of the value of the style argument.

只包含數位的字串 (對應至NumberStyles.None樣式) 一律會成功剖析。A string with digits only (which corresponds to the NumberStyles.None style) always parses successfully. 在輸入字串中NumberStyles , 大多數可能存在但不需要存在的其餘成員控制項元素。Most of the remaining NumberStyles members control elements that may be present, but are not required to be present, in the input string. 下表指出個別NumberStyles成員如何影響中s可能存在的元素。The following table indicates how individual NumberStyles members affect the elements that may be present in s.

NumberStylesNumberStyles value 除了數位之外s , 中允許的元素Elements permitted in s in addition to digits
None 僅限數位元素。The digits element only.
AllowDecimalPoint 小數點 (.) 和小數位數元素。The decimal point (.) and fractional-digits elements.
AllowExponent "E" 或 "E" 字元,表示指數標記法,以及exponential_digitsThe "e" or "E" character, which indicates exponential notation, along with exponential_digits.
AllowLeadingWhite 開頭sws元素。The ws element at the start of s.
AllowTrailingWhite 結尾sws元素。The ws element at the end of s.
AllowLeadingSign 開頭s為的sign元素。The sign element at the start of s.
AllowTrailingSign 結尾ssign元素。The sign element at the end of s.
AllowParentheses 以括弧形式括住數值的sign元素。The sign element in the form of parentheses enclosing the numeric value.
AllowThousands 群組分隔符號 (,) 元素。The group separator (,) element.
AllowCurrencySymbol Currency ($) 元素。The currency ($) element.
Currency 所有元素。All elements. 不過, s不能以指數標記法表示十六進位數位或數位。However, s cannot represent a hexadecimal number or a number in exponential notation.
Float 在開頭或結尾s的 ws 元素, 在的開頭s和小數點 (.) 符號之間進行正負號The ws element at the start or end of s, sign at the start of s, and the decimal point (.) symbol. s參數也可以使用指數標記法。The s parameter can also use exponential notation.
Number 、、群組分隔符號 () 和小數點 (.) 元素。 ws signThe ws, sign, group separator (,), and decimal point (.) elements.
Any 所有元素。All elements. 不過, s不能代表十六進位數位。However, s cannot represent a hexadecimal number.

不同于允許NumberStyles (但不需要) 在中s NumberStyles.AllowHexSpecifier出現特定樣式元素的其他值, 樣式值表示中s的個別數位字元一律會解讀為十六進位字元。Unlike the other NumberStyles values, which allow for, but do not require, the presence of particular style elements in s, the NumberStyles.AllowHexSpecifier style value means that the individual numeric characters in s are always interpreted as hexadecimal characters. 有效的十六進位字元是0-9、A-f 和 a-f。Valid hexadecimal characters are 0-9, A-F, and a-f. 不支援前置詞(例如 "0x"),而且會導致剖析作業失敗。A prefix, such as "0x", is not supported and causes the parse operation to fail. 唯一可以與style參數結合的其他旗標為NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be combined with the style parameter are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. (列舉包含複合數位樣式, NumberStyles.HexNumber其中同時包含空白字元旗標)。 NumberStyles(The NumberStyles enumeration includes a composite number style, NumberStyles.HexNumber, that includes both white-space flags.)

注意

如果s是十六進位數位的字串表示, 其前面不能加上與十六進位數位區別的任何0x裝飾&h(例如或)。If s is the string representation of a hexadecimal number, it cannot be preceded by any decoration (such as 0x or &h) that differentiates it as a hexadecimal number. 這會導致轉換失敗。This causes the conversion to fail.

參數會使用針對目前系統文化特性初始化之NumberFormatInfo物件中的格式資訊來進行剖析。 sThe s parameter is parsed by using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. 若要指定用於剖析作業的文化特性, 其格式資訊會呼叫Parse(String, NumberStyles, IFormatProvider)多載。To specify the culture whose formatting information is used for the parse operation, call the Parse(String, NumberStyles, IFormatProvider) overload.

另請參閱

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

重要

此 API 不符合 CLS 規範。

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

將數字的字串表示轉換成它的對等 16 位元不帶正負號的整數。Converts the string representation of a number to its 16-bit unsigned integer equivalent.

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

參數

s
String String String String

字串,表示要轉換的數字。A string that represents the number to convert.

傳回

16 位元不帶正負號的整數,與 s 中所包含的數字相等。A 16-bit unsigned integer equivalent to the number contained in s.

例外狀況

s 的格式不正確。s is not in the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

範例

下列範例會呼叫Parse(String)方法,將字串陣列中的每個元素轉換成不帶正負號的16位整數。The following example calls the Parse(String) method to convert each element in a string array to an unsigned 16-bit integer.

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
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參數應該是下列格式之數位的字串表示。The s parameter should be the string representation of a number in the following form.

[ws][sign]digits[ws][ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。Elements in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

元素Element 說明Description
wsws 選擇性的空白字元。Optional white space.
簽署sign 選擇性的正負號。An optional sign. 有效的正負號字元是由NumberFormatInfo.NegativeSign目前NumberFormatInfo.PositiveSign文化特性的和屬性所決定。Valid sign characters are determined by the NumberFormatInfo.NegativeSign and NumberFormatInfo.PositiveSign properties of the current culture. 不過,負號符號只能搭配零使用;否則,方法OverflowException會擲回。However, the negative sign symbol can be used only with zero; otherwise, the method throws an OverflowException.
digitsdigits 範圍從0到9的數位序列。A sequence of digits ranging from 0 to 9. 任何前置的零都會被忽略。Any leading zeros are ignored.

注意

s參數所指定的字串會NumberStyles.Integer使用樣式來加以解讀。The string specified by the s parameter is interpreted by using the NumberStyles.Integer style. 它不能包含任何群組分隔符號或小數分隔符號, 而且不能有小數部分。It cannot contain any group separators or decimal separator, and it cannot have a decimal portion.

參數會使用針對目前系統文化特性初始化之System.Globalization.NumberFormatInfo物件中的格式資訊來進行剖析。 sThe s parameter is parsed by using the formatting information in a System.Globalization.NumberFormatInfo object that is initialized for the current system culture. 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfoFor more information, see NumberFormatInfo.CurrentInfo. 若要使用特定文化特性的格式資訊來剖析字串, 請使用Parse(String, IFormatProvider)方法。To parse a string by using the formatting information of a specific culture, use the Parse(String, IFormatProvider) method.

另請參閱

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

重要

此 API 不符合 CLS 規範。

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

將指定之特定文化特性格式數字的字串表示轉換成它的對等 16 位元不帶正負號的整數。Converts the string representation of a number in a specified culture-specific format to its 16-bit unsigned integer equivalent.

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

參數

s
String String String String

字串,表示要轉換的數字。A string that represents the number to convert.

provider
IFormatProvider IFormatProvider IFormatProvider IFormatProvider

物件,提供關於 s 的特定文化特性格式資訊。An object that supplies culture-specific formatting information about s.

傳回

16 位元不帶正負號的整數,與 s 中所指定的數字相等。A 16-bit unsigned integer equivalent to the number specified in s.

例外狀況

s 的格式不正確。s is not in the correct format.

s 代表小於 MinValue 或大於 MaxValue 的數字。s represents a number less than MinValue or greater than MaxValue.

範例

下列範例會具現化使用兩個加號(+ +)做為其正號的自訂文化特性。The following example instantiates a custom culture that uses two plus signs (++) as its positive sign. 然後,它會Parse(String, IFormatProvider)呼叫方法,使用CultureInfo同時代表這個自訂文化特性和不因文化特性而異的物件來剖析字串陣列。It then calls the Parse(String, IFormatProvider) method to parse an array of strings by using CultureInfo objects that represent both this custom culture and the invariant culture.

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.
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參數包含下列格式的數位:The s parameter contains a number of the form:

[ws][sign]digits[ws][ws][sign]digits[ws]

以方括弧([和])括住的專案是選擇性的。Items in square brackets ([ and ]) are optional. 下表說明每個元素。The following table describes each element.

元素Element 說明Description
wsws 選擇性的空白字元。Optional white space.
Signsign 選擇性的正負號,如果s表示值為零,則為負號。An optional sign, or a negative sign if s represents the value zero.
digitsdigits 範圍從0到9的數位序列。A sequence of digits ranging from 0 to 9.

S 參數是使用NumberStyles.Integer樣式來解讀。The s parameter is interpreted using the NumberStyles.Integer style. 除了位元組值的十進位數以外,只允許開頭和尾端空格加上前置正負號。In addition to the byte value's decimal digits, only leading and trailing spaces along with a leading sign is allowed. (如果有負號,則必須s代表值為零,否則方法OverflowException會擲回。)若要明確定義樣式專案,以及可以出現在中s的特定文化特性格式資訊,請Parse(String, NumberStyles, IFormatProvider)使用方法。(If the negative sign is present, s must represent a value of zero or the method throws an OverflowException.) To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Parse(String, NumberStyles, IFormatProvider) method.

IFormatProvider 參數是GetFormatsNumberFormatInfo執行方式,其方法會傳回物件,以提供有關格式的特定文化特性資訊。 providerThe provider parameter is an IFormatProvider implementation whose GetFormat method returns a NumberFormatInfo object that provides culture-specific information about the format of s. 有三種方式可使用provider參數, 將自訂格式資訊提供給剖析作業:There are three ways to use the provider parameter to supply custom formatting information to the parse operation:

如果provider NumberFormatInfonull,則會使用目前文化特性的。If provider is null, the NumberFormatInfo for the current culture is used.

另請參閱

適用於