共用方式為


UInt16.TryParse 方法

定義

將數位的字串表示轉換為其相等的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 或失敗時未定義值的結果。

傳回

如果已成功剖析 utf8Texttrue;否則,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 參數是 nullEmpty,則轉換會失敗,格式不正確。 或表示小於 UInt16.MinValue 或大於 UInt16.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

如果成功轉換 strue;否則,false

屬性

適用於

TryParse(String, UInt16)

來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs

重要

此 API 不符合 CLS 規範。

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

嘗試將數位的字串表示轉換成其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位無符號整數值,如果轉換失敗,則為零。 如果 s 參數為 nullEmpty,則轉換會失敗,或表示小於 UInt16.MinValue 或大於 UInt16.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

如果成功轉換 strue;否則,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) 方法,不同之處在於,如果轉換失敗,則不會擲回例外狀況。 如果 s 無效且無法成功剖析,此方法就不需要使用例外狀況處理來測試 FormatException

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

[ws][符號]數位[ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。
簽署 選擇性符號。 有效的符號字元取決於目前文化特性的 NumberFormatInfo.NegativeSignNumberFormatInfo.PositiveSign 屬性。
位數 從 0 到 9 的十進位數序列。

注意

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

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

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

此多載會將 s 參數中的所有數位解譯為十進位數。 若要剖析十六進位數位的字串表示,請改為呼叫 TryParse(String, NumberStyles, IFormatProvider, UInt16) 多載。

另請參閱

適用於

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的結果,或失敗時未定義的值。

傳回

如果已成功剖析 strue;否則,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 或失敗時未定義值的結果。

傳回

如果已成功剖析 strue;否則,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 或失敗時未定義值的結果。

傳回

如果已成功剖析 utf8Texttrue;否則,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

當這個方法傳回時,會包含16位無符號整數值,相當於轉換成功時,utf8Text 中包含的數位,如果轉換失敗則為零。 這個參數會未初始化傳遞;將覆寫原本在結果中提供的任何值。

傳回

如果成功轉換 utf8Texttrue;否則,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 參數是 nullEmpty,則轉換會失敗,不符合 style的格式,或代表小於 UInt16.MinValue 或大於 UInt16.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

如果成功轉換 strue;否則,false

屬性

適用於

TryParse(String, NumberStyles, IFormatProvider, UInt16)

來源:
UInt16.cs
來源:
UInt16.cs
來源:
UInt16.cs

重要

此 API 不符合 CLS 規範。

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

嘗試將指定樣式和特定文化特性格式之數位的字串表示轉換成其 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 參數是 nullEmpty,則轉換會失敗,不符合 style的格式,或代表小於 UInt16.MinValue 或大於 UInt16.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

如果成功轉換 strue;否則,false

屬性

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

範例

下列範例會使用數個不同的字串和 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) 方法,不同之處在於,如果轉換失敗,則不會擲回例外狀況。 如果 s 無效且無法成功剖析,此方法就不需要使用例外狀況處理來測試 FormatException

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

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

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

[ws]hexdigits[ws]

下表描述每個元素。

ws 選擇性空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,sstyle 包含 NumberStyles.AllowTrailingWhite 旗標,則空格符會顯示在 s 開頭。

$ 特定文化特性貨幣符號。 字串中的位置是由 provider 參數之 GetFormat 方法所傳回之 NumberFormatInfo 物件的 CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,貨幣符號可以出現在 s 中。

符號 選擇性符號。 如果 style 包含 NumberStyles.AllowLeadingSign 旗標,則符號會顯示在 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則會出現在 s 結尾。 括弧可用於 s,如果 style 包含 NumberStyles.AllowParentheses 旗標,則表示負值。 不過,如果負號存在,s 只能代表剖析作業成功的值零。

位數 0 到 9 的數位序列。

特定文化特性的群組分隔符。 如果 style 包含 NumberStyles.AllowThousands 旗標,provider 所指定文化特性的群組分隔符可能會出現在 s 中。

. 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,provider 所指定之文化特性的小數點符號可能會出現在 s 中。

fractional_digits 一個或多個數位0的出現次數。 只有 style 包含 NumberStyles.AllowDecimalPoint 旗標時,小數位數才會出現在 s 中。

E “e” 或 “E” 字元,表示值是以指數(科學)表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。

exponential_digits 0 到 9 的數位序列。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。

十六進位數 從 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 s開頭的 ws 專案。
AllowTrailingWhite s結尾的 ws 專案。
AllowLeadingSign 符號 元素,位數
AllowTrailingSign 數字之後,符號 專案。
AllowParentheses 以括弧括住零數值的括弧形式,符號 專案。
AllowThousands 群組分隔符 () 元素。
AllowCurrencySymbol currency ($) 元素。
Currency 所有元素。 不過,s 不能代表十六進位數或指數表示法的數位。
Float ws 項目位於 s的開頭或結尾,符號s開頭,以及小數點 (.) 符號。 s 參數也可以使用指數表示法。
Number ws符號、群組分隔符()和小數點(.) 元素。
Any 所有元素。 不過,s 不能代表十六進位數。

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是十六進位值。 有效的十六進位數位是 0 到 9、a 到 f 和 A 到 F。不支援 「0x」 之類的前置詞,並導致剖析作業失敗。 style 中唯一可以存在的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 列舉具有包含兩個空格符旗標的複合樣式 HexNumber

注意

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

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

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

另請參閱

適用於