Int16.Parse 方法

定義

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

多載

Parse(String, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 16 位元帶正負號的整數。Converts the span representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent.

Parse(String, NumberStyles)

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

Parse(String)

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

Parse(String, IFormatProvider)

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

Parse(String, NumberStyles, IFormatProvider)

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

public:
 static short Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public static short Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> int16
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Short

參數

s
String

字串,包含要轉換的數字。A string containing a number to convert.

style
NumberStyles

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

provider
IFormatProvider

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

傳回

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

例外狀況

snulls is null.

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.

範例

下列範例會使用各種 styleprovider 參數來剖析 Int16 值的字串表示。The following example uses a variety of style and provider parameters to parse the string representations of Int16 values.

String^ value;
Int16 number;
NumberStyles style;

// Parse string using "." as the thousands separator 
// and " " as the decimal separator.
value = "19 694,00";
style = NumberStyles::AllowDecimalPoint | NumberStyles::AllowThousands;
CultureInfo^ provider = gcnew CultureInfo("fr-FR");

number = Int16::Parse(value, style, provider);
Console::WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '19 694,00' converted to 19694.

try
{
   number = Int16::Parse(value, style, CultureInfo::InvariantCulture);
   Console::WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '19 694,00'.

// Parse string using "$" as the currency symbol for en_GB and
// en-US cultures.
value = "$6,032.00";
style = NumberStyles::Number | NumberStyles::AllowCurrencySymbol;
provider = gcnew CultureInfo("en-GB");

try
{
   number = Int16::Parse(value, style, CultureInfo::InvariantCulture);
   Console::WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '$6,032.00'.
                        
provider = gcnew CultureInfo("en-US");
number = Int16::Parse(value, style, provider);
Console::WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '$6,032.00' converted to 6032.      
string value;
short number;
NumberStyles style;
CultureInfo provider;

// Parse string using "." as the thousands separator 
// and " " as the decimal separator.
value = "19 694,00";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
provider = new CultureInfo("fr-FR");

number = Int16.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '19 694,00' converted to 19694.

try
{
   number = Int16.Parse(value, style, CultureInfo.InvariantCulture);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '19 694,00'.

// Parse string using "$" as the currency symbol for en_GB and
// en-US cultures.
value = "$6,032.00";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
provider = new CultureInfo("en-GB");

try
{
   number = Int16.Parse(value, style, CultureInfo.InvariantCulture);
   Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
//    Unable to parse '$6,032.00'.
                        
provider = new CultureInfo("en-US");
number = Int16.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
//    '$6,032.00' converted to 6032.      
Dim value As String
Dim number As Short
Dim style As NumberStyles
Dim provider As CultureInfo

' Parse string using "." as the thousands separator 
' and " " as the decimal separator.
value = "19 694,00"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
provider = New CultureInfo("fr-FR")

number = Int16.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '19 694,00' converted to 19694.

Try
   number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
'    Unable to parse '19 694,00'.

' Parse string using "$" as the currency symbol for en_GB and
' en-US cultures.
value = "$6,032.00"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
provider = New CultureInfo("en-GB")

Try
   number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
   Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
'    Unable to parse '$6,032.00'.
                        
provider = New CultureInfo("en-US")
number = Int16.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
'    '$6,032.00' converted to 6032.      

備註

style 參數會定義在 s 參數中允許的樣式元素(例如空白字元或正號),使剖析作業成功。The style parameter defines the style elements (such as white space or the positive sign) 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:

atl-ws-01[$]簽訂[數位,] 數位 [. fractional_digits] [e [sign] 數位] [ws][ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]

或者,如果 style 包含 AllowHexSpecifierOr, if style includes AllowHexSpecifier:

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

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

項目Element 說明Description
wsws 選擇性的空白字元。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則 ss 開頭會出現空白字元。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 其在字串中的位置是由目前文化特性的 NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo.CurrencyNegativePattern 屬性所定義。Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern and NumberFormatInfo.CurrencyNegativePattern property of the current culture. 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可能會出現在 s 中。The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正負號。An optional sign. 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則正負號可以出現在 s 的開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,它可以出現在 s 的結尾。The sign can appear at the beginning 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. 如果 style 包含 NumberStyles.AllowParentheses 旗標,則可以在 s 中使用括弧來表示負數值。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 thousands separator symbol. 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的千位分隔符號可能會出現在 s 中。The current culture's thousands separator symbol can appear in s if style includes the NumberStyles.AllowThousands flag.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digitsfractional_digits 0位數的序列。A sequence of the 0 digit. 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,小數數位就會出現在 s 中。Fractional digits can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. 如果fractional_digits中出現0以外的任何數位,則方法會擲回 OverflowExceptionIf any digit other than 0 appears in fractional_digits, the method throws an OverflowException.
ee ' E ' 或 ' E ' 字元,表示 s 可以用指數標記法表示。The 'e' or 'E' character, which indicates that s can be represented in exponential notation. 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數可以表示指數標記法中的數位。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag. 不過,s 參數必須代表 Int16 資料類型範圍內的數位,而且不能有非零的小數部分。However, the s parameter must represent a number in the range of the Int16 data type and cannot have a non-zero fractional component.
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 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.

非複合 NumberStyles 值Non-composite NumberStyles values 除了數位之外,s 中允許的元素Elements permitted in s in addition to digits
NumberStyles.None 僅限十進位數。Decimal digits only.
NumberStyles.AllowDecimalPoint .The . fractional_digits元素。and fractional_digits elements. 不過, fractional_digits只能包含一或多個0位數,或擲回 OverflowExceptionHowever, fractional_digits must consist of only one or more 0 digits or an OverflowException is thrown.
NumberStyles.AllowExponent s 參數也可以使用指數標記法。The s parameter can also use exponential notation.
NumberStyles.AllowLeadingWhite s開頭的ws元素。The ws element at the beginning of s.
NumberStyles.AllowTrailingWhite s結尾的ws元素。The ws element at the end of s.
NumberStyles.AllowLeadingSign 正負號可以出現在數位前面。A sign can appear before digits.
NumberStyles.AllowTrailingSign 正負號可以在數位之後出現。A sign can appear after digits.
NumberStyles.AllowParentheses 以括弧形式括住數值的sign元素。The sign element in the form of parentheses enclosing the numeric value.
NumberStyles.AllowThousands 元素。The , element.
NumberStyles.AllowCurrencySymbol $ 元素。The $ element.

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是不含前置詞之十六進位值的字串表示。If the NumberStyles.AllowHexSpecifier flag is used, s must be the string representation of a hexadecimal value without a prefix. 例如,"9AF3" 會成功剖析,但 "0x9AF3" 不會。For example, "9AF3" parses successfully, but "0x9AF3" does not.. style 中唯一可以出現的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be present in style are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. NumberStyles 列舉具有複合數位樣式,NumberStyles.HexNumber,其中同時包含空白字元旗標)。(The NumberStyles enumeration has a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

provider 參數是 IFormatProvider 的實作為,其 GetFormat 方法會取得 NumberFormatInfo 物件。The provider parameter is an IFormatProvider implementation whose GetFormat method obtains a NumberFormatInfo object. NumberFormatInfo 物件提供有關 s格式的特定文化特性資訊。The NumberFormatInfo object provides culture-specific information about the format of s. 如果 null``provider,則會使用目前文化特性的 NumberFormatInfo 物件。If provider is null, the NumberFormatInfo object for the current culture is used.

另請參閱

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 16 位元帶正負號的整數。Converts the span representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent.

public static short Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = null);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> int16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = null) As Short

參數

s
ReadOnlySpan<Char>

範圍,其包含代表所要轉換數字的字元。A span containing the characters representing the number to convert.

style
NumberStyles

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

provider
IFormatProvider

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

傳回

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

Parse(String, NumberStyles)

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

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

參數

s
String

字串,包含要轉換的數字。A string containing a number to convert.

style
NumberStyles

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

傳回

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

例外狀況

snulls is null.

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.

範例

下列範例會使用 Int16.Parse(String, NumberStyles) 方法,利用 en-us 文化特性來剖析 Int16 值的字串表示。The following example uses the Int16.Parse(String, NumberStyles) method to parse the string representations of Int16 values using the en-US culture.

using namespace System;
using namespace System::Globalization;

ref class ParseSample
{
public:
   static void Main()
   {
      String^ value;
      NumberStyles style;

      // Parse a number with a thousands separator (throws an exception).
      value = "14,644";
      style = NumberStyles::None;
      ParseSample::ParseToInt16(value, style);
      
      style = NumberStyles::AllowThousands;
      ParseToInt16(value, style);
      
      // Parse a number with a thousands separator and decimal point.
      value = "14,644.00";
      style = NumberStyles::AllowThousands | NumberStyles::Integer |
              NumberStyles::AllowDecimalPoint;
      ParseToInt16(value, style);
      
      // Parse a number with a fractional component (throws an exception).
      value = "14,644.001";
      ParseToInt16(value, style);
      
      // Parse a number in exponential notation.
      value = "145E02";
      style = style | NumberStyles::AllowExponent;
      ParseToInt16(value, style);
      
      // Parse a number in exponential notation with a positive sign.
      value = "145E+02";
      ParseToInt16(value, style);
      
      // Parse a number in exponential notation with a negative sign
      // (throws an exception).
      value = "145E-02";
      ParseToInt16(value, style);
   }

private:
   static void ParseToInt16(String^ value, NumberStyles style)
   {
      try
      {
         Int16 number = Int16::Parse(value, style);
         Console::WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException ^e)
      {
         Console::WriteLine("Unable to parse '{0}' with style {1}.", value, 
                            style);
      }
      catch (OverflowException ^e)
      {
         Console::WriteLine("'{0}' is out of range of the Int16 type.", value);
      }
   }
};

int main()
{
    ParseSample::Main();
    Console::ReadLine();
    return 0;
}
// The example displays the following output:
//       Unable to parse '14,644' with style None.
//       Converted '14,644' to 14644.
//       Converted '14,644.00' to 14644.
//       '14,644.001' is out of range of the Int16 type.
//       Converted '145E02' to 14500.
//       Converted '145E+02' to 14500.
//       '145E-02' is out of range of the Int16 type.
using System;
using System.Globalization;

public class ParseSample
{
   public static void Main()
   {
      string value; 
      NumberStyles style;
      
      // Parse a number with a thousands separator (throws an exception).
      value = "14,644";
      style = NumberStyles.None;
      ParseToInt16(value, style);
      
      style = NumberStyles.AllowThousands;
      ParseToInt16(value, style);
      
      // Parse a number with a thousands separator and decimal point.
      value = "14,644.00";
      style = NumberStyles.AllowThousands | NumberStyles.Integer |
              NumberStyles.AllowDecimalPoint;
      ParseToInt16(value, style);
      
      // Parse a number with a fractional component (throws an exception).
      value = "14,644.001";
      ParseToInt16(value, style);
      
      // Parse a number in exponential notation.
      value = "145E02";
      style = style | NumberStyles.AllowExponent;
      ParseToInt16(value, style);
      
      // Parse a number in exponential notation with a positive sign.
      value = "145E+02";
      ParseToInt16(value, style);
      
      // Parse a number in exponential notation with a negative sign
      // (throws an exception).
      value = "145E-02";
      ParseToInt16(value, style);
   }

   private static void ParseToInt16(string value, NumberStyles style)
   {
      try
      {
         short number = Int16.Parse(value, style);
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to parse '{0}' with style {1}.", value, 
                           style.ToString());
      }
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is out of range of the Int16 type.", value);
      }
   }   
}
// The example displays the following output to the console:
//       Unable to parse '14,644' with style None.
//       Converted '14,644' to 14644.
//       Converted '14,644.00' to 14644.
//       '14,644.001' is out of range of the Int16 type.
//       Converted '145E02' to 14500.
//       Converted '145E+02' to 14500.
//       '145E-02' is out of range of the Int16 type.
Imports System.Globalization

Module ParseSample
   Public Sub Main()
      Dim value As String 
      Dim style As NumberStyles
      
      ' Parse a number with a thousands separator (throws an exception).
      value = "14,644"
      style = NumberStyles.None
      ParseToInt16(value, style)
      
      style = NumberStyles.AllowThousands
      ParseToInt16(value, style)
      
      ' Parse a number with a thousands separator and decimal point.
      value = "14,644.00"
      style = NumberStyles.AllowThousands Or NumberStyles.Integer Or _
              NumberStyles.AllowDecimalPoint
      ParseToInt16(value, style)
      
      ' Parse a number with a fractional component (throws an exception).
      value = "14,644.001"
      ParseToInt16(value, style)
      
      ' Parse a number in exponential notation.
      value = "145E02"
      style = style Or NumberStyles.AllowExponent
      ParseToInt16(value, style)
      
      ' Parse a number in exponential notation with a positive sign.
      value = "145E+02"
      ParseToInt16(value, style)
      
      ' Parse a number in exponential notation with a negative sign
      ' (throws an exception).
      value = "145E-02"
      ParseToInt16(value, style)
   End Sub
   
   Private Sub ParseToInt16(value As String, style As NumberStyles)
      Try
         Dim number As Short = Int16.Parse(value, style)
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Catch e As FormatException
         Console.WriteLine("Unable to parse '{0}' with style {1}.", value, _
                           style.ToString())
      Catch e As OverflowException
         Console.WriteLine("'{0}' is out of range of the Int16 type.", value)
      End Try
   End Sub   
End Module
' The example displays the following output to the console:
'       Unable to parse '14,644' with style None.
'       Converted '14,644' to 14644.
'       Converted '14,644.00' to 14644.
'       '14,644.001' is out of range of the Int16 type.
'       Converted '145E02' to 14500.
'       Converted '145E+02' to 14500.
'       '145E-02' is out of range of the Int16 type.

備註

style 參數會定義可在 s 參數中允許的樣式元素(例如空白字元或符號符號),使剖析作業成功。The style parameter defines the style elements (such as white space or a 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:

atl-ws-01[$]簽訂[數位,] 數位 [. fractional_digits] [e [sign] 數位] [ws][ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]

或者,如果 style 包含 AllowHexSpecifierOr, if style includes AllowHexSpecifier:

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

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

項目Element 說明Description
wsws 選擇性的空白字元。Optional white space. 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或如果 style 包含 NumberStyles.AllowTrailingWhite 旗標,則 ss 開頭會出現空白字元。White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the NumberStyles.AllowTrailingWhite flag.
$ 特定文化特性的貨幣符號。A culture-specific currency symbol. 其在字串中的位置是由目前文化特性的 NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo.CurrencyNegativePattern 屬性所定義。Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern and NumberFormatInfo.CurrencyNegativePattern property of the current culture. 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可能會出現在 s 中。The current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
簽署sign 選擇性的正負號。An optional sign. 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則正負號可以出現在 s 的開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,它可以出現在 s 的結尾。The sign can appear at the beginning 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. 如果 style 包含 NumberStyles.AllowParentheses 旗標,則可以在 s 中使用括弧來表示負數值。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 thousands separator symbol. 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的千位分隔符號可能會出現在 s 中。The current culture's thousands separator symbol can appear in s if style includes the NumberStyles.AllowThousands flag.
.. 文化特性特定的小數點符號。A culture-specific decimal point symbol. 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可能會出現在 s 中。The current culture's decimal point symbol can appear in s if style includes the NumberStyles.AllowDecimalPoint flag.
fractional_digitsfractional_digits 0位數的序列。A sequence of the 0 digit. 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,小數數位就會出現在 s 中。Fractional digits can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. 如果fractional_digits中出現0以外的任何數位,則方法會擲回 OverflowExceptionIf any digit other than 0 appears in fractional_digits, the method throws an OverflowException.
ee ' E ' 或 ' E ' 字元,表示 s 可以用指數標記法表示。The 'e' or 'E' character, which indicates that s can be represented in exponential notation. 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數可以表示指數標記法中的數位。The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag. 不過,s 參數必須代表 Int16 資料類型範圍內的數位,而且不能有非零的小數部分。However, the s parameter must represent a number in the range of the Int16 data type and cannot have a non-zero fractional component.
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 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.

非複合 NumberStyles 值Non-composite NumberStyles values 除了數位之外,s 中允許的元素Elements permitted in s in addition to digits
NumberStyles.None 僅限十進位數。Decimal digits only.
NumberStyles.AllowDecimalPoint .The . fractional_digits元素。and fractional_digits elements. 不過, fractional_digits只能包含一或多個0位數,或擲回 OverflowExceptionHowever, fractional_digits must consist of only one or more 0 digits or an OverflowException is thrown.
NumberStyles.AllowExponent s 參數也可以使用指數標記法。The s parameter can also use exponential notation.
NumberStyles.AllowLeadingWhite s開頭的ws元素。The ws element at the beginning of s.
NumberStyles.AllowTrailingWhite s結尾的ws元素。The ws element at the end of s.
NumberStyles.AllowLeadingSign 正負號可以出現在數位前面。A sign can appear before digits.
NumberStyles.AllowTrailingSign 正負號可以在數位之後出現。A sign can appear after digits.
NumberStyles.AllowParentheses 以括弧形式括住數值的sign元素。The sign element in the form of parentheses enclosing the numeric value.
NumberStyles.AllowThousands 元素。The , element.
NumberStyles.AllowCurrencySymbol $ 元素。The $ element.

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是不含前置詞之十六進位值的字串表示。If the NumberStyles.AllowHexSpecifier flag is used, s must be the string representation of a hexadecimal value without a prefix. 例如,"9AF3" 會成功剖析,但 "0x9AF3" 則不會。For example, "9AF3" parses successfully, but "0x9AF3" does not. style 中唯一可以出現的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteThe only other flags that can be present in style are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. NumberStyles 列舉具有複合數位樣式,NumberStyles.HexNumber,其中同時包含空白字元旗標)。(The NumberStyles enumeration has a composite number style, NumberStyles.HexNumber, that includes both white space flags.)

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

另請參閱

Parse(String)

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

public:
 static short Parse(System::String ^ s);
public static short Parse (string s);
static member Parse : string -> int16
Public Shared Function Parse (s As String) As Short

參數

s
String

字串,包含要轉換的數字。A string containing a number to convert.

傳回

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

例外狀況

snulls is null.

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

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

範例

下列範例示範如何使用 Int16.Parse(String) 方法,將字串值轉換為16位帶正負號的整數值。The following example demonstrates how to convert a string value into a 16-bit signed integer value using the Int16.Parse(String) method. 產生的整數值接著會顯示在主控台中。The resulting integer value is then displayed to the console.

String^ value;
Int16 number;
   
value = " 12603 ";
try
{
   number = Int16::Parse(value);
   Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                      value);
}
   
