UInt32.TryParse 方法

定義

嘗試將數字的字串表示轉換為 32 位元不帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

多載

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt32)

嘗試將 UTF-8 字元的範圍剖析為值。

TryParse(ReadOnlySpan<Char>, UInt32)

嘗試將數字的範圍表示轉換為其對等 32 位元不帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

TryParse(String, UInt32)

嘗試將數字的字串表示轉換為 32 位元不帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt32)

嘗試將字元範圍剖析成值。

TryParse(String, IFormatProvider, UInt32)

嘗試將字串剖析成值。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt32)

嘗試將 UTF-8 字元的範圍剖析為值。

TryParse(ReadOnlySpan<Byte>, UInt32)

嘗試將包含數位字串表示的 UTF-8 字元範圍轉換為其對等的 32 位不帶正負號的整數。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, UInt32)

嘗試將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 32 位元不帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

TryParse(String, NumberStyles, IFormatProvider, UInt32)

嘗試將指定之樣式及特定文化特性格式資訊數字的字串表示,轉換為它的 32 位元不帶正負號的整數對應項。 傳回值,該值指出轉換成功或失敗。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs

嘗試將 UTF-8 字元的範圍剖析為值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result) = IUtf8SpanParsable<System::UInt32>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out uint result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * uint32 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As UInteger) As Boolean

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的 UTF-8 字元範圍。

provider
IFormatProvider

提供關於 utf8Text 之特定文化特性格式資訊的物件。

result
UInt32

傳回時,包含成功剖析 utf8Text 或失敗時未定義值的結果。

傳回

true 如果 utf8Text 已成功剖析,則為 ,否則為 false

適用於

TryParse(ReadOnlySpan<Char>, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
UInt32.cs

重要

此 API 不符合 CLS 規範。

嘗試將數字的範圍表示轉換為其對等 32 位元不帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::UInt32 % result);
public static bool TryParse (ReadOnlySpan<char> s, out uint result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, out uint result);
static member TryParse : ReadOnlySpan<char> * uint32 -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * uint32 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As UInteger) As Boolean

參數

s
ReadOnlySpan<Char>

範圍,其包含代表所要轉換數字的字元。

result
UInt32

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 32 位元不帶正負號整數,如果轉換失敗則為零。 如果 參數為 nullEmpty ,不是正確的格式,或代表小於UInt32.MinValue或大於UInt32.MaxValue的數位,則轉換會失敗。 s 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

屬性

適用於

TryParse(String, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
UInt32.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Int64.TryParse(String, Int64)

嘗試將數字的字串表示轉換為 32 位元不帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::UInt32 % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, out uint result);
public static bool TryParse (string? s, out uint result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, out uint result);
[<System.CLSCompliant(false)>]
static member TryParse : string * uint32 -> bool
static member TryParse : string * uint32 -> bool
Public Shared Function TryParse (s As String, ByRef result As UInteger) As Boolean

參數

s
String

字串,表示要轉換的數字。

result
UInt32

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 32 位元不帶正負號整數,如果轉換失敗則為零。 如果 參數為 nullEmpty ,不是正確的格式,或代表小於UInt32.MinValue或大於UInt32.MaxValue的數位,則轉換會失敗。 s 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

屬性

範例

下列範例會針對字串陣列中的每個專案呼叫 TryParse(String, UInt32) 方法一次。

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 就像 Parse 方法一樣,不同之處在于,如果轉換失敗,它不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 s 無效,而且無法成功剖析。

參數 s 應該是以下列形式表示十進位數的字串:

[ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。
簽署 選擇性符號。 有效的符號字元取決於 NumberFormatInfo.NegativeSign 目前文化特性的 和 NumberFormatInfo.PositiveSign 屬性。
數字 小數位數序列,範圍從 0 到 9。

參數 s 是使用 NumberStyles.Integer 樣式來解譯。 除了十進位數之外,只允許前置和尾端有前置符號的空格。 若要使用可以存在於 s 中的文化特性特定格式資訊明確定義樣式專案,請呼叫 TryParse(String, NumberStyles, IFormatProvider, UInt32) 方法。

注意

參數指定的 s 字串不能包含任何群組分隔符號或小數分隔符號,而且不能有小數部分。

參數 s 是使用物件中 NumberFormatInfo 目前系統文化特性的格式資訊進行剖析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo

這個多載會將 參數中的所有 s 數位解譯為十進位數。 若要剖析十六進位數位的字串標記法,請改為呼叫 TryParse(String, NumberStyles, IFormatProvider, UInt32) 多載。

另請參閱

適用於

TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
UInt32.cs

嘗試將字元範圍剖析成值。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result) = ISpanParsable<System::UInt32>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out uint result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * uint32 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As UInteger) As Boolean

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

