Int16.Parse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为其等效的 16 位带符号整数。
重载
| Parse(String, NumberStyles, IFormatProvider) | 将指定样式和区域性特定格式的数字的字符串表示形式转换为其等效的 16 位带符号整数。 | 
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) | 将指定样式和区域性特定格式的数字的跨度表示形式转换为其等效的 16 位带符号整数。 | 
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) | 将 UTF-8 字符的范围分析为值。 | 
| Parse(String, IFormatProvider) | 将指定区域性特定格式的数字的字符串表示形式转换为其等效的 16 位带符号整数。 | 
| Parse(String) | 将数字的字符串表示形式转换为其等效的 16 位带符号整数。 | 
| Parse(ReadOnlySpan<Char>, IFormatProvider) | 将字符的范围分析为值。 | 
| Parse(ReadOnlySpan<Byte>, IFormatProvider) | 将 UTF-8 字符的范围分析为值。 | 
| Parse(String, NumberStyles) | 将指定样式中的数字的字符串表示形式转换为其等效的 16 位带符号整数。 | 
Parse(String, NumberStyles, IFormatProvider)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将指定样式和区域性特定格式的数字的字符串表示形式转换为其等效的 16 位带符号整数。
public:
 static short Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);public:
 static short Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<short>::Parse;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 -> int16Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Short参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,指示 s中可以存在的样式元素。 要指定的典型值为 Integer。
- provider
- IFormatProvider
提供有关 s的区域性特定格式设置信息的 IFormatProvider。
返回
与 s中指定的数字等效的 16 位有符号整数。
实现
例外
              s
              null。
              s 的格式不符合 style。
示例
以下示例使用各种 style 和 provider 参数分析 Int16 值的字符串表示形式。
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.
// Parse string using "." as the thousands separator
// and " " as the decimal separator.
let value = "19 694,00"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let provider = CultureInfo "fr-FR"
let number = Int16.Parse(value, style, provider)
printfn $"'{value}' converted to {number}." 
// Displays:
//    '19 694,00' converted to 19694.
try
    let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
    printfn $"'{value}' converted to {number}." 
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '19 694,00'.
// Parse string using "$" as the currency symbol for en_GB and
// en-US cultures.
let value = "$6,032.00"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
try
    let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
    printfn $"'{value}' converted to {number}." 
with :? FormatException ->
    printfn $"Unable to parse '{value}'."
