共用方式為


Int64.TryParse 方法

定義

將數位的字串表示轉換為其相等的64位帶正負號整數。 傳回值表示轉換成功或失敗。

多載

TryParse(String, IFormatProvider, Int64)

嘗試將字串剖析成值。

TryParse(ReadOnlySpan<Char>, Int64)

將數位的範圍表示轉換為其相等的64位帶正負號整數。 傳回值表示轉換成功或失敗。

TryParse(String, Int64)

將數位的字串表示轉換為其相等的64位帶正負號整數。 傳回值表示轉換成功或失敗。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int64)

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

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int64)

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

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

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

TryParse(ReadOnlySpan<Byte>, Int64)

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

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

將指定樣式和特定文化特性格式的數位範圍表示轉換為其64位帶正負號的整數對等。 傳回值表示轉換成功或失敗。

TryParse(String, NumberStyles, IFormatProvider, Int64)

將指定樣式和特定文化特性格式之數位的字串表示轉換為其64位帶正負號的整數對等。 傳回值表示轉換成功或失敗。

TryParse(String, IFormatProvider, Int64)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

嘗試將字串剖析成值。

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

參數

s
String

要剖析的字串。

provider
IFormatProvider

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

result
Int64

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

傳回

如果已成功剖析 strue;否則,false

適用於

TryParse(ReadOnlySpan<Char>, Int64)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將數位的範圍表示轉換為其相等的64位帶正負號整數。 傳回值表示轉換成功或失敗。

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] long % result);
public static bool TryParse (ReadOnlySpan<char> s, out long result);
static member TryParse : ReadOnlySpan<char> * int64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Long) As Boolean

參數

s
ReadOnlySpan<Char>

範圍,包含表示要轉換之數位的字元。

result
Int64

當這個方法傳回時,如果轉換成功,則包含相當於 s中所含數位的 64 位帶正負號的整數值,如果轉換失敗,則為零。 如果 s 參數為 nullEmpty,則轉換會失敗,或代表小於 Int64.MinValue 或大於 Int64.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

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

適用於

TryParse(String, Int64)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將數位的字串表示轉換為其相等的64位帶正負號整數。 傳回值表示轉換成功或失敗。

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] long % result);
public static bool TryParse (string s, out long result);
public static bool TryParse (string? s, out long result);
static member TryParse : string * int64 -> bool
Public Shared Function TryParse (s As String, ByRef result As Long) As Boolean

參數

s
String

字串,包含要轉換的數位。

result
Int64

當這個方法傳回時,如果轉換成功,則包含相當於 s中所含數位的 64 位帶正負號的整數值,如果轉換失敗,則為零。 如果 s 參數為 nullEmpty,則轉換會失敗,或代表小於 Int64.MinValue 或大於 Int64.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

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

範例

下列範例會呼叫具有數個不同字串值的 Int64.TryParse(String, Int64) 方法。

using System;

public class StringParsing
{
   public static void Main()
   {
      TryToParse(null);
      TryToParse("160519");
      TryToParse("9432.0");
      TryToParse("16,667");
      TryToParse("   -322   ");
      TryToParse("+4302");
      TryToParse("(100);");
      TryToParse("01FA");
   }