提供關於 s 之特定文化特性格式資訊的物件。

result
UInt32

當這個方法傳回時,會包含成功剖析 s 的結果,或失敗時未定義的值。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

TryParse(String, IFormatProvider, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
UInt32.cs

嘗試將字串剖析成值。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result) = IParsable<System::UInt32>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out uint result);
static member TryParse : string * IFormatProvider * uint32 -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As UInteger) As Boolean

參數

s
String

要剖析的字串。

provider
IFormatProvider

提供關於 s 之特定文化特性格式資訊的物件。

result
UInt32

當這個方法傳回時,包含成功剖析 s 或失敗時未定義值的結果。

傳回

true 如果 s 已成功剖析,則為 ,否則為 false

適用於

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs

嘗試將 UTF-8 字元的範圍剖析為值。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result) = System::Numerics::INumberBase<System::UInt32>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out uint result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * uint32 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As UInteger) As Boolean

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的 UTF-8 字元範圍。

style
NumberStyles

數位樣式的位元組合,可以存在於 中 utf8Text

provider
IFormatProvider

提供關於 utf8Text 之特定文化特性格式資訊的物件。

result
UInt32

傳回時,包含成功剖析 utf8Text 或失敗時未定義值的結果。

傳回

true 如果 utf8Text 已成功剖析,則為 ,否則為 false

適用於

TryParse(ReadOnlySpan<Byte>, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs

嘗試將包含數位字串表示的 UTF-8 字元範圍轉換為其對等的 32 位不帶正負號的整數。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::UInt32 % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out uint result);
static member TryParse : ReadOnlySpan<byte> * uint32 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As UInteger) As Boolean

參數

utf8Text
ReadOnlySpan<Byte>

範圍,包含代表要轉換之數位的 UTF-8 字元。

result
UInt32

當這個方法傳回時,會包含等於轉換成功時所包含的 utf8Text 數位的 32 位不帶正負號的整數值,如果轉換失敗,則為零。 此參數會以未初始化的狀態來傳遞,並會覆寫任何原本在結果中提供的值。

傳回

如果 utf8Text 轉換成功,則為 true,否則為 false

適用於

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
UInt32.cs

重要

此 API 不符合 CLS 規範。

嘗試將數字的範圍表示 (使用指定樣式和特定文化特性格式) 轉換為其對等 32 位元不帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result);
public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result) = System::Numerics::INumberBase<System::UInt32>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out uint result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out uint result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out uint result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * uint32 -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * uint32 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As UInteger) As Boolean

參數

s
ReadOnlySpan<Char>

範圍,其包含代表所要轉換數字的字元。 此範圍使用 style 參數指定的樣式來解譯。

style
NumberStyles

列舉值的位元組合,其表示 s 所允許的格式。 一般會指定的值是 Integer

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。

result
UInt32

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 32 位元不帶正負號整數,如果轉換失敗則為零。 如果 s 參數為 nullEmpty ,且格式不符合 style ,或 表示小於UInt32.MinValue 或大於 UInt32.MaxValue的數位,則轉換會失敗。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

屬性

適用於

TryParse(String, NumberStyles, IFormatProvider, UInt32)

Source:
UInt32.cs
Source:
UInt32.cs
Source:
UInt32.cs

重要

此 API 不符合 CLS 規範。

符合 CLS 規範替代方案
System.Int64.TryParse(String, Int64)

嘗試將指定之樣式及特定文化特性格式資訊數字的字串表示,轉換為它的 32 位元不帶正負號的整數對應項。 傳回值,該值指出轉換成功或失敗。

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result);
public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt32 % result) = System::Numerics::INumberBase<System::UInt32>::TryParse;
[System.CLSCompliant(false)]
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out uint result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out uint result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out uint result);
[<System.CLSCompliant(false)>]
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * uint32 -> bool
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * uint32 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As UInteger) As Boolean