// Displays:
//    Unable to parse '$6,032.00'.
let provider = CultureInfo "en-US"
let number = Int16.Parse(value, style, provider)
printfn $"'{value}' converted to {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 参数中允许的样式元素(如空格或正号),以便分析操作成功。 它必须是 NumberStyles 枚举中的位标志的组合。 根据 style的值,s 参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
或者,如果 style 包括 AllowHexSpecifier:
[ws]hexdigits[ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 如果 style包含 NumberStyles.AllowLeadingWhite 标志,或者s末尾style包含 NumberStyles.AllowTrailingWhite 标志,则空格可能会显示在s的开头。 | 
| $ | 区域性特定的货币符号。 字符串中的位置由当前区域性的 NumberFormatInfo.CurrencyPositivePattern 和 NumberFormatInfo.CurrencyNegativePattern 属性定义。 如果 style包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在s中。 | 
| 签名 | 可选符号。 如果 style包含 NumberStyles.AllowLeadingSign 标志,则符号可以在s开头显示,如果style包含 NumberStyles.AllowTrailingSign 标志,则它将显示在s末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在s中使用括号来指示负值。 | 
| 位 | 从 0 到 9 的数字序列。 | 
| 、 | 区域性特定的千位分隔符符号。 如果 style包含 NumberStyles.AllowThousands 标志,则当前区域性的千位分隔符可以出现在s中。 | 
| 。 | 区域性特定的小数点符号。 如果 style包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在s中。 | 
| fractional_digits | 0 位序列。 如果 style包含 NumberStyles.AllowDecimalPoint 标志,则可以在s中显示小数位数。 如果除 0 以外的任何数字出现在 fractional_digits中,该方法将引发 OverflowException。 | 
| e | “e”或“E”字符,指示 s可以用指数表示法表示。 如果style包含 NumberStyles.AllowExponent 标志,s参数可以表示指数表示法中的数字。 但是,s参数必须表示 Int16 数据类型范围内的数字,并且不能具有非零小数部分。 | 
| hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 | 
注意
无论 style 参数的值如何,分析操作都会忽略 s 中任何终止的 NUL (U+0000) 字符。
仅 s中可能存在的元素。
| 非复合 NumberStyles 值 | 除数字外允许的元素 | 
|---|---|
| NumberStyles.None | 仅十进制数字。 | 
| NumberStyles.AllowDecimalPoint | 。 和 fractional_digits 元素。 但是,fractional_digits 必须仅包含一个或多个数字或引发 OverflowException。 | 
| NumberStyles.AllowExponent | s参数也可以使用指数表示法。 | 
| NumberStyles.AllowLeadingWhite | s开头的 ws 元素。 | 
| NumberStyles.AllowTrailingWhite | s末尾的 ws 元素。 | 
| NumberStyles.AllowLeadingSign | 符号可以出现在 位之前。 | 
| NumberStyles.AllowTrailingSign | 数字后,可以显示一个符号。 | 
| NumberStyles.AllowParentheses | 符号 元素,以括住数值的括号形式。 | 
| NumberStyles.AllowThousands | , 元素。 | 
| NumberStyles.AllowCurrencySymbol | $ 元素。 | 
如果使用 NumberStyles.AllowHexSpecifier 标志,s 必须是没有前缀的十六进制值的字符串表示形式。 例如,“9AF3”分析成功,但“0x9AF3”则不会。 
              style 中唯一可以存在的其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (NumberStyles 枚举具有包含两个空格标志的复合数字样式 NumberStyles.HexNumber。
              provider 参数是一个 IFormatProvider 实现,其 GetFormat 方法获取 NumberFormatInfo 对象。 
              NumberFormatInfo 对象提供有关 s格式的区域性特定信息。 如果 providernull,则使用当前区域性的 NumberFormatInfo 对象。
另请参阅
- NumberStyles
- ToString()
- 分析 .NET 中的数值字符串
适用于
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将指定样式和区域性特定格式的数字的跨度表示形式转换为其等效的 16 位带符号整数。
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 -> int16Public 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>
一个范围,包含表示要转换的数字的字符。
- style
- NumberStyles
枚举值的按位组合,指示 s中可以存在的样式元素。 要指定的典型值为 Integer。
- provider
- IFormatProvider
提供有关 s的区域性特定格式设置信息的 IFormatProvider。
返回
与 s中指定的数字等效的 16 位有符号整数。
实现
适用于
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Source:
- Int16.cs
- Source:
- Int16.cs
将 UTF-8 字符的范围分析为值。
public static short Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> int16Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Short参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的范围。
- style
- NumberStyles
              utf8Text中可以存在的数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text的区域性特定格式设置信息。
返回
分析 utf8Text的结果。
实现
适用于
Parse(String, IFormatProvider)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将指定区域性特定格式的数字的字符串表示形式转换为其等效的 16 位带符号整数。
public:
 static short Parse(System::String ^ s, IFormatProvider ^ provider);public:
 static short Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<short>::Parse;public static short Parse (string s, IFormatProvider provider);public static short Parse (string s, IFormatProvider? provider);static member Parse : string * IFormatProvider -> int16Public Shared Function Parse (s As String, provider As IFormatProvider) As Short参数
- s
- String
包含要转换的数字的字符串。
- provider
- IFormatProvider
提供有关 s的区域性特定格式设置信息的 IFormatProvider。
返回
与 s中指定的数字等效的 16 位有符号整数。
实现
例外
              s
              null。
              s 格式不正确。
              s 表示小于 Int16.MinValue 或大于 Int16.MaxValue的数字。
示例
以下示例使用 Int16.Parse(String, IFormatProvider) 方法分析 Int16 值的字符串表示形式。
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.
let stringToConvert = " 214 "
try
    let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {number}." 
with 
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is out of range of the Int16 data type."
let stringToConvert = " + 214"
try
    let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {number}." 
with 
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException -> 
    printfn $"'{stringToConvert}' is out of range of the Int16 data type."
let stringToConvert = " +214 "
try
    let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {number}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is out of range of the Int16 data type."
// 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 参数包含多种形式:
[ws][sign]digits[ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 | 
| 标志 | 可选符号。 | 
| 数字 | 一系列数字,范围从 0 到 9。 | 
              s 参数使用 NumberStyles.Integer 样式进行解释。 除了十进制数字,s中只允许前导和尾随空格和前导符号。 若要显式定义样式元素以及 s中存在的特定于区域性的格式信息,请使用 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。
              provider 参数是获取 NumberFormatInfo 对象的 IFormatProvider 实现。 
              NumberFormatInfo 提供有关 s格式的区域性特定信息。 如果 providernull,则使用当前区域性的 NumberFormatInfo。
另请参阅
- ToString()
- TryParse
- 分析 .NET 中的数值字符串
适用于
Parse(String)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将数字的字符串表示形式转换为其等效的 16 位带符号整数。
public:
 static short Parse(System::String ^ s);public static short Parse (string s);static member Parse : string -> int16Public Shared Function Parse (s As String) As Short参数
- s
- String
包含要转换的数字的字符串。
返回
与 s中包含的数字等效的 16 位有符号整数。
例外
              s
              null。
              s 格式不正确。
              s 表示小于 Int16.MinValue 或大于 Int16.MaxValue的数字。
示例
以下示例演示如何使用 Int16.Parse(String) 方法将字符串值转换为 16 位有符号整数值。 然后,生成的整数值会显示到控制台。
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.
let value = " 12603 "
try
    let number = Int16.Parse value
    printfn $"Converted '{value}' to {number}." 
with :? FormatException -> 
    printfn $"Unable to convert '{value}' to a 16-bit signed integer."
let value = " 16,054"
try
    let number = Int16.Parse value
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn "Unable to convert '{value}' to a 16-bit signed integer."
                    
let value = " -17264"
try
    let number = Int16.Parse value
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn "Unable to convert '{value}' to a 16-bit signed integer."
    
// 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 参数包含多种形式:
[ws][sign]digits[ws]
方括号 ([ 和 ]) 中的元素是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 | 
| 签名 | 可选符号。 | 
| 位 | 一系列数字,范围从 0 到 9。 | 
              s 参数使用 NumberStyles.Integer 样式进行解释。 除了整数值的十进制数字外,还允许使用前导符号和尾随空格。 若要显式定义 s中可以存在的样式元素,请使用 Int16.Parse(String, NumberStyles) 或 Parse 方法。
              s 参数是使用为当前系统区域性初始化的 NumberFormatInfo 对象中的格式信息进行分析的。 有关详细信息,请参阅 CurrentInfo。 若要使用其他区域性的格式设置信息分析字符串,请使用 Int16.Parse(String, IFormatProvider) 或 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。
另请参阅
- ToString()
- 分析 .NET 中的数值字符串
适用于
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将字符的范围分析为值。
public:
 static short Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<short>::Parse;public static short Parse (ReadOnlySpan<char> s, IFormatProvider? provider);static member Parse : ReadOnlySpan<char> * IFormatProvider -> int16Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Short参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s的区域性特定格式设置信息。
返回
分析 s的结果。
实现
适用于
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Source:
- Int16.cs
- Source:
- Int16.cs
将 UTF-8 字符的范围分析为值。
public:
 static short Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<short>::Parse;public static short Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);static member Parse : ReadOnlySpan<byte> * IFormatProvider -> int16Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Short参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符的范围。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text的区域性特定格式设置信息。
返回
分析 utf8Text的结果。
实现
适用于
Parse(String, NumberStyles)
- Source:
- Int16.cs
- Source:
- Int16.cs
- Source:
- Int16.cs
将指定样式中的数字的字符串表示形式转换为其等效的 16 位带符号整数。
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 -> int16Public Shared Function Parse (s As String, style As NumberStyles) As Short参数
- s
- String
包含要转换的数字的字符串。
- style
- NumberStyles
枚举值的按位组合,指示 s中可以存在的样式元素。 要指定的典型值为 Integer。
返回
与 s中指定的数字等效的 16 位有符号整数。
例外
              s
              null。
              s 的格式不符合 style。
示例
以下示例使用 Int16.Parse(String, NumberStyles) 方法通过 en-US 区域性分析 Int16 值的字符串表示形式。
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.
open System
open System.Globalization
let parseToInt16 (value: string) (style: NumberStyles) =
    try
        let number = Int16.Parse(value, style)
        printfn $"Converted '{value}' to {number}."
    with
    | :? FormatException ->
        printfn $"Unable to parse '{value}' with style {style}."
    | :? OverflowException ->
        printfn $"'{value}' is out of range of the Int16 type."
[<EntryPoint>]
let main _ =
    // Parse a number with a thousands separator (throws an exception).
    let value = "14,644"
    let style = NumberStyles.None
    parseToInt16 value style
    let style = NumberStyles.AllowThousands
    parseToInt16 value style
    // Parse a number with a thousands separator and decimal point.
    let value = "14,644.00"
    let style = NumberStyles.AllowThousands ||| NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    parseToInt16 value style
    // Parse a number with a fractional component (throws an exception).
    let value = "14,644.001"
    parseToInt16 value style
    // Parse a number in exponential notation.
    let value = "145E02"
    let style = style ||| NumberStyles.AllowExponent
    parseToInt16 value style
    // Parse a number in exponential notation with a positive sign.
    let value = "145E+02"
    parseToInt16 value style
    // Parse a number in exponential notation with a negative sign
    // (throws an exception).
    let value = "145E-02"
    parseToInt16 value style
    0
// 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 参数中允许的样式元素(如空格或符号符号),以便分析操作成功。 它必须是 NumberStyles 枚举中的位标志的组合。 根据 style的值,s 参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
或者,如果 style 包括 AllowHexSpecifier:
[ws]hexdigits[ws]
方括号 ([ 和 ]) 中的项是可选的。 下表描述了每个元素。
| 元素 | 描述 | 
|---|---|
| ws | 可选空格。 如果 style包含 NumberStyles.AllowLeadingWhite 标志,或者s末尾style包含 NumberStyles.AllowTrailingWhite 标志,则空格可能会显示在s的开头。 | 
| $ | 区域性特定的货币符号。 字符串中的位置由当前区域性的 NumberFormatInfo.CurrencyPositivePattern 和 NumberFormatInfo.CurrencyNegativePattern 属性定义。 如果 style包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在s中。 | 
| 签名 | 可选符号。 如果 style包含 NumberStyles.AllowLeadingSign 标志,则符号可以在s开头显示,如果style包含 NumberStyles.AllowTrailingSign 标志,则它将显示在s末尾。 如果style包含 NumberStyles.AllowParentheses 标志,则可以在s中使用括号来指示负值。 | 
| 位 | 从 0 到 9 的数字序列。 | 
| 、 | 区域性特定的千位分隔符符号。 如果 style包含 NumberStyles.AllowThousands 标志,则当前区域性的千位分隔符可以出现在s中。 | 
| 。 | 区域性特定的小数点符号。 如果 style包含 NumberStyles.AllowDecimalPoint 标志,则当前区域性的小数点符号可以出现在s中。 | 
| fractional_digits | 0 位序列。 如果 style包含 NumberStyles.AllowDecimalPoint 标志,则可以在s中显示小数位数。 如果除 0 以外的任何数字出现在 fractional_digits中,该方法将引发 OverflowException。 | 
| e | “e”或“E”字符,指示 s可以用指数表示法表示。 如果style包含 NumberStyles.AllowExponent 标志,s参数可以表示指数表示法中的数字。 但是,s参数必须表示 Int16 数据类型范围内的数字,并且不能具有非零小数部分。 | 
| hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 | 
注意
无论 style 参数的值如何,分析操作都会忽略 s 中任何终止的 NUL (U+0000) 字符。
仅 s中可能存在的元素。
| 非复合 NumberStyles 值 | 除数字外允许的元素 | 
|---|---|
| NumberStyles.None | 仅十进制数字。 | 
| NumberStyles.AllowDecimalPoint | 。 和 fractional_digits 元素。 但是,fractional_digits 必须仅包含一个或多个数字或引发 OverflowException。 | 
| NumberStyles.AllowExponent | s参数也可以使用指数表示法。 | 
| NumberStyles.AllowLeadingWhite | s开头的 ws 元素。 | 
| NumberStyles.AllowTrailingWhite | s末尾的 ws 元素。 | 
| NumberStyles.AllowLeadingSign | 符号可以出现在 位之前。 | 
| NumberStyles.AllowTrailingSign | 数字后,可以显示一个符号。 | 
| NumberStyles.AllowParentheses | 符号 元素,以括住数值的括号形式。 | 
| NumberStyles.AllowThousands | , 元素。 | 
| NumberStyles.AllowCurrencySymbol | $ 元素。 | 
如果使用 NumberStyles.AllowHexSpecifier 标志,s 必须是没有前缀的十六进制值的字符串表示形式。 例如,“9AF3”分析成功,但“0x9AF3”不会。 
              style 中唯一可以存在的其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 (NumberStyles 枚举具有包含两个空格标志的复合数字样式 NumberStyles.HexNumber。
              s 参数是使用为当前系统区域性初始化的 NumberFormatInfo 对象中的格式信息进行分析的。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要使用特定区域性的格式信息分析 s,请调用 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。
另请参阅
- NumberStyles
- ToString()
- TryParse
- 分析 .NET 中的数值字符串