value = " 16,054";
try
{
   number = Int16::Parse(value);
   Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                     value);
}
                           
value = " -17264";
try
{
   number = Int16::Parse(value);
   Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                      value);
}
// The example displays the following output to the console:
//       Converted ' 12603 ' to 12603.
//       Unable to convert ' 16,054' to a 16-bit signed integer.
//       Converted ' -17264' to -17264.      
string value;
short number;

value = " 12603 ";
try
{
   number = Int16.Parse(value);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                     value);
}

value = " 16,054";
try
{
   number = Int16.Parse(value);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                     value);
}
                        
value = " -17264";
try
{
   number = Int16.Parse(value);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", 
                     value);
}
// The example displays the following output to the console:
//       Converted ' 12603 ' to 12603.
//       Unable to convert ' 16,054' to a 16-bit signed integer.
//       Converted ' -17264' to -17264.      
Dim value As String
Dim number As Short

value = " 12603 "
Try
   number = Short.Parse(value)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
                     value)
End Try

value = " 16,054"
Try
   number = Short.Parse(value)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
                     value)
End Try
                        
value = " -17264"
Try
   number = Short.Parse(value)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
                     value)
End Try
' The example displays the following output to the console:
'       Converted ' 12603 ' to 12603.
'       Unable to convert ' 16,054' to a 16-bit signed integer.
'       Converted ' -17264' to -17264.      