參數

s
String

字串,表示要轉換的數字。 這個字串使用 style 參數指定的樣式來解譯。

style
NumberStyles

列舉值的位元組合,其表示 s 所允許的格式。 一般會指定的值是 Integer

provider
IFormatProvider

物件,其提供關於 s 的特定文化特性格式資訊。

result
UInt32

如果轉換成功,這個方法會傳回包含與 s 中內含數字相等的 32 位元不帶正負號整數,如果轉換失敗則為零。 如果 s 參數為 nullEmpty ,且格式不符合 style ,或 表示小於UInt32.MinValue 或大於 UInt32.MaxValue的數位,則轉換會失敗。 這個參數未初始化便傳遞,result 中原始提供的任何值都將遭到覆寫。

傳回

如果 s 轉換成功,則為 true,否則為 false

屬性

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

範例

下列範例會使用數個不同的字串和 NumberStyles 值來呼叫 TryParse(String, NumberStyles, IFormatProvider, UInt32) 方法。

using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;
      
      numericString = "2106034";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);
      
      numericString = "-10603";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);
      
      numericString = "29103674.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);
      
      numericString = "10345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "41792210E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles); 
      
      numericString = "9112E-01";
      CallTryParse(numericString, styles);
          
      numericString = "312E01";
      CallTryParse(numericString, styles); 
      
      numericString = "FFC86DA1";
      CallTryParse(numericString, NumberStyles.HexNumber);
      
      numericString = "0x8F8C";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }
   
   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      uint number;
      bool result = UInt32.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 '2106034' to 2106034.
//       Attempted conversion of '-10603' failed.
//       Converted '29103674.00' to 29103674.
//       Attempted conversion of '10345.72' failed.
//       Converted '41792210E-01' to 4179221.
//       Attempted conversion of '9112E-01' failed.
//       Converted '312E01' to 3120.
//       Converted 'FFC86DA1' to 4291325345.
//       Attempted conversion of '0x8F8C' failed.
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
    match UInt32.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
    | true, number ->
        printfn $"Converted '{stringToConvert}' to {number}."
        printfn $"Attempted conversion of '{stringToConvert}' failed."
    | _ -> ()

do
    let numericString = "2106034"
    let styles = NumberStyles.Integer
    callTryParse numericString styles

    let numericString = "-10603"
    let styles = NumberStyles.None
    callTryParse numericString styles

    let numericString = "29103674.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 = "41792210E-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 = "FFC86DA1"
    callTryParse numericString NumberStyles.HexNumber

    let numericString = "0x8F8C"
    callTryParse numericString NumberStyles.HexNumber
   
// The example displays the following output:
//       Converted '2106034' to 2106034.
//       Attempted conversion of '-10603' failed.
//       Converted '29103674.00' to 29103674.
//       Attempted conversion of '10345.72' failed.
//       Converted '41792210E-01' to 4179221.
//       Attempted conversion of '9112E-01' failed.
//       Converted '312E01' to 3120.
//       Converted 'FFC86DA1' to 4291325345.
//       Attempted conversion of '0x8F8C' failed.
Imports System.Globalization

Module Example
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "2106034"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-10603"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)
      
      numericString = "29103674.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      
      numericString = "10345.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "41792210E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
      
      numericString = "9112E-01"
      CallTryParse(numericString, styles)
          
      numericString = "312E01"
      CallTryParse(numericString, styles) 
      
      numericString = "FFC86DA1"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x8F8C"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As UInteger
      Dim result As Boolean = UInt32.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 '2106034' to 2106034.
'       Attempted conversion of '-10603' failed.
'       Converted '29103674.00' to 29103674.
'       Attempted conversion of '10345.72' failed.
'       Converted '41792210E-01' to 4179221.
'       Attempted conversion of '9112E-01' failed.
'       Converted '312E01' to 3120.
'       Converted 'FFC86DA1' to 4291325345.
'       Attempted conversion of '0x8F8C' failed.

備註

方法 TryParse(String, NumberStyles, IFormatProvider, UInt32) 就像 Parse(String, NumberStyles, IFormatProvider) 方法一樣,不同之處在于,如果轉換失敗,它不會擲回例外狀況。 這個方法不需要使用例外狀況處理來測試 FormatException 是否 s 無效,且無法成功剖析。