   private static void TryToParse(string value)
   {
      bool success = Int64.TryParse(value, out long number);
      if (success)
      {
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      else
      {
         if (value == null) value = "";
         Console.WriteLine("Attempted conversion of '{0}' failed.", value);
      }
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Converted '160519' to 160519.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100);' failed.
//       Attempted conversion of '01FA' failed.
open System

let tryToParse (value: string) =
    match Int64.TryParse value with
    | true, number ->
        printfn $"Converted '{value}' to {number}."
    | _ ->
        let value =
            if isNull value then 
                ""
            else
                value
        printfn $"Attempted conversion of '{value}' failed."

tryToParse null
tryToParse "160519"
tryToParse "9432.0"
tryToParse "16,667"
tryToParse "   -322   "
tryToParse "+4302"
tryToParse "(100);"
tryToParse "01FA"


// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Converted '160519' to 160519.
//       Attempted conversion of '9432.0' failed.
//       Attempted conversion of '16,667' failed.
//       Converted '   -322   ' to -322.
//       Converted '+4302' to 4302.
//       Attempted conversion of '(100);' failed.
//       Attempted conversion of '01FA' failed.
Module StringParsing
   Public Sub Main()
      TryToParse(Nothing)
      TryToParse("160519")
      TryToParse("9432.0")
      TryToParse("16,667")
      TryToParse("   -322   ")
      TryToParse("+4302")
      TryToParse("(100)")
      TryToParse("01FA")
   End Sub
   
   Private Sub TryToParse(value As String)
      Dim number As Long
      Dim result As Boolean = Int64.TryParse(value, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Else
         If value Is Nothing Then value = "" 
         Console.WriteLine("Attempted conversion of '{0}' failed.", value)
      End If     
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '' failed.
'       Converted '160519' to 160519.
'       Attempted conversion of '9432.0' failed.
'       Attempted conversion of '16,667' failed.
'       Converted '   -322   ' to -322.
'       Converted '+4302' to 4302.
'       Attempted conversion of '(100)' failed.
'       Attempted conversion of '01FA' failed.

在這裡範例中,TryParse(String, Int64) 方法無法轉換的一些字串如下:

  • "9432.0". 轉換失敗,因為字串不能包含小數分隔符;它必須只包含整數位數。

  • "16,667". 轉換失敗,因為字串不能包含群組分隔符;它必須只包含整數位數。

  • "(100)". 轉換失敗,因為字串不能包含目前文化特性 NumberFormatInfo.NegativeSignNumberFormatInfo.NumberNegativePattern 屬性所定義的負號。

  • “01FA”。 轉換失敗,因為字串不能包含十六進位數位;它必須只包含十進位數。

備註

TryParse 方法就像 Parse 方法,但 TryParse 方法在轉換失敗時不會擲回例外狀況。 它不需要使用例外狀況處理來測試 FormatExceptions 無效且無法成功剖析。

s 參數包含一些表單:

[ws][sign]digits[ws]

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

元素 描述
ws 選擇性的空格符。
簽署 選擇性符號。
位數 範圍從 0 到 9 的數位序列。

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

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

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

另請參閱

適用於

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int64)

來源:
Int64.cs
來源:
Int64.cs

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

result
Int64

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

傳回

如果已成功剖析 utf8Texttrue;否則,false

適用於

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int64)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

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

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

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

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

result
Int64

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

傳回

如果已成功剖析 strue;否則,false

適用於

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

來源:
Int64.cs
來源:
Int64.cs

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

物件,提供與 utf8Text相關的特定文化特性格式資訊。

result
Int64

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

傳回

如果已成功剖析 utf8Texttrue;否則,false

適用於

TryParse(ReadOnlySpan<Byte>, Int64)

來源:
Int64.cs
來源:
Int64.cs

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

result
Int64

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

傳回

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

適用於

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

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將指定樣式和特定文化特性格式的數位範圍表示轉換為其64位帶正負號的整數對等。 傳回值表示轉換成功或失敗。

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

參數

s
ReadOnlySpan<Char>

範圍,包含表示要轉換之數位的字元。 範圍是使用 style所指定的樣式來解譯。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Integer

provider
IFormatProvider

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

result
Int64

當這個方法傳回時,如果轉換成功,則包含相當於 s中所含數位的 64 位帶正負號的整數值,如果轉換失敗,則為零。 如果 s 參數是 nullEmpty,則轉換會失敗,不符合 style的格式,或代表 小於 int64.MinValue 或大於 Int64.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

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

適用於

TryParse(String, NumberStyles, IFormatProvider, Int64)

來源:
Int64.cs
來源:
Int64.cs
來源:
Int64.cs

將指定樣式和特定文化特性格式之數位的字串表示轉換為其64位帶正負號的整數對等。 傳回值表示轉換成功或失敗。

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

參數

s
String

字串,包含要轉換的數位。 字串會使用 style所指定的樣式來解譯。

style
NumberStyles

列舉值的位元組合,表示 s中可以存在的樣式專案。 要指定的一般值是 Integer

provider
IFormatProvider

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

result
Int64

當這個方法傳回時,如果轉換成功,則包含相當於 s中所含數位的 64 位帶正負號的整數值,如果轉換失敗,則為零。 如果 s 參數是 nullEmpty,則轉換會失敗,不符合 style的格式,或代表 小於 int64.MinValue 或大於 Int64.MaxValue的數位。 這個參數會未初始化傳遞;任何原本在 result 中提供的值都會遭到覆寫。

傳回

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

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

範例

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

using System;
using System.Globalization;

public class StringParsing
{
   public static void Main()
   {
      string numericString;
      NumberStyles styles;

      numericString = "106779";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);

      numericString = "-30677";
      styles = NumberStyles.None;
      CallTryParse(numericString, styles);

      styles = NumberStyles.AllowLeadingSign;
      CallTryParse(numericString, styles);

      numericString = "301677-";
      CallTryParse(numericString, styles);

      styles = styles | NumberStyles.AllowTrailingSign;
      CallTryParse(numericString, styles);

      numericString = "$10634";
      styles = NumberStyles.Integer;
      CallTryParse(numericString, styles);

      styles = NumberStyles.Integer | NumberStyles.AllowCurrencySymbol;
      CallTryParse(numericString, styles);

      numericString = "10345.00";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "10345.72";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(numericString, styles);

