Int16.TryParse 方法

定義

將數字的字串表示轉換成它的對等 16 位元帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

多載

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16)

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

TryParse(ReadOnlySpan<Char>, Int16)

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

TryParse(String, Int16)

將數字的字串表示轉換成它的對等 16 位元帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16)

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

TryParse(String, IFormatProvider, Int16)

嘗試將字串剖析成值。

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

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

TryParse(ReadOnlySpan<Byte>, Int16)

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

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

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

TryParse(String, NumberStyles, IFormatProvider, Int16)

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

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int16)

Source:
Int16.cs
Source:
Int16.cs

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
Int16

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

傳回

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

適用於

TryParse(ReadOnlySpan<Char>, Int16)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

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

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

參數

s
ReadOnlySpan<Char>

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

result
Int16

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

傳回

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

適用於

TryParse(String, Int16)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

將數字的字串表示轉換成它的對等 16 位元帶正負號的整數。 傳回值,該值指出轉換成功或失敗。

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

參數

s
String

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

result
Int16

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

傳回

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

範例

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

using System;

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

   private static void TryToParse(string value)
   {
      short number;
      bool result = Int16.TryParse(value, out number);
      if (result)
      {
         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 '16051' to 16051.
//       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 Int16.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 "16051"
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 '16051' to 16051.
//       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("16051")
      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 Int16
      Dim result As Boolean = Int16.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 '16051' to 16051.
'       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, Int16) 如下:

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

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

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

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

備註

方法 Int16.TryParse(String, Int16) 與 方法不同 Int16.Parse(String) 之處在于傳回布林值,指出剖析作業是否成功,而不是傳回剖析 Int16 的值。 它不需要使用例外狀況處理,在無效且無法成功剖析的 事件 s 中測試 FormatException

參數 s 應該是數位的字串表示,格式如下:

[ws][sign]digits[ws]

方括弧中的專案 ([ 和 ]) 是選擇性專案。 下表說明每個元素。

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

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

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

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

另請參閱

適用於

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int16)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

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

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

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

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

result
Int16

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

傳回

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

適用於

TryParse(String, IFormatProvider, Int16)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

嘗試將字串剖析成值。

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

參數

s
String

要剖析的字串。

provider
IFormatProvider

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

result
Int16

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

傳回

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

適用於

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

Source:
Int16.cs
Source:
Int16.cs

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Int16

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

傳回

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

適用於

TryParse(ReadOnlySpan<Byte>, Int16)

Source:
Int16.cs
Source:
Int16.cs

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

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

參數

utf8Text
ReadOnlySpan<Byte>

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

result
Int16

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

傳回

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

適用於

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

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

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

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

參數

s
ReadOnlySpan<Char>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Int16

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

傳回

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

適用於

TryParse(String, NumberStyles, IFormatProvider, Int16)

Source:
Int16.cs
Source:
Int16.cs
Source:
Int16.cs

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

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

參數

s
String

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Int16

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

傳回

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

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

範例

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

using System;
using System.Globalization;

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

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

      numericString = "-30677";
      styles = NumberStyles.None;
      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)
   {
      short number;
      bool result = Int16.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 to the console:
//       Converted '10677' to 10677.
//       Attempted conversion of '-30677' failed.
//       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 -32575.
//       Attempted conversion of '0x80C1' failed.
open System
open System.Globalization

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

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

    let numericString = "-30677"
    let styles = NumberStyles.None
    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 '10677' to 10677.
//       Attempted conversion of '-30677' failed.
//       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 -32575.
//       Attempted conversion of '0x80C1' failed.
Imports System.Globalization

Module StringParsing
   Public Sub Main()
      Dim numericString As String
      Dim styles As NumberStyles
      
      numericString = "10677"
      styles = NumberStyles.Integer
      CallTryParse(numericString, styles)
      
      numericString = "-30677"
      styles = NumberStyles.None
      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 Short
      Dim result As Boolean = Int16.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 '10677' to 10677.
'       Attempted conversion of '-30677' failed.
'       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 -32575.
'       Attempted conversion of '0x80C1' failed.

備註

方法 Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) 與 方法不同 Int16.Parse(String, NumberStyles, IFormatProvider) ,方法是傳回布林值,指出剖析作業是否成功,而不是傳回剖 Int16 析的值。 它不需要使用例外狀況處理來測試 FormatException 事件 s 中無效且無法成功剖析的 。

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

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

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

[ws]hexdigits[ws]

方括弧中的專案 ([ 和 ]) 是選擇性專案。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。 如果包含 旗標,則空白字元可以出現在 的 s 開頭,如果包含 旗標, NumberStyles.AllowTrailingWhite 則會出現 style 在 結尾 sNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 字串中的位置是由 CurrencyPositivePattern 參數的 方法 provider 所傳回之 物件的 屬性 NumberFormatInfoGetFormat 定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 貨幣符號就可以出現在 中 s
簽署 選擇性符號。
數字 從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符號。 如果包含 旗標,則 所 provider 指定文化特性的千位分隔符號可以出現在 中 sNumberStyles.AllowThousandsstyle
. 特定文化特性的小數點符號。 如果包含 旗標,則 所 provider 指定文化特性的小數點符號可以出現在 中 sNumberStyles.AllowDecimalPointstyle
fractional_digits 數位 0 的一或多個出現次數。 只有包含 NumberStyles.AllowDecimalPoint 旗標時 style ,小數位數才會出現在 中 s
e 'e' 或 'E' 字元,表示值是以指數標記法表示。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

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

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

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

NumberStyles.AllowHexSpecifier如果使用旗標, s 必須是不含前置詞的十六進位值。 例如,「9AF3」 會成功剖析,但 「0x9AF3」 則不會。 唯一可以出現在 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 具有複合樣式, NumberStyles.HexNumber 其中包含兩個空白字元旗標.)

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

另請參閱

適用於