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

返回

Int16

s 中指定的数字等效的 16 位带符号整数。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 的格式不符合 styles 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 参数来分析值的字符串表示形式 Int16The 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. 它必须是枚举中的位标志的组合 NumberStylesIt must be a combination of bit flags from the NumberStyles enumeration. 根据的值 styles 参数可能包括以下元素:Depending on the value of style, the s parameter may include the following elements:

ws[$]表明[数字,] 位数 [.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]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space. 如果包含标志,则空白可以出现在开头; s style NumberStyles.AllowLeadingWhite s 如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则为末尾处。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.CurrencyPositivePattern NumberFormatInfo.CurrencyNegativePattern 当前区域性的和属性定义。Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern and NumberFormatInfo.CurrencyNegativePattern property of the current culture. s如果包含标志,则当前区域性的货币符号可以出现在中 style NumberStyles.AllowCurrencySymbolThe current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. 如果包含标志,则符号可以出现在的开头 s style NumberStyles.AllowLeadingSign ,如果包含标志,则它可以出现在的末尾 s style NumberStyles.AllowTrailingSignThe 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. 如果包含标志,则可以在中使用括号 s 来表示负值 style NumberStyles.AllowParenthesesParentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
位数digits 介于0到9之间的一系列数字。A sequence of digits from 0 through 9.
,, 区域性特定的千位分隔符。A culture-specific thousands separator symbol. s如果包含标志,则当前区域性的千位分隔符符号可以出现在中 style NumberStyles.AllowThousandsThe current culture's thousands separator symbol 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.
fractional_digitsfractional_digits 0数字的序列。A sequence of the 0 digit. s如果 style 包含标志,则小数位数可以出现在中 NumberStyles.AllowDecimalPointFractional 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. s如果包含标志,则参数可以表示指数表示法中的数字 style NumberStyles.AllowExponentThe 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.

备注

s无论参数的值如何,分析操作都将忽略中任何终止的 NUL (U + 0000) 字符 styleAny 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 影响可能出现在中的元素 sThe 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位数字 OverflowException ,否则将引发。However, 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 开头的 ws 元素 sThe ws element at the beginning of s.
NumberStyles.AllowTrailingWhite 末尾处的 ws 元素 sThe 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 用括号括起数值的 符号 元素。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.. 中可以存在的唯一其他标志 styleNumberStyles.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参数是 IFormatProviderGetFormat 方法获取对象的实现 NumberFormatInfoThe provider parameter is an IFormatProvider implementation whose GetFormat method obtains a NumberFormatInfo object. NumberFormatInfo对象提供有关的格式的区域性特定信息 sThe NumberFormatInfo object provides culture-specific information about the format of s. 如果 providernull ,则 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 = default);
public static short Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
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 = Nothing) 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.

返回

Int16

s 中指定的数字等效的 16 位带符号整数。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.

返回

Int16

s 中指定的数字等效的 16 位带符号整数。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 的格式不符合 styles 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 区域性的值的字符串表示形式 Int16The 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. 它必须是枚举中的位标志的组合 NumberStylesIt must be a combination of bit flags from the NumberStyles enumeration. 根据的值 styles 参数可能包括以下元素:Depending on the value of style, the s parameter may include the following elements:

ws[$]表明[数字,] 位数 [.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. 如果包含标志,则空白可以出现在开头; s style NumberStyles.AllowLeadingWhite s 如果 style 包含 NumberStyles.AllowTrailingWhite 标志,则为末尾处。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.CurrencyPositivePattern NumberFormatInfo.CurrencyNegativePattern 当前区域性的和属性定义。Its position in the string is defined by the NumberFormatInfo.CurrencyPositivePattern and NumberFormatInfo.CurrencyNegativePattern property of the current culture. s如果包含标志,则当前区域性的货币符号可以出现在中 style NumberStyles.AllowCurrencySymbolThe current culture's currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag.
signsign 一个可选的符号。An optional sign. 如果包含标志,则符号可以出现在的开头 s style NumberStyles.AllowLeadingSign ,如果包含标志,则它可以出现在的末尾 s style NumberStyles.AllowTrailingSignThe 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. 如果包含标志,则可以在中使用括号 s 来表示负值 style NumberStyles.AllowParenthesesParentheses can be used in s to indicate a negative value if style includes the NumberStyles.AllowParentheses flag.
位数digits 介于0到9之间的一系列数字。A sequence of digits from 0 through 9.
,, 区域性特定的千位分隔符。A culture-specific thousands separator symbol. s如果包含标志,则当前区域性的千位分隔符符号可以出现在中 style NumberStyles.AllowThousandsThe current culture's thousands separator symbol 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.
fractional_digitsfractional_digits 0数字的序列。A sequence of the 0 digit. s如果 style 包含标志,则小数位数可以出现在中 NumberStyles.AllowDecimalPointFractional 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. s如果包含标志,则参数可以表示指数表示法中的数字 style NumberStyles.AllowExponentThe 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.

备注

s无论参数的值如何,分析操作都将忽略中任何终止的 NUL (U + 0000) 字符 styleAny 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 影响可能出现在中的元素 sThe 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位数字 OverflowException ,否则将引发。However, 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 开头的 ws 元素 sThe ws element at the beginning of s.
NumberStyles.AllowTrailingWhite 末尾处的 ws 元素 sThe 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 用括号括起数值的 符号 元素。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. 中可以存在的唯一其他标志 styleNumberStyles.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.

返回

Int16

s 中包含的数字等效的 16 位有符号整数。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.

示例

下面的示例演示如何使用方法将字符串值转换为16位带符号整数值 Int16.Parse(String)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:

ws[sign] 位数 [ws][ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。Optional white space.
signsign 一个可选的符号。An optional sign.
位数digits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9.

s使用样式对参数进行解释 NumberStyles.IntegerThe 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);
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.

返回

Int16

s 中指定的数字等效的 16 位带符号整数。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 Int16.Parse(String, IFormatProvider)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:

ws[sign] 位数 [ws][ws][sign]digits[ws]

方括号 ([ and ]) 中的元素是可选的。Elements in square brackets ([ and ]) are optional. 下表对每个元素进行了描述。The following table describes each element.

元素Element 说明Description
wsws 可选空白。An optional white space.
签名sign 一个可选的符号。An optional sign.
位数digits 介于0到9之间的一系列数字。A sequence of digits ranging from 0 to 9.

s使用样式对参数进行解释 NumberStyles.IntegerThe s parameter is interpreted using the NumberStyles.Integer style. 除了十进制数字以外,在中,只允许使用前导和尾随空格以及前导符号 sIn 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参数是 IFormatProvider 获取对象的实现 NumberFormatInfoThe provider parameter is an IFormatProvider implementation that obtains a NumberFormatInfo object. NumberFormatInfo提供有关的格式的区域性特定信息 sThe NumberFormatInfo provides culture-specific information about the format of s. 如果 providernull ,则 NumberFormatInfo 使用当前区域性的。If provider is null, the NumberFormatInfo for the current culture is used.

另请参阅

适用于