Int16.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。
重载
TryParse(String, Int16) |
将数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Char>, Int16) |
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。 |
TryParse(String, NumberStyles, IFormatProvider, Int16) |
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16) |
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。 |
TryParse(String, Int16)
将数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (string s, out short result);
public static bool TryParse (string? s, out short result);
static member TryParse : string * int16 -> bool
Public Shared Function TryParse (s As String, ByRef result As Short) As Boolean
参数
- s
- String
包含要转换的数字的字符串。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果 s
参数为 null
或 Empty、格式不正确,或者表示的数字小于 MinValue 或大于 MaxValue,则转换失败。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
示例
以下示例使用许多不同的字符串值调用 Int16.TryParse(String, Int16) 该方法。
using System;
public class StringParsing
{
public static void Main()
{
TryToParse(null);
TryToParse("16051");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
private static void TryToParse(string value)
{
short number;
bool result = Int16.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
}
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '16051' to 16051.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100)' failed.
// Attempted conversion of '01FA' failed.
open System
let tryToParse (value: string) =
match Int16.TryParse value with
| true, number -> printfn "Converted '{value}' to {number}."
| _ ->
let value = if isNull value then "" else value
printfn $"Attempted conversion of '{value}' failed."
tryToParse null
tryToParse "16051"
tryToParse "9432.0"
tryToParse "16,667"
tryToParse " -322 "
tryToParse "+4302"
tryToParse "(100);"
tryToParse "01FA"
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '16051' to 16051.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100)' failed.
// Attempted conversion of '01FA' failed.
Module StringParsing
Public Sub Main()
TryToParse(Nothing)
TryToParse("16051")
TryToParse("9432.0")
TryToParse("16,667")
TryToParse(" -322 ")
TryToParse("+4302")
TryToParse("(100)")
TryToParse("01FA")
End Sub
Private Sub TryToParse(value As String)
Dim number As Int16
Dim result As Boolean = Int16.TryParse(value, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
If value Is Nothing Then value = ""
Console.WriteLine("Attempted conversion of '{0}' failed.", value)
End If
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '' failed.
' Converted '16051' to 16051.
' Attempted conversion of '9432.0' failed.
' Attempted conversion of '16,667' failed.
' Converted ' -322 ' to -322.
' Converted '+4302' to 4302.
' Attempted conversion of '(100)' failed.
' Attempted conversion of '01FA' failed.
此方法无法转换的一些字符串 TryParse(String, Int16) 如下:
"9432.0". 转换失败,因为字符串不能包含小数分隔符;它必须仅包含整型数字。
"16,667". 转换失败,因为字符串不能包含组分隔符;它必须仅包含整型数字。
"(100)". 转换失败,因为字符串不能包含当前区域性 NumberFormatInfo.NegativeSign 和 NumberFormatInfo.NumberNegativePattern 属性定义的负号。
“01FA”。 转换失败,因为字符串不能包含十六进制数字;它必须仅包含十进制数字。
注解
该方法 Int16.TryParse(String, Int16) 不同于该方法 Int16.Parse(String) ,方法是返回一个布尔值,该值指示分析操作是否成功,而不是返回已分析 Int16 的值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的事件 s
。
该 s
参数应为数字的字符串表示形式:
[ws][sign]digits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 |
位数 | 一系列数字,范围从 0 到 9。 |
参数 s
使用 NumberStyles.Integer 样式进行解释。 除了十进制数字,只允许前导和尾随空格以及前导符号。 若要显式定义样式元素以及可以存在的 s
特定于区域性的格式设置信息,请使用该方法 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 。
该 s
参数使用为当前系统区域性初始化的对象中的 NumberFormatInfo 格式设置信息进行分析。 有关详细信息,请参阅 CurrentInfo。
此方法的 TryParse 重载将参数中的所有 s
数字解释为十进制数字。 若要分析十六进制数的字符串表示形式,请调用 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 重载。
另请参阅
- Parse(String)
- ToString()
- 分析 .NET 中的数字字符串
- 示例:.NET Core WinForms 格式设置实用工具 (C#)
- 示例:.NET Core WinForms 格式设置实用工具 (Visual Basic)
适用于
TryParse(ReadOnlySpan<Char>, Int16)
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<char> s, out short result);
static member TryParse : ReadOnlySpan<char> * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Short) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果 s
参数为 null
或 Empty、格式不符合 style
,或者表示的数字小于 MinValue 或大于 MaxValue,则转换失败。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
适用于
TryParse(String, NumberStyles, IFormatProvider, Int16)
将指定样式和区域性特定格式的数字的字符串表示形式转换为它的等效 16 位有符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- s
- String
包含要转换的数字的字符串。 该字符串使用由 style
指定的样式来进行解释。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果 s
参数为 null
或 Empty、格式不符合 style
,或者表示的数字小于 MinValue 或大于 MaxValue,则转换失败。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
例外
示例
以下示例使用多个不同的字符串值调用 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 该方法。
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "10677";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
short number;
bool result = Int16.TryParse(stringToConvert, styles,
CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
else
Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
}
}
// The example displays the following output to the console:
// Converted '10677' to 10677.
// Attempted conversion of '-30677' failed.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to -32575.
// Attempted conversion of '0x80C1' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match Int16.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
let numericString = "10677"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-30677"
let styles = NumberStyles.None
callTryParse numericString styles
let numericString = "10345.00"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "10345.72";
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "22,593"
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
callTryParse numericString styles
let numericString = "12E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "12E03"
callTryParse numericString styles
let numericString = "80c1"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x80C1"
callTryParse numericString NumberStyles.HexNumber
0
// The example displays the following output to the console:
// Converted '10677' to 10677.
// Attempted conversion of '-30677' failed.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to -32575.
// Attempted conversion of '0x80C1' failed.
Imports System.Globalization
Module StringParsing
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "10677"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-30677"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "10345.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "22,593"
styles = NumberStyles.Integer Or NumberStyles.AllowThousands
CallTryParse(numericString, styles)
numericString = "12E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "12E03"
CallTryParse(numericString, styles)
numericString = "80c1"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x80C1"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As Short
Dim result As Boolean = Int16.TryParse(stringToConvert, styles, _
CultureInfo.InvariantCulture, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.", _
Convert.ToString(stringToConvert))
End If
End Sub
End Module
' The example displays the following output to the console:
' Converted '10677' to 10677.
' Attempted conversion of '-30677' failed.
' Converted '10345.00' to 10345.
' Attempted conversion of '10345.72' failed.
' Converted '22,593' to 22593.
' Attempted conversion of '12E-01' failed.
' Converted '12E03' to 12000.
' Converted '80c1' to -32575.
' Attempted conversion of '0x80C1' failed.
注解
该方法 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 不同于该方法 Int16.Parse(String, NumberStyles, IFormatProvider) ,方法是返回一个布尔值,该值指示分析操作是否成功,而不是返回已分析 Int16 的值。 它无需使用异常处理来测试 FormatException 无效且无法成功分析的事件 s
。
该 style
参数定义样式元素 (,例如空格或正或负号) ,允许在参数中 s
成功分析操作。 它必须是枚举中的 NumberStyles 位标志的组合。 根据样式的值,该 s
参数可能包含以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
或者,如果 style
参数包括 NumberStyles.AllowHexSpecifier:
[ws]hexdigits[ws]
方括号中的项 ([ 和 ]) 是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 如果包含NumberStyles.AllowLeadingWhite标志,则可以在开头s``style 显示空格,或者在NumberStyles.AllowTrailingWhitestyle 包含标志的s 末尾显示空格。 |
$ | 特定于区域性的货币符号。 字符串中的位置由CurrencyPositivePattern参数方法provider 返回GetFormat的对象的属性NumberFormatInfo定义。 如果style 包含标志,NumberStyles.AllowCurrencySymbol则可以显示s 货币符号。 |
sign | 可选符号。 |
位数 | 从 0 到 9 的数字序列。 |
, | 区域性特定的千位分隔符。 如果包含NumberStyles.AllowThousands标志,则可以显示style``s 指定的provider 区域性的千位分隔符。 |
. | 区域性特定的小数点符号。 如果包含NumberStyles.AllowDecimalPoint标志,则可以显示style``s 由指定的provider 区域性的小数点符号。 |
fractional_digits | 数字 0 的一个或多个匹配项。 仅当包含NumberStyles.AllowDecimalPoint标志时style ,小数位数才会显示s 。 |
e | “e”或“E”字符,指示该值以指数表示法表示。 如果style 包含标志,则s 参数可以表示指数表示法中的NumberStyles.AllowExponent数字。 |
hexdigits | 从 0 到 f 或 0 到 F 的十六进制数字序列。 |
备注
无论参数的值style
如何,分析操作都会忽略任何终止 NUL (U+0000) 字符s
。
只有十进制数字的字符串 (,它与 NumberStyles.None 标志) 始终成功分析。 大多数剩余 NumberStyles 成员控制元素可能不是此输入字符串中的必需元素。 下表指示各个 NumberStyles 成员如何影响可能存在于 s
的元素。
非复合 NumberStyles 值 | 除数字外允许的元素 |
---|---|
NumberStyles.None | 仅十进制数字。 |
NumberStyles.AllowDecimalPoint | . 和 fractional_digits 元素。 但是, fractional_digits 必须只包含一个或多个数字或方法返回 false 。 |
NumberStyles.AllowExponent | 该 s 参数还可以使用指数表示法。 如果 s 表示指数表示法中的数字,则必须在数据类型范围内 Int16 表示一个整数,而不带非零小数分量。 |
NumberStyles.AllowLeadingWhite | 开头 的 ws 元素。 |
NumberStyles.AllowTrailingWhite | 结尾 处的 ws 元素。 |
NumberStyles.AllowLeadingSign | 可以在 数字 之前显示一个符号。 |
NumberStyles.AllowTrailingSign | 符号可以在 数字 之后显示。 |
NumberStyles.AllowParentheses | 括住数值的括号形式的 符号 元素。 |
NumberStyles.AllowThousands | 元素。 |
NumberStyles.AllowCurrencySymbol | 元素 $ 。 |
NumberStyles.Currency | 所有元素。 s 参数不能表示十六进制数或指数表示法中的数字。 |
NumberStyles.Float | The ws element at the beginning or end of s , sign at the beginning of s , and the . 符号开头。 该 s 参数还可以使用指数表示法。 |
NumberStyles.Number | ws、sign、thousands 分隔符 (、) 和小数点 (。) 元素。 |
NumberStyles.Any | 除不能表示十六进制数外 s 的所有样式。 |
如果使用标志 NumberStyles.AllowHexSpecifier , s
则必须是没有前缀的十六进制值。 例如,“9AF3”已成功分析,但“0x9AF3”不会。 可以存在于 style
其中的唯一其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 NumberStyles (枚举具有复合样式,NumberStyles.HexNumber包括空格标志.)
该 provider
参数是一个 IFormatProvider 实现,例如对象 CultureInfo 或 NumberFormatInfo 对象,其 GetFormat 方法返回对象 NumberFormatInfo 。 该 NumberFormatInfo 对象提供有关格式 s
的区域性特定信息。 null
如果是provider
,NumberFormatInfo则使用当前区域性的对象。
另请参阅
适用于
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16)
将指定样式和区域性特定格式的数字的范围表示形式转换为它的等效 16 位带符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。 该范围使用由 style
指定的样式来进行解释。
- style
- NumberStyles
枚举值的按位组合,用于指示可出现在 s
中的样式元素。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- Int16
当此方法返回时,如果转换成功,则包含与 s
中所包含数字等效的 16 位有符号整数值;如果转换失败,则为零。 如果 s
参数为 null
或 Empty、格式不符合 style
,或者表示的数字小于 MinValue 或大于 MaxValue,则转换失败。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。