參數 style 會定義樣式專案 (,例如空白字元或正負號) ,在參數中 s 允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 styles 參數可能包含下列元素:

[ws][ $ ][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]

方括弧中的專案 ([ 和 ]) 是選擇性專案。 或者,如果 style 參數包含 NumberStyles.AllowHexSpecifier ,參數 s 可能包含下列元素:

[ws]hexdigits[ws]

下表說明每個元素。

元素 描述
ws 選擇性空白字元。 如果 style 包含 旗標, NumberStyles.AllowLeadingWhite 則空白字元可以出現在 開頭 s ,如果包含 旗標, NumberStyles.AllowTrailingWhite 則會出現 style 在 結尾 s 處。
$ 特定文化特性的貨幣符號。 字串中的位置是由 CurrencyPositivePattern 參數的 方法 provider 所傳回之 物件的 屬性 NumberFormatInfoGetFormat 定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 貨幣符號就可以出現在 中 s
簽署 選擇性符號。 如果 style 包含 旗標, NumberStyles.AllowLeadingSign 則符號可以出現在 開頭 s ,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,它可能會出現在 結尾 s 。 如果 style 包含 NumberStyles.AllowParentheses 旗標,可以使用 s 括弧來表示負值。 不過,如果負號存在, s 則只能代表剖析作業成功的值零。
數字 從 0 到 9 的數位序列。
, 特定文化特性的群組分隔符號。 如果包含 旗標,則 所 provider 指定文化特性的群組分隔符號可以出現在 中 sNumberStyles.AllowThousandsstyle
. 特定文化特性的小數點符號。 如果包含 旗標,則 所 provider 指定文化特性的小數點符號可以出現在 中 sNumberStyles.AllowDecimalPointstyle
fractional_digits 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s
E 「e」 或 「E」 字元,表示值是以指數 (科學) 標記法表示。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent
exponential_digits 從 0 到 9 的數位序列。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論引數的值 style 為何,剖析作業都會忽略 中 s 任何終止的 NUL (U+0000) 字元。

只有十進位數的字串 (對應至 NumberStyles.None 旗標) 一律會成功剖析。 此輸入字串中可能存在但不需要存在的大部分其餘 NumberStyles 成員控制項專案。 下表指出個別 NumberStyles 成員如何影響 中 s 可能存在的專案。

非複合 NumberStyles 除了數位之外,值中允許的專案
None 僅限十進位數。
AllowDecimalPoint 小數點 () 和 fractional_digits 元素。 不過, fractional_digits 必須只包含一或多個 0 位數,否則方法會傳 false 回 。
AllowExponent 「e」 或 「E」 字元,表示指數標記法,以及 exponential_digits。 如果 s 表示指數標記法中的數位,它不能有非零的小數部分。
AllowLeadingWhite 開頭的 sws元素。
AllowTrailingWhite 結尾處的 sws元素。
AllowLeadingSign 數位之前的符號專案。
AllowTrailingSign 數位後面的符號專案。
AllowParentheses 以括弧括住零數值形式的 sign 元素。
AllowThousands 群組分隔符號 () 元素。
AllowCurrencySymbol 貨幣 ($) 專案。
Currency 所有元素。 不過, s 不能代表十六進位數或指數標記法中的數位。
Float 開頭或結尾的 sws元素,會在 開頭加上符號s ,小數點 () 符號。 參數 s 也可以使用指數標記法。
Number wssign、group 分隔符號 () 和小數點 () 元素。
Any 所有元素。 不過, s 不能代表十六進位數。

NumberStyles.AllowHexSpecifier如果使用旗標, s 則必須是十六進位值。 唯一可以出現在 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 具有複合樣式, HexNumber 其中包含空白字元旗標.)

注意

如果 s 是十六進位數位的字串標記法,則不能在前面加上任何裝飾 (,例如 0x&h) ,將它區分為十六進位數。 這會導致轉換失敗。

參數 provider 是實作 IFormatProvider 。 其 GetFormat 方法會 NumberFormatInfo 傳回 物件,這個物件提供 有關 格式 s 的文化特性特定資訊。 參數 provider 可以是下列任一項:

如果 providernull ,則會 NumberFormatInfo 使用目前文化特性的 物件。

另請參閱

適用於