SByte.TryParse 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將數字的字串表示轉換成它的對等 8 位元帶正負號的整數。 傳回碼 (Return Code) 會指出轉換是成功或是失敗。
多載
TryParse(ReadOnlySpan<Byte>, IFormatProvider, SByte) |
嘗試將UTF-8字元的範圍剖析為值。 |
TryParse(ReadOnlySpan<Char>, SByte) |
嘗試將數字的範圍表示轉換為其對等的 SByte,並傳回值以指出轉換是否成功。 |
TryParse(String, SByte) |
嘗試將數字的字串表示轉換成其相等的 SByte,並傳回一個值表示轉換是否成功。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, SByte) |
嘗試將字元範圍剖析成值。 |
TryParse(String, IFormatProvider, SByte) |
嘗試將字串剖析成值。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, SByte) |
嘗試將UTF-8字元的範圍剖析為值。 |
TryParse(ReadOnlySpan<Byte>, SByte) |
嘗試將包含數位字串表示的UTF-8字元範圍轉換為其8位帶正負號的整數對等專案。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, SByte) |
嘗試將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等的 SByte,並傳回值以指出轉換是否成功。 |
TryParse(String, NumberStyles, IFormatProvider, SByte) |
使用指定的文化特性特定格式資訊和格式樣式,將日期和時間的指定字串表示轉換為其對等的 SByte,並傳回值,這個值表示轉換是否成功。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
嘗試將UTF-8字元的範圍剖析為值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = IUtf8SpanParsable<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As SByte) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的UTF-8字元範圍。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
- result
- SByte
傳回時,包含成功剖析 utf8Text
或失敗時未定義值的結果。
傳回
true
如果 utf8Text
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
重要
此 API 不符合 CLS 規範。
嘗試將數字的範圍表示轉換為其對等的 SByte,並傳回值以指出轉換是否成功。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::SByte % result);
public static bool TryParse (ReadOnlySpan<char> s, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, out sbyte result);
static member TryParse : ReadOnlySpan<char> * sbyte -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * sbyte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As SByte) As Boolean
參數
- s
- ReadOnlySpan<Char>
範圍,其包含代表所要轉換數字的字元。
- result
- SByte
如果轉換成功,這個方法會傳回包含與 s
中內含數字相等的 8 位元帶正負號整數,如果轉換失敗則為零。 如果 s
參數為 null
或 Empty,或 格式不正確,或代表小於 SByte.MinValue 或大於 SByte.MaxValue 的數位,則轉換會失敗。 這個參數未初始化便傳遞,result
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
- 屬性
適用於
TryParse(String, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
重要
此 API 不符合 CLS 規範。
嘗試將數字的字串表示轉換成其相等的 SByte,並傳回一個值表示轉換是否成功。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::SByte % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, out sbyte result);
public static bool TryParse (string? s, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, out sbyte result);
[<System.CLSCompliant(false)>]
static member TryParse : string * sbyte -> bool
static member TryParse : string * sbyte -> bool
Public Shared Function TryParse (s As String, ByRef result As SByte) As Boolean
參數
- s
- String
字串,其包含要轉換的數字。
- result
- SByte
如果轉換成功,這個方法會傳回包含與 s
中內含數字相等的 8 位元帶正負號整數,如果轉換失敗則為零。 如果 s
參數為 null
或 Empty,或 格式不正確,或代表小於 SByte.MinValue 或大於 SByte.MaxValue 的數位,則轉換會失敗。 這個參數未初始化便傳遞,result
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
- 屬性
範例
下列範例會呼叫 方法,嘗試將陣列 SByte 中的字串轉換成值 TryParse(String, SByte) 。
string[] numericStrings = {"-3.6", "12.8", "+16.7", " 3 ", "(17)",
"-17", "+12", "18-", "987", "1,024", " 127 "};
sbyte number;
foreach (string numericString in numericStrings)
{
if (sbyte.TryParse(numericString, out number))
Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
else
Console.WriteLine("Cannot convert '{0}' to an SByte.", numericString);
}
// The example displays the following output to the console:
// Cannot convert '-3.6' to an SByte.
// Cannot convert '12.8' to an SByte.
// Cannot convert '+16.7' to an SByte.
// Converted ' 3 ' to 3.
// Cannot convert '(17)' to an SByte.
// Converted '-17' to -17.
// Converted '+12' to 12.
// Cannot convert '18-' to an SByte.
// Cannot convert '987' to an SByte.
// Cannot convert '1,024' to an SByte.
// Converted ' 127 ' to 127.
open System
let numericStrings =
[| "-3.6"; "12.8"; "+16.7"; " 3 "; "(17)"
"-17"; "+12"; "18-"; "987"; "1,024"; " 127 " |]
for numericString in numericStrings do
match SByte.TryParse numericString with
| true, number ->
printfn $"Converted '{numericString}' to {number}."
| _ ->
printfn $"Cannot convert '{numericString}' to an SByte."
// The example displays the following output to the console:
// Cannot convert '-3.6' to an SByte.
// Cannot convert '12.8' to an SByte.
// Cannot convert '+16.7' to an SByte.
// Converted ' 3 ' to 3.
// Cannot convert '(17)' to an SByte.
// Converted '-17' to -17.
// Converted '+12' to 12.
// Cannot convert '18-' to an SByte.
// Cannot convert '987' to an SByte.
// Cannot convert '1,024' to an SByte.
// Converted ' 127 ' to 127.
Dim numericStrings() As String = {"-3.6", "12.8", "+16.7", " 3 ", _
"(17)", "-17", "+12", "18-", "987", _
"1,024", " 127 "}
Dim number As SByte
For Each numericString As String In numericStrings
If SByte.TryParse(numericString, number) Then
Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
Else
Console.WriteLine("Cannot convert '{0}' to an SByte.", numericString)
End If
Next
' The example displays the following output to the console:
' Cannot convert '-3.6' to an SByte.
' Cannot convert '12.8' to an SByte.
' Cannot convert '+16.7' to an SByte.
' Converted ' 3 ' to 3.
' Cannot convert '(17)' to an SByte.
' Converted '-17' to -17.
' Converted '+12' to 12.
' Cannot convert '18-' to an SByte.
' Cannot convert '987' to an SByte.
' Cannot convert '1,024' to an SByte.
' Converted ' 127 ' to 127.
備註
方法 SByte.TryParse(String, SByte) 就像 SByte.Parse(String) 方法一樣,不同之處在於,如果轉換失敗,它不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 value
無效,而且無法成功剖析。
參數 s
應該是以下列形式表示十進位數的字串:
[ws][sign]digits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空格符。 |
簽署 | 選擇性符號。 有效的符號字元取決於 NumberFormatInfo.NegativeSign 目前文化特性的 和 NumberFormatInfo.PositiveSign 屬性。 |
數字 | 小數位數序列,範圍從0到9。 |
注意
參數指定的 value
字串不能包含任何群組分隔符或小數分隔符,而且不能有小數部分。
參數 s
是使用 NumberStyles.Integer 樣式來解譯。 除了十進位數之外,只允許前置和尾端有前置符號的空格。 若要使用可以存在於 value
中的文化特性特定格式資訊明確定義樣式專案,請呼叫 TryParse(String, NumberStyles, IFormatProvider, SByte) 方法。
參數 s
是使用物件中 NumberFormatInfo 目前文化特性的格式資訊進行剖析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。
這個多載會將 參數中的所有 value
數位解譯為十進位數。 若要剖析十六進位數位的字串表示法,請改為呼叫 TryParse(String, NumberStyles, IFormatProvider, SByte) 多載。
另請參閱
- 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, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
嘗試將字元範圍剖析成值。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = ISpanParsable<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As SByte) As Boolean
參數
- s
- ReadOnlySpan<Char>
要剖析的字元範圍。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
- result
- SByte
當這個方法傳回時,會包含成功剖析 s
的結果,或失敗時未定義的值。
傳回
true
如果 s
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(String, IFormatProvider, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
嘗試將字串剖析成值。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = IParsable<System::SByte>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out sbyte result);
static member TryParse : string * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As SByte) As Boolean
參數
- s
- String
要剖析的字串。
- provider
- IFormatProvider
提供關於 s
之特定文化特性格式資訊的物件。
- result
- SByte
當這個方法傳回時,包含成功剖析 s
或失敗時未定義值的結果。
傳回
true
如果 s
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
嘗試將UTF-8字元的範圍剖析為值。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
要剖析的UTF-8字元範圍。
- style
- NumberStyles
數字樣式的位元組合,可以存在於 中 utf8Text
。
- provider
- IFormatProvider
提供關於 utf8Text
之特定文化特性格式資訊的物件。
- result
- SByte
傳回時,包含成功剖析 utf8Text
或失敗時未定義值的結果。
傳回
true
如果 utf8Text
已成功剖析,則為 ,否則為 false
。
適用於
TryParse(ReadOnlySpan<Byte>, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
嘗試將包含數位字串表示的UTF-8字元範圍轉換為其8位帶正負號的整數對等專案。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::SByte % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out sbyte result);
static member TryParse : ReadOnlySpan<byte> * sbyte -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As SByte) As Boolean
參數
- utf8Text
- ReadOnlySpan<Byte>
範圍,包含代表要轉換之數位的UTF-8字元。
- result
- SByte
當這個方法傳回時,會包含等於轉換成功時所包含的 utf8Text
數位的8位帶正負號整數值,如果轉換失敗,則為零。 此參數會以未初始化的狀態來傳遞,並會覆寫任何原本在結果中提供的值。
傳回
如果 utf8Text
轉換成功,則為 true
,否則為 false
。
適用於
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
重要
此 API 不符合 CLS 規範。
嘗試將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等的 SByte,並傳回值以指出轉換是否成功。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean
參數
- s
- ReadOnlySpan<Char>
範圍,其包含代表所要轉換數字的字元。
- style
- NumberStyles
列舉值的位元組合,其表示 s
所允許的格式。 一般會指定的值是 Integer。
- provider
- IFormatProvider
物件,其提供關於 s
的特定文化特性格式資訊。
- result
- SByte
當這個方法傳回時,如果轉換成功便包含相對於 s
中所含數字的 8 位元帶正負號的整數,如果轉換失敗則為零。 如果 s
參數為 null
或 Empty,且格式不符合 style
,或代表小於 SByte.MinValue 或大於 SByte.MaxValue 的數位,則轉換會失敗。 這個參數未初始化便傳遞,result
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
- 屬性
適用於
TryParse(String, NumberStyles, IFormatProvider, SByte)
- 來源:
- SByte.cs
- 來源:
- SByte.cs
- 來源:
- SByte.cs
使用指定的文化特性特定格式資訊和格式樣式,將日期和時間的指定字串表示轉換為其對等的 SByte,並傳回值,這個值表示轉換是否成功。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::SByte % result) = System::Numerics::INumberBase<System::SByte>::TryParse;
[System.CLSCompliant(false)]
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out sbyte result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out sbyte result);
[<System.CLSCompliant(false)>]
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * sbyte -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As SByte) As Boolean
參數
- s
- String
字串,表示要轉換的數字。
- style
- NumberStyles
列舉值的位元組合,其表示 s
所允許的格式。 一般會指定的值是 Integer。
- provider
- IFormatProvider
物件,其提供關於 s
的特定文化特性格式資訊。
- result
- SByte
當這個方法傳回時,如果轉換成功便包含相對於 s
中所含數字的 8 位元帶正負號的整數,如果轉換失敗則為零。 如果 s
參數為 null
或 Empty,則轉換會失敗,且格式不符合 style
,或代表小於 SByte.MinValue 或大於 SByte.MaxValue 的數位。 這個參數未初始化便傳遞,result
中原始提供的任何值都將遭到覆寫。
傳回
如果 s
轉換成功,則為 true
,否則為 false
。
- 屬性
例外狀況
範例
下列範例會 TryParse(String, NumberStyles, IFormatProvider, SByte) 使用數個不同的字串和 NumberStyles 值來呼叫 方法。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "106";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-106";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "103.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "103.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E-01";
CallTryParse(numericString, styles);
numericString = "12E01";
CallTryParse(numericString, styles);
numericString = "C8";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x8C";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
sbyte number;
bool result = SByte.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 '106' to 106.
// Attempted conversion of '-106' failed.
// Converted '103.00' to 103.
// Attempted conversion of '103.72' failed.
// Converted '10E-01' to 1.
// Attempted conversion of '12E-01' failed.
// Converted '12E01' to 120.
// Converted 'C8' to -56.
// Attempted conversion of '0x8C' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) styles =
match SByte.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
let numericString = "106"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-106"
let styles = NumberStyles.None
callTryParse numericString styles
let numericString = "103.00"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "103.72"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "10E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "12E-01"
callTryParse numericString styles
let numericString = "12E01"
callTryParse numericString styles
let numericString = "C8"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x8C"
callTryParse numericString NumberStyles.HexNumber
0
// The example displays the following output:
// Converted '106' to 106.
// Attempted conversion of '-106' failed.
// Converted '103.00' to 103.
// Attempted conversion of '103.72' failed.
// Converted '10E-01' to 1.
// Attempted conversion of '12E-01' failed.
// Converted '12E01' to 120.
// Converted 'C8' to -56.
// Attempted conversion of '0x8C' failed.
Imports System.Globalization
Module StringParsing
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "106"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-106"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "103.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "103.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "12E-01"
CallTryParse(numericString, styles)
numericString = "12E01"
CallTryParse(numericString, styles)
numericString = "C8"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x8C"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As SByte
Dim result As Boolean = SByte.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 '106' to 106.
' Attempted conversion of '-106' failed.
' Converted '103.00' to 103.
' Attempted conversion of '103.72' failed.
' Converted '10E-01' to 1.
' Attempted conversion of '12E-01' failed.
' Converted '12E01' to 120.
' Converted 'C8' to -56.
' Attempted conversion of '0x8C' failed.
備註
方法 TryParse(String, NumberStyles, IFormatProvider, SByte) 就像 Parse(String, NumberStyles, IFormatProvider) 方法一樣,不同之處在於,如果轉換失敗,則不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 value
無效,而且無法成功剖析。
參數 style
會定義樣式專案 (,例如空格符或正負號) ,參數中 value
允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 style
, value
參數可能包含下列元素:
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
style
如果參數包含 AllowHexSpecifier,則 value
參數可能包含下列元素:
[ws]hexdigits[ws]
在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。
元素 | 描述 |
---|---|
ws | 選擇性空格符。 如果 包含 旗標,則會在 開頭value 出現空格符,如果包含 NumberStyles.AllowTrailingWhite 旗標,則會出現在 結尾style value 。NumberStyles.AllowLeadingWhitestyle |
$ | 特定文化特性的貨幣符號。 字串中的位置是由 CurrencyPositivePattern 參數的 方法provider 所GetFormat傳回之 對象的 屬性NumberFormatInfo所定義。 如果style 包含旗標,NumberStyles.AllowCurrencySymbol則可以在 中value 顯示貨幣符號。 |
簽署 | 選擇性符號。 如果 包含旗標,則符號可以出現在 value 開頭,如果包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 結尾style value 。NumberStyles.AllowLeadingSignstyle 如果包含 NumberStyles.AllowParentheses 旗標,則可以在 中使用value 括弧來表示負值style 。 |
數字 | 從 0 到 9 的數位序列。 |
, | 特定文化特性的群組分隔符。 如果包含旗標,則 指定provider 文化特性的群組分隔符可能會出現在 中value 。NumberStyles.AllowThousandsstyle |
. | 特定文化特性的小數點符號。 如果包含旗標,則 所指定provider 文化特性的小數點符號可能會出現在 中value 。NumberStyles.AllowDecimalPointstyle |
fractional_digits | 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時style ,小數位數才會出現在 中value 。 |
E | “e” 或 “E” 字元,表示該值是以指數 (科學) 表示法表示。 如果style 包含 NumberStyles.AllowExponent 旗標,參數value 可以代表指數表示法的數位。 |
exponential_digits | 從 0 到 9 的數位序列。 如果style 包含 NumberStyles.AllowExponent 旗標,參數value 可以代表指數表示法的數位。 |
hexdigits | 從 0 到 f 或 0 到 F 的十六進位數位序列。 |
注意
中任何終止的 NUL (U+0000) 字元 s
都會被剖析作業忽略,不論自變數的值 style
為何。
只有十進位數的字串 (對應至 NumberStyles.None 旗標) 一律會成功剖析。 此輸入字串中可能存在但不需要存在的其餘 NumberStyles 成員控制項專案。 下表指出個別 NumberStyles 成員如何影響 中 value
可能存在的專案。
非複合 NumberStyles 值 |
除了數位之外,值中允許的專案 |
---|---|
None | 僅限十進位數。 |
AllowDecimalPoint | 小數點 (。) 和 fractional_digits 專案。 不過, fractional_digits 只能包含一或多個 0 位數,否則方法會傳 false 回 。 |
AllowExponent | “e” 或 “E” 字元,表示指數表示法,以及 exponential_digits。 如果 value 表示指數表示法的數位,則不能有非零的小數部分。 |
AllowLeadingWhite | 開頭的 value ws 元素。 |
AllowTrailingWhite | 結尾處的 value ws 專案。 |
AllowLeadingSign | 數位之前的符號元素。 |
AllowTrailingSign | 數字後面的符號專案。 |
AllowParentheses | 以括弧括住數值形式的 sign 元素。 |
AllowThousands | 群組分隔符 (,) 專案。 |
AllowCurrencySymbol | 貨幣 ($) 專案。 |
Currency | 所有元素。 不過, value 不能以指數表示法表示十六進位數位或數位。 |
Float | 開頭或結尾的 value ws 元素,在 開頭value 為 符號,而小數點 (。) 符號。 參數 value 也可以使用指數表示法。 |
Number | ws、sign、group separator (、) 和小數點 (。) 元素。 |
Any | 所有元素。 不過, value 不能代表十六進位數位。 |
NumberStyles.AllowHexSpecifier如果使用 旗標,value
必須是十六進位值。 有效的十六進位數位為0-9、a-f和 A-F。 唯一可以存在的 style
旗標是 NumberStyles.AllowLeadingWhite 和 NumberStyles.AllowTrailingWhite。 NumberStyles (列舉具有復合樣式 ,HexNumber其中包含空格符旗標.)
注意
如果 value
是十六進位數位的字串表示法,則不能在前面加上任何裝飾 (,例如 0x
或 &h
) 將它區分為十六進位數位。 這會導致轉換失敗。
參數 provider
是實作 IFormatProvider 。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,提供 有關 格式 value
的文化特性特定資訊。 參數 provider
可以是下列任一項:
CultureInfo物件,表示提供格式資訊的文化特性。 其 GetFormat 方法會傳 NumberFormatInfo 回 物件,該物件會提供該文化特性的數值格式資訊。
物件 NumberFormatInfo ,提供數值格式資訊。 (其 實作 GetFormat 只會傳回本身。)
實作的 IFormatProvider自定義物件。 其 GetFormat 方法會具現化並傳回 NumberFormatInfo 提供格式化信息的物件。
如果 為 provider
null
,則會 NumberFormatInfo 使用目前文化特性的物件。
另請參閱
適用於
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應