備註

s 參數包含下列格式的數位:The s parameter contains a number of the form:

atl-ws-01[sign] 數位 [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.
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 integer value's decimal digits, only leading and trailing spaces together with a leading sign are allowed. 若要明確定義 s中可以存在的樣式元素,請使用 Int16.Parse(String, NumberStyles)Parse 方法。To explicitly define the style elements that can be present in s, use either the Int16.Parse(String, NumberStyles) or the Parse method.

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

另請參閱

Parse(String, IFormatProvider)

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

public:
 static short Parse(System::String ^ s, IFormatProvider ^ provider);
public static short Parse (string s, IFormatProvider provider);
static member Parse : string * IFormatProvider -> int16
Public Shared Function Parse (s As String, provider As IFormatProvider) As Short

參數

s
String

字串,包含要轉換的數字。A string containing a number to convert.

provider
IFormatProvider

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

傳回

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

例外狀況

snulls is null.

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

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

範例

下列範例會使用 Int16.Parse(String, IFormatProvider) 方法來剖析 Int16 值的字串表示。The following example parses string representations of Int16 values with the Int16.Parse(String, IFormatProvider) method.

String^ stringToConvert;
Int16 number;

stringToConvert = " 214 ";
try
{
   number = Int16::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException ^e)
{
   Console::WriteLine("'{0'} is out of range of the Int16 data type.", 
                     stringToConvert);
}

stringToConvert = " + 214";                     
try
{
   number = Int16::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException ^e)
{
   Console::WriteLine("'{0'} is out of range of the Int16 data type.", 
                     stringToConvert);
}

stringToConvert = " +214 "; 
try
{
   number = Int16::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException ^e)
{
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException ^e)
{
   Console::WriteLine("'{0'} is out of range of the Int16 data type.", 
                     stringToConvert);
}
// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214'.
//       Converted ' +214 ' to 214.
string stringToConvert;
short number;

stringToConvert = " 214 ";
try
{
   number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
   Console.WriteLine("'{0'} is out of range of the Int16 data type.", 
                     stringToConvert);
}

stringToConvert = " + 214";                     
try
{
   number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
   Console.WriteLine("'{0'} is out of range of the Int16 data type.", 
                     stringToConvert);
}

stringToConvert = " +214 "; 
try
{
   number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
   Console.WriteLine("'{0'} is out of range of the Int16 data type.", 
                     stringToConvert);
}
// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214'.
//       Converted ' +214 ' to 214.
Dim stringToConvert As String
Dim number As Short

stringToConvert = " 214 "
Try
   number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
                     stringToConvert)
