UInt16.TryParse 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将数字的字符串表示形式转换为它的等效 16 位无符号整数。 一个指示转换是否成功的返回值。
重载
TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt16) |
尝试将 UTF-8 字符范围解析为值。 |
TryParse(ReadOnlySpan<Char>, UInt16) |
尝试将数字的范围表示形式转换为其等效的 16 位无符号整数。 一个指示转换是否成功的返回值。 |
TryParse(String, UInt16) |
尝试将数字的字符串表示形式转换为等效的 16 位无符号整数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt16) |
尝试将字符范围解析为值。 |
TryParse(String, IFormatProvider, UInt16) |
尝试将字符串分析为值。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt16) |
尝试将 UTF-8 字符范围解析为值。 |
TryParse(ReadOnlySpan<Byte>, UInt16) |
尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的 16 位无符号整数。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, UInt16) |
尝试将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 16 位无符号整数。 一个指示转换是否成功的返回值。 |
TryParse(String, NumberStyles, IFormatProvider, UInt16) |
尝试将数字的指定样式和区域性特定格式的字符串表示形式转换为其等效的 16 位无符号整数。 一个指示转换是否成功的返回值。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
尝试将 UTF-8 字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result) = IUtf8SpanParsable<System::UInt16>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out ushort result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As UShort) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符范围。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
- result
- UInt16
返回时,包含成功分析 utf8Text
的结果或失败时未定义的值。
返回
true
如果 utf8Text
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
重要
此 API 不符合 CLS。
尝试将数字的范围表示形式转换为其等效的 16 位无符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::UInt16 % result);
public static bool TryParse (ReadOnlySpan<char> s, out ushort result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, out ushort result);
static member TryParse : ReadOnlySpan<char> * uint16 -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * uint16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As UShort) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。
- result
- UInt16
当此方法返回时,如果转换成功,则包含与 s
中所含数字等效的 16 位无符号整数值;如果转换失败,则包含零。 如果 s
参数为 null
或 Empty 的格式不正确,则转换失败。 ,或 表示小于 UInt16.MinValue 或大于 UInt16.MaxValue 的数字。 此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
- 属性
适用于
TryParse(String, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
尝试将数字的字符串表示形式转换为等效的 16 位无符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::UInt16 % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, out ushort result);
public static bool TryParse (string? s, out ushort result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, out ushort result);
[<System.CLSCompliant(false)>]
static member TryParse : string * uint16 -> bool
static member TryParse : string * uint16 -> bool
Public Shared Function TryParse (s As String, ByRef result As UShort) As Boolean
参数
- s
- String
表示要转换的数字的字符串。
- result
- UInt16
当此方法返回时,如果转换成功,则包含与 s
中所含数字等效的 16 位无符号整数值;如果转换失败,则包含零。 如果参数为 null
或 Empty,格式不正确,或表示小于 UInt16.MinValue 或大于 UInt16.MaxValue 的数字,则转换失败。s
此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
- 属性
示例
下面的示例为字符串数组中的每个元素调用 TryParse(String, UInt16) 一次 方法。
string[] numericStrings = { "1293.8", "+1671.7", "28347.",
" 33113684 ", "(0)", "-0", "-1",
"+1293617", "18-", "119870", "31,024",
" 3127094 ", "00700000" };
uint number;
foreach (string numericString in numericStrings)
{
if (UInt32.TryParse(numericString, out number))
Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
else
Console.WriteLine("Cannot convert '{0}' to a UInt32.", numericString);
}
// The example displays the following output:
// Cannot convert '1293.8' to a UInt32.
// Cannot convert '+1671.7' to a UInt32.
// Cannot convert '28347.' to a UInt32.
// Converted ' 33113684 ' to 33113684.
// Cannot convert '(0)' to a UInt32.
// Converted '-0' to 0.
// Cannot convert '-1' to a UInt32.
// Converted '+1293617' to 1293617.
// Cannot convert '18-' to a UInt32.
// Converted '119870' to 119870.
// Cannot convert '31,024' to a UInt32.
// Converted ' 3127094 ' to 3127094.
// Converted '0070000' to 70000.
let numericStrings =
[| "1293.8"; "+1671.7"; "28347."
" 33113684 "; "(0)"; "-0"; "-1"
"+1293617"; "18-"; "119870"; "31,024"
" 3127094 "; "00700000" |]
for numericString in numericStrings do
match UInt32.TryParse numericString with
| true, number ->
printfn $"Converted '{numericString}' to {number}."
| _ ->
printfn $"Cannot convert '{numericString}' to a UInt32."
// The example displays the following output:
// Cannot convert '1293.8' to a UInt32.
// Cannot convert '+1671.7' to a UInt32.
// Cannot convert '28347.' to a UInt32.
// Converted ' 33113684 ' to 33113684.
// Cannot convert '(0)' to a UInt32.
// Converted '-0' to 0.
// Cannot convert '-1' to a UInt32.
// Converted '+1293617' to 1293617.
// Cannot convert '18-' to a UInt32.
// Converted '119870' to 119870.
// Cannot convert '31,024' to a UInt32.
// Converted ' 3127094 ' to 3127094.
// Converted '0070000' to 70000.
Dim numericStrings() As String = {"1293.8", "+1671.7", "28347.",
" 33113684 ", "(0)", "-0", "-1",
"+1293617", "18-", "119870",
"31,024", " 3127094 ", "0070000" }
Dim number As UInteger
For Each numericString As String In numericStrings
If UInt32.TryParse(numericString, number) Then
Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
Else
Console.WriteLine("Cannot convert '{0}' to a UInt32.", numericString)
End If
Next
' The example displays the following output:
' Cannot convert '1293.8' to a UInt32.
' Cannot convert '+1671.7' to a UInt32.
' Cannot convert '28347.' to a UInt32.
' Converted ' 33113684 ' to 33113684.
' Cannot convert '(0)' to a UInt32.
' Converted '-0' to 0.
' Cannot convert '-1' to a UInt32.
' Converted '+1293617' to 1293617.
' Cannot convert '18-' to a UInt32.
' Converted '119870' to 119870.
' Cannot convert '31,024' to a UInt32.
' Converted ' 3127094 ' to 3127094.
' Converted '0070000' to 70000.
注解
方法 TryParse(String, UInt16) 与 方法类似 Parse(String) ,只是在转换失败时不会引发异常。 如果 无效且无法成功分析,FormatExceptions
则此方法无需使用异常处理来测试 。
参数 s
应为采用以下格式的十进制数的字符串表示形式:
[ws][sign]digits[ws]
方括号 ([ and ]) 中的元素是可选的。 下表对每个元素进行了描述。
元素 | 说明 |
---|---|
ws | 可选空格。 |
sign | 可选符号。 有效的符号字符由 NumberFormatInfo.NegativeSign 当前区域性的 和 NumberFormatInfo.PositiveSign 属性确定。 |
位数 | 范围从 0 到 9 的十进制数字序列。 |
注意
参数 s
指定的字符串不能包含任何组分隔符或小数分隔符,并且不能包含小数部分。
参数 s
通过使用 NumberStyles.Integer 样式进行解释。 除了十进制数字之外,只允许使用带前导符号的空格和尾随空格。 若要使用中存在的 s
区域性特定格式设置信息显式定义样式元素,请 TryParse(String, NumberStyles, IFormatProvider, UInt16) 调用 方法。
通过使用 s
当前系统区域性的 对象中的 NumberFormatInfo 格式设置信息来分析 参数。 有关详细信息,请参阅 NumberFormatInfo.CurrentInfo。
此重载将 参数中的所有 s
数字解释为十进制数字。 若要分析十六进制数的字符串表示形式,请改为调用 TryParse(String, NumberStyles, IFormatProvider, UInt16) 重载。
另请参阅
- Parse(String)
- ToString()
- 分析 .NET 中的数字字符串
- 示例:.NET Core WinForms 格式设置实用工具 (C#)
- 示例:.NET Core WinForms 格式设置实用工具 (Visual Basic)
适用于
TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
尝试将字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result) = ISpanParsable<System::UInt16>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out ushort result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As UShort) As Boolean
参数
- s
- ReadOnlySpan<Char>
要分析的字符范围。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- UInt16
此方法返回时,包含成功分析 s
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(String, IFormatProvider, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
尝试将字符串分析为值。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result) = IParsable<System::UInt16>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out ushort result);
static member TryParse : string * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As UShort) As Boolean
参数
- s
- String
要分析的字符串。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- UInt16
此方法返回时,包含成功分析 s
的结果或失败时的未定义值。
返回
true
如果 s
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
尝试将 UTF-8 字符范围解析为值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result) = System::Numerics::INumberBase<System::UInt16>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out ushort result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As UShort) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
要分析的 UTF-8 字符范围。
- style
- NumberStyles
可以存在于 中的 utf8Text
数字样式的按位组合。
- provider
- IFormatProvider
一个对象,提供有关 utf8Text
的区域性特定格式设置信息。
- result
- UInt16
返回时,包含成功分析 utf8Text
的结果或失败时未定义的值。
返回
true
如果 utf8Text
已成功分析,则为 ;否则为 false
。
适用于
TryParse(ReadOnlySpan<Byte>, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
尝试将包含数字字符串表示形式的 UTF-8 字符范围转换为其等效的 16 位无符号整数。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::UInt16 % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out ushort result);
static member TryParse : ReadOnlySpan<byte> * uint16 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As UShort) As Boolean
参数
- utf8Text
- ReadOnlySpan<Byte>
包含表示要转换的数字的 UTF-8 字符的跨度。
- result
- UInt16
此方法返回时,如果转换成功,则包含与 中包含的 utf8Text
数字等效的 16 位无符号整数值;如果转换失败,则包含零。 此参数未经初始化即进行传递;最初在 result 中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 utf8Text
;否则为 false
。
适用于
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
重要
此 API 不符合 CLS。
尝试将指定样式和区域性特定格式的数字的范围表示形式转换为其等效的 16 位无符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result) = System::Numerics::INumberBase<System::UInt16>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ushort result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out ushort result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ushort result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * uint16 -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As UShort) As Boolean
参数
- s
- ReadOnlySpan<Char>
一个范围,包含表示要转换的数字的字符。 该范围使用由 style
参数指定的样式来进行解释。
- style
- NumberStyles
枚举值的一个按位组合,指示 s
所允许的格式。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- UInt16
当此方法返回时,如果转换成功,则包含与 s
中所含数字等效的 16 位无符号整数值;如果转换失败,则包含零。 如果参数为 null
或 Empty、的格式不符合 style
,或者表示小于 UInt16.MinValue 或大于 UInt16.MaxValue 的数字,则转换失败。s
此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
- 属性
适用于
TryParse(String, NumberStyles, IFormatProvider, UInt16)
- Source:
- UInt16.cs
- Source:
- UInt16.cs
- Source:
- UInt16.cs
尝试将数字的指定样式和区域性特定格式的字符串表示形式转换为其等效的 16 位无符号整数。 一个指示转换是否成功的返回值。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt16 % result) = System::Numerics::INumberBase<System::UInt16>::TryParse;
[System.CLSCompliant(false)]
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out ushort result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ushort result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ushort result);
[<System.CLSCompliant(false)>]
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * uint16 -> bool
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * uint16 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As UShort) As Boolean
参数
- s
- String
表示要转换的数字的字符串。 该字符串使用由 style
参数指定的样式来进行解释。
- style
- NumberStyles
枚举值的一个按位组合,指示 s
所允许的格式。 要指定的一个典型值为 Integer。
- provider
- IFormatProvider
一个对象,提供有关 s
的区域性特定格式设置信息。
- result
- UInt16
当此方法返回时,如果转换成功,则包含与 s
中所含数字等效的 16 位无符号整数值;如果转换失败,则包含零。 如果参数为 null
或 Empty、的格式不符合 style
,或者表示小于 UInt16.MinValue 或大于 UInt16.MaxValue 的数字,则转换失败。s
此参数未经初始化即进行传递;最初在 result
中提供的任何值都会被覆盖。
返回
如果 true
成功转换,则为 s
;否则为 false
。
- 属性
例外
示例
以下示例使用许多不同的字符串和NumberStyles值调用 TryParse(String, NumberStyles, IFormatProvider, UInt16) 方法。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "10603";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-10603";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "29103.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "2210E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "9112E-01";
CallTryParse(numericString, styles);
numericString = "312E01";
CallTryParse(numericString, styles);
numericString = "FFC8";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x8F8C";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
ushort number;
bool result = UInt16.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:
// Converted '10603' to 10603.
// Attempted conversion of '-10603' failed.
// Converted '29103.00' to 29103.
// Attempted conversion of '10345.72' failed.
// Converted '2210E-01' to 221.
// Attempted conversion of '9112E-01' failed.
// Converted '312E01' to 3120.
// Converted 'FFC8' to 65480.
// Attempted conversion of '0x8F8C' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match UInt16.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
do
let numericString = "10603"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-10603"
let styles = NumberStyles.None
callTryParse numericString styles
let numericString = "29103.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 = "2210E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "9112E-01"
callTryParse numericString styles
let numericString = "312E01"
callTryParse numericString styles
let numericString = "FFC8"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x8F8C"
callTryParse numericString NumberStyles.HexNumber
// The example displays the following output:
// Converted '10603' to 10603.
// Attempted conversion of '-10603' failed.
// Converted '29103.00' to 29103.
// Attempted conversion of '10345.72' failed.
// Converted '2210E-01' to 221.
// Attempted conversion of '9112E-01' failed.
// Converted '312E01' to 3120.
// Converted 'FFC8' to 65480.
// Attempted conversion of '0x8F8C' failed.
Imports System.Globalization
Module Example
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "10603"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-10603"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "29103.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "2210E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "9112E-01"
CallTryParse(numericString, styles)
numericString = "312E01"
CallTryParse(numericString, styles)
numericString = "FFC8"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x8F8C"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As UShort
Dim result As Boolean = UInt16.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 '10603' to 10603.
' Attempted conversion of '-10603' failed.
' Converted '29103.00' to 29103.
' Attempted conversion of '10345.72' failed.
' Converted '2210E-01' to 221.
' Attempted conversion of '9112E-01' failed.
' Converted '312E01' to 3120.
' Converted 'FFC8' to 65480.
' Attempted conversion of '0x8F8C' failed.
注解
方法 TryParse(String, NumberStyles, IFormatProvider, UInt16) 与 方法类似 Parse(String, NumberStyles, IFormatProvider) ,只是在转换失败时不会引发异常。 此方法无需使用异常处理来测试 FormatExceptions
是否无效且无法成功分析。
参数 style
定义样式元素 (,如空格或正或负号) ,这些元素在 参数中 s
允许分析操作成功。 它必须是 枚举中的位标志 NumberStyles 的组合。 根据 的值 style
, s
参数可能包括以下元素:
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
方括号中的项 ([ 和 ]) 是可选的。 或者,如果 style
参数包含 AllowHexSpecifier,则 s
参数可以包含以下元素:
[ws]hexdigits[ws]
下表对每个元素进行了描述。
Ws 可选空格。 如果包含 标志,则开头s
会出现空格;如果style
style
包含 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 标志,s
则结尾处会显示空格。
$ 区域性特定的货币符号。 它在字符串中的位置由 CurrencyPositivePattern 参数的 方法provider
返回GetFormat的 NumberFormatInfo 对象的 属性定义。 如果style
包含 标志,NumberStyles.AllowCurrencySymbol则可以在 中s
显示货币符号。
标志 可选符号。 如果包含 标志,则符号可以出现在 的s
开头;如果style
style
包含 NumberStyles.AllowTrailingSign 标志,则它可能显示在 的末尾s
。NumberStyles.AllowLeadingSign 如果style
包含 标志,NumberStyles.AllowParentheses则可以在 中使用s
括号来指示负值。 但是,如果存在负号, s
则 只能表示分析操作成功的值零。
数字 从 0 到 9 的数字序列。
、 区域性特定的组分隔符。 如果style
包含 标志,则可以在 中s
显示 由 provider
指定的区域性的NumberStyles.AllowThousands组分隔符。
.
区域性特定的小数点符号。 如果包含 标志,则 指定的provider
区域性的小数点符号可以出现在 中s
。NumberStyles.AllowDecimalPointstyle
fractional_digits 出现一个或多个数字 0。 仅当包含 标志时style
,NumberStyles.AllowDecimalPoint小数位数才能显示在 中s
。
E“e”或“E”字符,指示值以指数 (科学) 表示法表示。 如果style
包含 标志,则 s
参数可以表示指数表示法中的NumberStyles.AllowExponent数字。
exponential_digits 从 0 到 9 的数字序列。 如果style
包含 标志,则 s
参数可以表示指数表示法中的NumberStyles.AllowExponent数字。
hexdigits 从 0 到 f 或 0 到 F 的十六进制数字序列。
注意
分析操作将忽略中 s
任何 (U+0000) 字符的终止 NUL,而不考虑 参数的值 style
。
仅具有十进制数字的字符串 (对应于 NumberStyles.None 标志) 始终成功分析。 大多数剩余 NumberStyles 成员控制此输入字符串中可能存在但不需要存在的元素。 下表指示各个 NumberStyles 成员如何影响 中可能存在 s
的元素。
非复合 NumberStyles 值 |
除数字外的值允许的元素 |
---|---|
None | 仅十进制数字。 |
AllowDecimalPoint | 小数点 (.) 和 fractional_digits 元素。 但是, fractional_digits 只能包含一个或多个 0 位数字,否则方法返回 false 。 |
AllowExponent | “e”或“E”字符,指示指数表示法以及 exponential_digits。 如果 s 以指数表示法表示数字,则它不能具有非零的小数部分。 |
AllowLeadingWhite | 开头的 s ws 元素。 |
AllowTrailingWhite | 位于 末尾的 s ws 元素。 |
AllowLeadingSign | 数字之前的符号元素。 |
AllowTrailingSign | 数字后面的符号元素。 |
AllowParentheses | 带零数值的括号形式的 符号 元素。 |
AllowThousands | 组分隔符 (、) 元素。 |
AllowCurrencySymbol | 元素 ($) 货币。 |
Currency | 所有元素。 但是, s 不能表示十六进制数或指数表示法中的数字。 |
Float | 开头或结尾处的 s ws元素,符号位于 的s 开头,小数点 (.) 符号。 参数 s 还可以使用指数表示法。 |
Number | ws、符号、组分隔符 (、) 和小数点 (.) 元素。 |
Any | 所有元素。 但是, s 不能表示十六进制数。 |
如果使用标志 NumberStyles.AllowHexSpecifier , s
必须是十六进制值。 有效的十六进制数字为 0 到 9、a 到 f 以及 A 到 F。不支持前缀(如“0x”),导致分析操作失败。 中唯一可以存在的 style
其他标志是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。
NumberStyles (枚举具有复合样式 HexNumber,其中包括空格标志 )
注意
如果 s
是十六进制数的字符串表示形式,则它前面不能有任何修饰 (,如 0x
或 &h
) 将其区分为十六进制数。 这会导致转换失败。
参数 provider
是实现 IFormatProvider 。 其 GetFormat 方法返回一个 NumberFormatInfo 对象,该对象提供有关 格式的 s
区域性特定信息。 参数 provider
可以是以下任意一种:
一个 CultureInfo 对象,该对象表示提供格式设置信息的区域性。 其 GetFormat 方法返回对象, NumberFormatInfo 该对象提供该区域性的数字格式设置信息。
一个 NumberFormatInfo 提供数字格式设置信息的 对象。 (它的 实现 GetFormat 只返回自身。)
实现 的 IFormatProvider自定义对象。 其 GetFormat 方法实例化并返回 NumberFormatInfo 提供格式设置信息的 对象。
如果 provider
为 null
,则 NumberFormatInfo 使用当前区域性的 对象。
另请参阅
适用于
反馈
https://aka.ms/ContentUserFeedback。
即将发布:在整个 2024 年,我们将逐步淘汰作为内容反馈机制的“GitHub 问题”,并将其取代为新的反馈系统。 有关详细信息,请参阅:提交和查看相关反馈