通过


Int16.Parse 方法

定义

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

重载

名称 说明
Parse(String, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(String, IFormatProvider)

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

Parse(String)

将数字的字符串表示形式转换为其等效的 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
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 -> int16
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Short

参数

s
String

包含要转换的数字的字符串。

style
NumberStyles

枚举值的按位组合,指示可在其中 s存在的样式元素。 要指定 Integer的典型值为 。

provider
IFormatProvider

提供有关 IFormatProvider 区域性的格式化信息 s

返回

等效于指定 s数字的 16 位有符号整数。

实现

例外

snull

style 不是值 NumberStyles

-或-

style不是值的组合AllowHexSpecifierHexNumber

s 格式不符合 style

s 表示小于 Int16.MinValue 或大于 Int16.MaxValue 的数字。

-或-

s 包括非零小数位数。

示例

以下示例使用各种 style 参数 provider 来分析值的字符串表示形式 Int16

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包含标志,则空白显示在标志的s开头,或者在包含styleNumberStyles.AllowTrailingWhite标志的s末尾NumberStyles.AllowLeadingWhite显示。
$ 区域性特定的货币符号。 字符串中的位置由NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo.CurrencyNegativePattern当前区域性的属性定义。 如果s包含标志,则当前区域性的货币符号可以出现在style其中NumberStyles.AllowCurrencySymbol
签名 可选符号。 如果style包含标志,则符号可以出现在标志的s开头,如果包含NumberStyles.AllowTrailingSign标志,则它将显示在标志的sstyleNumberStyles.AllowLeadingSign末尾。 括号可用于 s 指示负值(如果 style 包含 NumberStyles.AllowParentheses 标志)。
数字 从 0 到 9 的数字序列。
区域性特定的千位分隔符符号。 如果包含标志,则当前区域性的千位分隔符可以出现在其中 sstyleNumberStyles.AllowThousands
. 区域性特定的小数点符号。 如果包含标志,则当前区域性的小数点符号可以出现在sstyle其中NumberStyles.AllowDecimalPoint
fractional_digits 0 位序列。 如果style包含NumberStyles.AllowDecimalPoint标志,则可以显示s小数位数。 如果除 0 以外的任何数字出现在 fractional_digits中,该方法将引发一个 OverflowException
e “e”或“E”字符,指示 s 可以用指数表示法表示。 如果s包含标志,则参数style可以表示指数表示法中的NumberStyles.AllowExponent数字。 但是,参数 s 必须表示数据类型范围内的 Int16 数字,并且不能具有非零小数分量。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

无论参数的值s如何,分析作都会忽略任何终止 NUL (U+0000) 字符style

仅包含 数字 的字符串(对应于 NumberStyles.None 样式)始终成功分析。 大多数剩余 NumberStyles 成员控制可能但不需要在此输入字符串中存在的元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s的元素。

非复合 NumberStyles 值 除数字外允许的元素
NumberStyles.None 仅十进制数字。
NumberStyles.AllowDecimalPoint fractional_digits元素。 但是, fractional_digits 必须仅包含一个或多个数字或引发数字 OverflowException
NumberStyles.AllowExponent s 参数还可以使用指数表示法。
NumberStyles.AllowLeadingWhite 开头的 sws 元素。
NumberStyles.AllowTrailingWhite 末尾的 s 元素
NumberStyles.AllowLeadingSign 符号可以在 数字之前显示。
NumberStyles.AllowTrailingSign 符号可以在 数字之后显示。
NumberStyles.AllowParentheses 括住数值的括号形式的 符号 元素。
NumberStyles.AllowThousands 元素
NumberStyles.AllowCurrencySymbol 元素 $

如果使用标志 NumberStyles.AllowHexSpecifiers 则必须是没有前缀的十六进制值的字符串表示形式。 例如,“9AF3”分析成功,但“0x9AF3”则不会。 唯一可以存在于 style 其中的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteNumberStyles(枚举具有复合数字样式,NumberStyles.HexNumber包括两个空格标志。

参数 provider 是一个 IFormatProvider 实现,其 GetFormat 方法获取对象 NumberFormatInfo 。 该 NumberFormatInfo 对象提供有关其格式的 s区域性特定信息。 provider如果是nullNumberFormatInfo则使用当前区域性的对象。

另请参阅

适用于

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Source:
Int16.cs
Source:
Int16.cs
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 -> 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>

一个范围,包含表示要转换的数字的字符。

style
NumberStyles

枚举值的按位组合,指示可在其中 s存在的样式元素。 要指定 Integer的典型值为 。

provider
IFormatProvider

提供有关 IFormatProvider 区域性的格式化信息 s

返回

等效于指定 s数字的 16 位有符号整数。

实现

适用于

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Source:
Int16.cs
Source:
Int16.cs
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 -> int16
Public 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
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 -> int16
Public Shared Function Parse (s As String, provider As IFormatProvider) As Short

参数

s
String

包含要转换的数字的字符串。

provider
IFormatProvider

提供有关 IFormatProvider 区域性的格式化信息 s

返回

等效于指定 s数字的 16 位有符号整数。

实现

例外

snull

s 格式不正确。

s 表示小于 Int16.MinValue 或大于 Int16.MaxValue 的数字。

示例

以下示例使用Int16.Parse(String, IFormatProvider)该方法分析值的字符串表示形式Int16

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是获取IFormatProviderNumberFormatInfo对象的实现。 提供有关 NumberFormatInfo 其格式的 s区域性特定信息。 null如果是provider,则NumberFormatInfo使用当前区域性。

另请参阅

适用于

Parse(String)

Source:
Int16.cs
Source:
Int16.cs
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 -> int16
Public Shared Function Parse (s As String) As Short

参数

s
String

包含要转换的数字的字符串。

返回

一个 16 位有符号整数,等效于中包含的 s数字。

例外

snull

s 格式不正确。

s 表示小于 Int16.MinValue 或大于 Int16.MaxValue 的数字。

示例

以下示例演示如何使用 Int16.Parse(String) 该方法将字符串值转换为 16 位有符号整数值。 然后,生成的整数值会显示到控制台。

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) 方法。

另请参阅

适用于

Parse(ReadOnlySpan<Char>, IFormatProvider)

Source:
Int16.cs
Source:
Int16.cs
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 -> int16
Public 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
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 -> int16
Public 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
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 -> int16
Public Shared Function Parse (s As String, style As NumberStyles) As Short

参数

s
String

包含要转换的数字的字符串。

style
NumberStyles

枚举值的按位组合,指示可在其中 s存在的样式元素。 要指定 Integer的典型值为 。

返回

等效于指定 s数字的 16 位有符号整数。

例外

snull

style 不是值 NumberStyles

-或-

style不是值的组合AllowHexSpecifierHexNumber

s 格式不符合 style

s 表示小于 Int16.MinValue 或大于 Int16.MaxValue 的数字。

-或-

s 包括非零小数位数。

示例

以下示例使用该方法 Int16.Parse(String, NumberStyles) 使用 en-US 区域性分析值的字符串表示形式 Int16

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包含标志,则空白显示在标志的s开头,或者在包含styleNumberStyles.AllowTrailingWhite标志的s末尾NumberStyles.AllowLeadingWhite显示。
$ 区域性特定的货币符号。 字符串中的位置由NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo.CurrencyNegativePattern当前区域性的属性定义。 如果s包含标志,则当前区域性的货币符号可以出现在style其中NumberStyles.AllowCurrencySymbol
签名 可选符号。 如果style包含标志,则符号可以出现在标志的s开头,如果包含NumberStyles.AllowTrailingSign标志,则它将显示在标志的sstyleNumberStyles.AllowLeadingSign末尾。 括号可用于 s 指示负值(如果 style 包含 NumberStyles.AllowParentheses 标志)。
数字 从 0 到 9 的数字序列。
区域性特定的千位分隔符符号。 如果包含标志,则当前区域性的千位分隔符可以出现在其中 sstyleNumberStyles.AllowThousands
. 区域性特定的小数点符号。 如果包含标志,则当前区域性的小数点符号可以出现在sstyle其中NumberStyles.AllowDecimalPoint
fractional_digits 0 位序列。 如果style包含NumberStyles.AllowDecimalPoint标志,则可以显示s小数位数。 如果除 0 以外的任何数字出现在 fractional_digits中,该方法将引发一个 OverflowException
e “e”或“E”字符,指示 s 可以用指数表示法表示。 如果s包含标志,则参数style可以表示指数表示法中的NumberStyles.AllowExponent数字。 但是,参数 s 必须表示数据类型范围内的 Int16 数字,并且不能具有非零小数分量。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。

注意

无论参数的值s如何,分析作都会忽略任何终止 NUL (U+0000) 字符style

仅包含 数字 的字符串(对应于 NumberStyles.None 样式)始终成功分析。 大多数剩余 NumberStyles 成员控制可能但不需要在此输入字符串中存在的元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s的元素。

非复合 NumberStyles 值 除数字外允许的元素
NumberStyles.None 仅十进制数字。
NumberStyles.AllowDecimalPoint fractional_digits元素。 但是, fractional_digits 必须仅包含一个或多个数字或引发数字 OverflowException
NumberStyles.AllowExponent s 参数还可以使用指数表示法。
NumberStyles.AllowLeadingWhite 开头的 sws 元素。
NumberStyles.AllowTrailingWhite 末尾的 s 元素
NumberStyles.AllowLeadingSign 符号可以在 数字之前显示。
NumberStyles.AllowTrailingSign 符号可以在 数字之后显示。
NumberStyles.AllowParentheses 括住数值的括号形式的 符号 元素。
NumberStyles.AllowThousands 元素
NumberStyles.AllowCurrencySymbol 元素 $

如果使用标志 NumberStyles.AllowHexSpecifiers 则必须是没有前缀的十六进制值的字符串表示形式。 例如,“9AF3”分析成功,但“0x9AF3”不会。 唯一可以存在于 style 其中的其他标志是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteNumberStyles(枚举具有复合数字样式,NumberStyles.HexNumber包括两个空格标志。

s 参数使用为当前系统区域性初始化的对象中的 NumberFormatInfo 格式设置信息进行分析。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。 若要使用特定区域性的格式设置信息进行分析 s ,请调用 Int16.Parse(String, NumberStyles, IFormatProvider) 该方法。

另请参阅

适用于