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)
- 來源:
- UInt16.cs
- 來源:
- 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)
- 來源:
- UInt16.cs
- 來源:
- UInt16.cs
- 來源:
- 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
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
- 屬性
適用於
TryParse(String, UInt16)
- 來源:
- UInt16.cs
- 來源:
- UInt16.cs
- 來源:
- 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
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 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) 方法一樣,不同之處在於,如果轉換失敗,它不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 s
無效,而且無法成功剖析。
參數 s
應該是以下列形式表示十進位數的字串:
[ws][sign]digits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空格符。 |
簽署 | 選擇性符號。 有效的符號字元取決於 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 中剖析數值字串
- Sample: .NET Core WinForms Formatting Utility (C#) (範例:.NET Core WinForms 格式化公用程式 (C#))
- Sample: .NET Core WinForms Formatting Utility (Visual Basic) (範例:.NET Core WinForms 格式化公用程式 (Visual Basic))
適用於
TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt16)
- 來源:
- UInt16.cs
- 來源:
- UInt16.cs
- 來源:
- 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)
- 來源:
- UInt16.cs
- 來源:
- UInt16.cs
- 來源:
- 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)
- 來源:
- UInt16.cs
- 來源:
- 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)
- 來源:
- UInt16.cs
- 來源:
- 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位無符號整數值,如果轉換失敗則為零。 此參數會以未初始化的狀態來傳遞,並會覆寫任何原本在結果中提供的值。
傳回
如果 utf8Text
轉換成功,則為 true
,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, UInt16)
- 來源:
- UInt16.cs
- 來源:
- UInt16.cs
- 來源:
- 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 位元不帶正負號整數,如果轉換失敗則為零。 如果 s
參數為 null
或 Empty,則轉換會失敗,且格式不符合 style
,或代表小於 UInt16.MinValue 或大於 UInt16.MaxValue 的數位。 這個參數未初始化便傳遞,result
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
- 屬性
適用於
TryParse(String, NumberStyles, IFormatProvider, UInt16)
- 來源:
- UInt16.cs
- 來源:
- UInt16.cs
- 來源:
- 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 位元不帶正負號整數,如果轉換失敗則為零。 如果 s
參數為 null
或 Empty,則轉換會失敗,且格式不符合 style
,或代表小於 UInt16.MinValue 或大於 UInt16.MaxValue 的數位。 這個參數未初始化便傳遞,result
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
- 屬性
例外狀況
範例
下列範例會 TryParse(String, NumberStyles, IFormatProvider, UInt16) 使用數個不同的字串和 NumberStyles 值來呼叫 方法。
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) 方法一樣,不同之處在於,如果轉換失敗,則不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 s
無效,而且無法成功剖析。
參數 style
會定義樣式專案 (,例如空格符或正負號) ,參數中 s
允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 style
, s
參數可能包含下列元素:
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
方括弧中的專案 ([ 和 ]) 是選擇性專案。 或者,如果 style
參數包含 AllowHexSpecifier,則 s
參數可能包含下列元素:
[ws]hexdigits[ws]
下表說明每個元素。
Ws 選擇性空格符。 如果 包含 旗標,則會在 開頭s
出現空格符,如果包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾style
s
。NumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 字串中的位置是由 CurrencyPositivePattern 參數的 方法provider
所GetFormat傳回之 對象的 屬性NumberFormatInfo所定義。 如果style
包含旗標,NumberStyles.AllowCurrencySymbol則可以在 中s
顯示貨幣符號。
標誌 選擇性符號。 如果 包含旗標,則符號可以出現在 s
開頭,如果包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 結尾style
s
。NumberStyles.AllowLeadingSignstyle
如果包含 NumberStyles.AllowParentheses 旗標,則可以在 中使用s
括弧來表示負值style
。 不過,如果出現負號, s
則只能表示剖析作業成功的值零。
數位 從 0 到 9 的數位序列。
、 特定文化特性的群組分隔符。 如果包含旗標,則 指定provider
文化特性的群組分隔符可能會出現在 中s
。NumberStyles.AllowThousandsstyle
.
特定文化特性的小數點符號。 如果包含旗標,則 所指定provider
文化特性的小數點符號可能會出現在 中s
。NumberStyles.AllowDecimalPointstyle
fractional_digits 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時style
,小數位數才會出現在 中s
。
E “e” 或 “E” 字元,表示此值以指數表示法 (科學) 表示法。 如果style
包含 NumberStyles.AllowExponent 旗標,參數s
可以代表指數表示法的數位。
exponential_digits 從 0 到 9 的數位序列。 如果style
包含 NumberStyles.AllowExponent 旗標,參數s
可以代表指數表示法的數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。
注意
中任何終止的 NUL (U+0000) 字元 s
都會被剖析作業忽略,不論自變數的值 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 | 以括弧括住零數值形式的 sign 元素。 |
AllowThousands | 群組分隔符 (,) 專案。 |
AllowCurrencySymbol | 貨幣 ($) 專案。 |
Currency | 所有元素。 不過, s 不能以指數表示法表示十六進位數位或數位。 |
Float | 開頭或結尾的 s ws 元素,在 開頭s 為 符號,而小數點 (。) 符號。 參數 s 也可以使用指數表示法。 |
Number | ws、sign、group separator (、) 和小數點 (。) 元素。 |
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 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應