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 -> 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 位带符号整数。
实现
例外
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]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果包含 标志,则的s 开头会出现空格;如果style style 包含 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 标志,则可以在 的s 末尾显示空格。 |
$ | 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyPositivePattern 当前区域性的 和 NumberFormatInfo.CurrencyNegativePattern 属性定义。 如果style 包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 中s 。 |
sign | 可选符号。 如果包含 标志,则符号可以出现在 的s 开头,如果style 包含 NumberStyles.AllowTrailingSign 标志,则它可显示在 的末尾s 。NumberStyles.AllowLeadingSignstyle 如果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 包含 标志,则 s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 但是, s 参数必须表示数据类型范围内的 Int16 数字,并且不能具有非零小数部分。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style
。
仅包含 数字 的字符串 (对应于 NumberStyles.None 样式) 始终成功分析。 大多数剩余 NumberStyles 成员控制可能在此输入字符串中存在但不需要存在的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在 s
的元素。
非复合 NumberStyles 值 | 除了数字外,还允许 在 中 |
---|---|
NumberStyles.None | 仅十进制数字。 |
NumberStyles.AllowDecimalPoint | 和fractional_digits 元素。 但是, fractional_digits 只能包含一个或多个 0 位数字,否则 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
区域性特定信息。 如果 provider
为 null
,则 NumberFormatInfo 使用当前区域性的 对象。
另请参阅
适用于
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 -> 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
将 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
将指定的区域性特定格式的数字的字符串表示形式转换为它的等效 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 位带符号整数。
实现
例外
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]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
签名 | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
参数 s
是使用 样式解释的 NumberStyles.Integer 。 除了十进制数字外,在 中 s
只允许前导空格和尾随空格以及前导符号。 若要显式定义样式元素以及中可能存在 s
的区域性特定格式设置信息, Int16.Parse(String, NumberStyles, IFormatProvider) 请使用 方法。
参数 provider
是获取 IFormatProviderNumberFormatInfo 对象的实现。 NumberFormatInfo提供有关 格式的s
区域性特定信息。 如果 provider
为 null
, NumberFormatInfo 则使用当前区域性的 。
另请参阅
适用于
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 -> int16
Public 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]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 |
位数 | 从 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
将字符范围分析为值。
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
将 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
将指定样式的数字的字符串表示形式转换为它的等效 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 位带符号整数。
例外
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 | 可选空格。 如果包含 标志,则的s 开头会出现空格;如果style style 包含 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 标志,则可以在 的s 末尾显示空格。 |
$ | 区域性特定的货币符号。 它在字符串中的位置由 NumberFormatInfo.CurrencyPositivePattern 当前区域性的 和 NumberFormatInfo.CurrencyNegativePattern 属性定义。 如果style 包含 NumberStyles.AllowCurrencySymbol 标志,则当前区域性的货币符号可以出现在 中s 。 |
sign | 可选符号。 如果包含 标志,则符号可以出现在 的s 开头,如果style 包含 NumberStyles.AllowTrailingSign 标志,则它可显示在 的末尾s 。NumberStyles.AllowLeadingSignstyle 如果style 包含 NumberStyles.AllowParentheses 标志,则可以在 中使用s 括号来指示负值。 |
位数 | 从 0 到 9 的数字序列。 |
, | 区域性特定的千位分隔符。 如果包含 标志,则当前区域性的千位分隔符可以出现在 中s NumberStyles.AllowThousands。style |
. | 区域性特定的小数点符号。 如果style 包含 标志,则当前区域性的小数点符号会显示在 NumberStyles.AllowDecimalPoint 中s 。 |
fractional_digits | 0 位的序列。 如果style 包含 标志,NumberStyles.AllowDecimalPoint则可以显示s 小数位数。 如果 0 以外的任何数字出现在 fractional_digits中,则 方法将 OverflowException引发 。 |
e | “e”或“E”字符,表示 s 可以用指数表示法表示。 如果style 包含 标志,参数s 可以表示指数表示法的数字NumberStyles.AllowExponent。 但是, s 参数必须表示数据类型范围内的 Int16 数字,并且不能具有非零小数分量。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑参数的值 style
。
仅包含 数字 的字符串 (对应于 NumberStyles.None 样式) 始终成功分析。 大多数剩余 NumberStyles 成员控制元素,这些元素可能位于此输入字符串中,但不需要存在。 下表指示各个 NumberStyles 成员如何影响 中可能存在的 s
元素。
非复合 NumberStyles 值 | 除数字外允许的元素 |
---|---|
NumberStyles.None | 仅十进制数字。 |
NumberStyles.AllowDecimalPoint | 和fractional_digits 元素。 但是, fractional_digits 必须只包含一个或多个 0 位或 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) 方法。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