      numericString = "22,593";
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(numericString, styles);

      numericString = "12E-01";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(numericString, styles);

      numericString = "12E03";
      CallTryParse(numericString, styles);

      numericString = "80c1";
      CallTryParse(numericString, NumberStyles.HexNumber);

      numericString = "0x80C1";
      CallTryParse(numericString, NumberStyles.HexNumber);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      CultureInfo provider;

      // If currency symbol is allowed, use en-US culture.
      if ((styles & NumberStyles.AllowCurrencySymbol) > 0)
         provider = new CultureInfo("en-US");
      else
         provider = CultureInfo.InvariantCulture;

      bool success = Int64.TryParse(stringToConvert, styles,
                                   provider, out long number);
      if (success)
         Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
      else
         Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
   }
}
// The example displays the following output to the console:
//       Converted '106779' to 106779.
//       Attempted conversion of '-30677' failed.
//       Converted '-30677' to -30677.
//       Attempted conversion of '301677-' failed.
//       Converted '301677-' to -301677.
//       Attempted conversion of '$10634' failed.
//       Converted '$10634' to 10634.
//       Converted '10345.00' to 10345.
//       Attempted conversion of '10345.72' failed.
//       Converted '22,593' to 22593.
//       Attempted conversion of '12E-01' failed.
//       Converted '12E03' to 12000.
//       Converted '80c1' to 32961.
//       Attempted conversion of '0x80C1' failed.
open System
open System.Globalization

let callTryParse (stringToConvert: string) styles =
    let provider =
        // If currency symbol is allowed, use en-US culture.
        if int (styles &&& NumberStyles.AllowCurrencySymbol) > 0 then
            CultureInfo "en-US"
        else
            CultureInfo.InvariantCulture

    match Int64.TryParse(stringToConvert, styles, provider) with
    | true, number ->
        printfn $"Converted '{stringToConvert}' to {number}."
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."


[<EntryPoint>]
let main _ =
    let numericString = "106779"
    let styles = NumberStyles.Integer
    callTryParse numericString styles

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

    let styles = NumberStyles.AllowLeadingSign
    callTryParse numericString styles

    let numericString = "301677-"
    callTryParse numericString styles

    let styles = styles ||| NumberStyles.AllowTrailingSign
    callTryParse numericString styles

    let numericString = "$10634"
    let styles = NumberStyles.Integer
    callTryParse numericString styles

    let styles = NumberStyles.Integer ||| NumberStyles.AllowCurrencySymbol
    callTryParse numericString styles

    let numericString = "10345.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 = "22,593"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
    callTryParse numericString styles

    let numericString = "12E-01"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse numericString styles

    let numericString = "12E03"
    callTryParse numericString styles

    let numericString = "80c1"
    callTryParse numericString NumberStyles.HexNumber

    let numericString = "0x80C1"
    callTryParse numericString NumberStyles.HexNumber

    0

// The example displays the following output to the console:
//       Converted '106779' to 106779.
//       Attempted conversion of '-30677' failed.
//       Converted '-30677' to -30677.
//       Attempted conversion of '301677-' failed.
//       Converted '301677-' to -301677.
//       Attempted conversion of '$10634' failed.
//       Converted '$10634' to 10634.
//       Converted '10345.00' to 10345.
//       Attempted conversion of '10345.72' failed.
//       Converted '22,593' to 22593.
//       Attempted conversion of '12E-01' failed.
//       Converted '12E03' to 12000.
//       Converted '80c1' to 32961.
//       Attempted conversion of '0x80C1' failed.
Imports System.Globalization