End Try

stringToConvert = " + 214"                                 
Try
   number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
                     stringToConvert)
End Try

stringToConvert = " +214 " 
Try
   number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
                     stringToConvert)
End Try
' The example displays the following output to the console:
'       Converted ' 214 ' to 214.
'       Unable to parse ' + 214'.
'       Converted ' +214 ' to 214.

備註

s 參數包含下列格式的數位:The s parameter contains a number of the form:

atl-ws-01[sign] 數位 [ws][ws][sign]digits[ws]

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

項目Element 說明Description
wsws 選擇性的空白字元。An optional white space.
Signsign 選擇性的正負號。An optional sign.
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. 除了小數以外,s中只允許開頭和尾端空格加上前置正負號。In addition to decimal digits, only leading and trailing spaces together with a leading sign are allowed in s. 若要明確定義樣式專案,以及可在 s中顯示的特定文化特性格式資訊,請使用 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。To explicitly define the style elements together with the culture-specific formatting information that can be present in s, use the Int16.Parse(String, NumberStyles, IFormatProvider) method.

provider 參數是取得 NumberFormatInfo 物件的 IFormatProvider 實。The provider parameter is an IFormatProvider implementation that obtains a NumberFormatInfo object. NumberFormatInfo 提供有關 s格式的特定文化特性資訊。The NumberFormatInfo provides culture-specific information about the format of s. 如果 null``provider,則會使用目前文化特性的 NumberFormatInfoIf provider is null, the NumberFormatInfo for the current culture is used.

另請參閱

適用於