Module StringParsing
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "106779"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-30677"
      styles = NumberStyles.None
      CallTryParse(numericString, styles)
      
      styles = NumberStyles.AllowLeadingSign
      CallTryParse(numericString, styles)
      
      numericString = "301677-"
      CallTryParse(numericString, styles)
      
      styles = styles Or NumberStyles.AllowTrailingSign
      CallTryParse(numericString, styles)
      
      numericString = "$10634"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      styles = NumberStyles.Integer Or NumberStyles.AllowCurrencySymbol
      CallTryParse(numericString, styles)

      numericString = "10345.00"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)
      
      numericString = "10345.72"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(numericString, styles)

      numericString = "22,593" 
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(numericString, styles)
      
      numericString = "12E-01"
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(numericString, styles) 
          
      numericString = "12E03"
      CallTryParse(numericString, styles) 
      
      numericString = "80c1"
      CallTryParse(numericString, NumberStyles.HexNumber)
      
      numericString = "0x80C1"
      CallTryParse(numericString, NumberStyles.HexNumber)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
      Dim number As Long
      Dim provider As CultureInfo
      
      ' If currency symbol is allowed, use en-US culture.
      If CBool(styles And NumberStyles.AllowCurrencySymbol) Then
         provider = CultureInfo.CurrentCulture
      Else
         provider = New CultureInfo("en-US")
      End If
      
      Dim result As Boolean = Int64.TryParse(stringToConvert, styles, _
                                             provider, 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 '106779' to 106779.
'       Attempted conversion of '-30677' failed.
'       Converted '-30677' to -30677.
'       Attempted conversion of '301677-' failed.
'       Converted '301677-' to -301677.
'       Attempted conversion of '$10634' failed.
'       Converted '$10634' to 10634.
'       Converted '10345.00' to 10345.
'       Attempted conversion of '10345.72' failed.
'       Converted '22,593' to 22593.
'       Attempted conversion of '12E-01' failed.
'       Converted '12E03' to 12000.
'       Converted '80c1' to 32961.
'       Attempted conversion of '0x80C1' failed.

備註

TryParse 方法就像 Parse 方法,但 TryParse 方法在轉換失敗時不會擲回例外狀況。 它不需要在 s 無效且無法成功剖析的情況下,使用例外狀況處理來測試 FormatException

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

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

或者,如果 style 參數包含 NumberStyles.AllowHexSpecifier

[ws]hexdigits[ws]

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

元素 描述
ws 選擇性的空格符。 如果 style 包含 NumberStyles.AllowLeadingWhite 旗標,或 style 包含 NumberStyles.AllowTrailingWhite 旗標,則空格符會出現在 s 開頭,或 s 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由 provider 參數之 GetFormat 方法所傳回之 NumberFormatInfo 物件的 CurrencyPositivePattern 屬性所定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,貨幣符號可以出現在 s 中。
簽署 選擇性符號。 如果 style 包含 NumberStyles.AllowLeadingSignNumberStyles.AllowTrailingSign 旗標,則符號符號可能會出現在 s 中。
位數

fractional_digits

exponential_digits
從 0 到 9 的數位序列。 若為 fractional_digits,則只有數位0有效。
特定文化特性的千位分隔符。 如果 style 包含 NumberStyles.AllowThousands 旗標,provider 所指定文化特性的千位分隔符可能會出現在 s 中。
特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,provider 所指定之文化特性的小數點符號可能會出現在 s 中。
e 'e' 或 'E' 字元,表示值是以指數表示法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,s 參數就可以以指數表示法來表示數位。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

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

只有小數字數的字串(對應至 NumberStyles.None 旗標),一律會成功剖析。 其餘大部分 NumberStyles 成員控件元素,這些元素可能不是必須存在於此輸入字串中。 下表指出個別 NumberStyles 成員如何影響 s中可能出現的專案。

非複合 NumberStyles 值 除了數位以外,允許的 元素
NumberStyles.None 只有十進位數。
NumberStyles.AllowDecimalPoint 小數點 (. ) 和 fractional_digits 專案。 不過,fractional_digits 必須只包含一或多個 0 位數,否則方法會傳回 false
NumberStyles.AllowExponent s 參數也可以使用指數表示法。 s 參數必須代表 Int64 數據類型範圍內沒有非零分數位件的整數。
NumberStyles.AllowLeadingWhite s開頭的 ws ws 專案。
NumberStyles.AllowTrailingWhite s結尾的 ws 專案。
NumberStyles.AllowLeadingSign 位數之前,可能會出現符號。
NumberStyles.AllowTrailingSign 符號可以在位數之後出現。
NumberStyles.AllowParentheses 以括弧括住數值的括弧形式,符號 專案。
NumberStyles.AllowThousands 千位分隔符 ( ) 元素。
NumberStyles.AllowCurrencySymbol $ 專案。
NumberStyles.Currency 所有元素。 s 參數不能代表十六進位數或指數表示法的數位。
NumberStyles.Float ws 項目位於 s的開頭或結尾,符號s開頭,以及小數點 (. ) 符號。 s 參數也可以使用指數表示法。
NumberStyles.Number ws符號、千位分隔符()和小數點(.) 元素。
NumberStyles.Any 除了 s 以外的所有樣式都不能代表十六進位數。

如果使用 NumberStyles.AllowHexSpecifier 旗標,s 必須是不含前置詞的十六進位值。 例如,“C9AF3” 剖析成功,但 “0xC9AF3” 則不會。 style 中唯一可以存在的其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (NumberStyles 列舉具有復合樣式,NumberStyles.HexNumber,其中包含兩個空格符旗標。

provider 參數是 IFormatProvider 實作,例如 CultureInfo 物件或 NumberFormatInfo 物件,其 GetFormat 方法會傳回 NumberFormatInfo 物件。 NumberFormatInfo 物件提供有關 s格式的文化特性特定資訊。 如果 providernull,則會使用目前文化特性的 NumberFormatInfo 物件。

另請參閱

適用於