共用方式為


Byte.TryParse 方法

定義

嘗試將數字的字串表示轉換為其 Byte 等價值,並回傳一個表示轉換是否成功的值。

多載

名稱 Description
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

嘗試將一串 UTF-8 字元解析成一個值。

TryParse(ReadOnlySpan<Char>, Byte)

嘗試將數字的張成表示轉換為對 Byte 應的數值,並回傳一個表示轉換是否成功的值。

TryParse(String, Byte)

嘗試將數字的字串表示轉換為其 Byte 等價值,並回傳一個表示轉換是否成功的值。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

嘗試將一串字元解析成一個數值。

TryParse(String, IFormatProvider, Byte)

嘗試將字串解析成一個值。

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

嘗試將一串 UTF-8 字元解析成一個值。

TryParse(ReadOnlySpan<Byte>, Byte)

嘗試將包含數字字串表示的 UTF-8 字元區間轉換為其 8 位元無符號整數對應值。

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

將特定風格及特定文化格式中的數字跨度表示轉換為其 Byte 等價版本。 回傳值表示轉換是否成功。

TryParse(String, NumberStyles, IFormatProvider, Byte)

將特定風格及文化特定格式中的數字字串表示轉換為其 Byte 等價值。 回傳值表示轉換是否成功。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

嘗試將一串 UTF-8 字元解析成一個值。

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

參數

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

result
Byte

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

傳回

trueutf8Text 成功解析;否則, false

適用於

TryParse(ReadOnlySpan<Char>, Byte)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

嘗試將數字的張成表示轉換為對 Byte 應的數值,並回傳一個表示轉換是否成功的值。

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

參數

s
ReadOnlySpan<Char>

一個包含要轉換數字字元的區間。

result
Byte

當此方法回傳時,包含 Byte 的值等同於轉換成功時所包含 s 的數字;若轉換失敗則為零。 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。

傳回

trues成功轉換;否則,。 false

適用於

TryParse(String, Byte)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

嘗試將數字的字串表示轉換為其 Byte 等價值,並回傳一個表示轉換是否成功的值。

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

參數

s
String

一個包含一個需要轉換的數字的字串。

result
Byte

當此方法回傳時,包含 Byte 的值等同於轉換成功時所包含 s 的數字;若轉換失敗則為零。 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。

傳回

trues成功轉換;否則,。 false

範例

以下範例以多種不同字串值呼叫該 TryParse(String, Byte) 方法。

using System;

public class ByteConversion
{
   public static void Main()
   {
      string[] byteStrings = { null, string.Empty, "1024",
                               "100.1", "100", "+100", "-100",
                               "000000000000000100", "00,100",
                               "   20   ", "FF", "0x1F" };

      foreach (var byteString in byteStrings)
      {
          CallTryParse(byteString);
      }
   }

   private static void CallTryParse(string stringToConvert)
   {
      byte byteValue;
      bool success = Byte.TryParse(stringToConvert, out byteValue);
      if (success)
      {
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      }
      else
      {
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert);
      }
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
open System

let callTryParse (stringToConvert: string) =
    match Byte.TryParse stringToConvert with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."

let byteStrings = 
    [ null; String.Empty; "1024"
      "100.1"; "100"; "+100"; "-100"
      "000000000000000100"; "00,100"
      "   20   "; "FF"; "0x1F" ]

for byteString in byteStrings do
    callTryParse byteString

// The example displays the following output to the console:
//       Attempted conversion of '' failed.
//       Attempted conversion of '' failed.
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Attempted conversion of '00,100' failed.
//       Converted '   20   ' to 20
//       Attempted conversion of 'FF' failed.
//       Attempted conversion of '0x1F' failed.
Module ByteConversion
   Public Sub Main()
      Dim byteStrings() As String = { Nothing, String.Empty, "1024", 
                                    "100.1", "100", "+100", "-100",
                                    "000000000000000100", "00,100",
                                    "   20   ", "FF", "0x1F"}

      For Each byteString As String In byteStrings
        CallTryParse(byteString)
      Next
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String)  
      Dim byteValue As Byte
      Dim success As Boolean = Byte.TryParse(stringToConvert, byteValue)
      If success Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert)
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '' failed.
'       Attempted conversion of '' failed.
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Attempted conversion of '00,100' failed.
'       Converted '   20   ' to 20
'       Attempted conversion of 'FF' failed.
'       Attempted conversion of '0x1F' failed.

備註

s參數格式不正確、為nullString.Empty為 ,或代表小MinValue於或大MaxValue於 的數值,則轉換失敗,方法會返回false

Byte.TryParse(String, Byte) 方法與 方法 Byte.Parse(String) 相似,但 TryParse(String, Byte) 若轉換失敗不會拋出例外。

s參數應為以下形式的數字的字串表示:

[ws][sign]digits[ws]

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

元素 說明
ws 選擇性的空格符。
簽署 一個可選的正號,依當前文化的特性所規定 NumberFormatInfo.PositiveSign
數字 一串從0到9的十進位數字序列。

s參數是透過 Integer style 來解釋的。 除了位元組值的十進位數字外,僅允許前置與後置空格及引號。 (若符號存在,則必須為正符號,否則方法會拋出 。 OverflowException)若要明確定義風格元素及可存在 s的文化特定格式資訊,請使用該 Byte.Parse(String, NumberStyles, IFormatProvider) 方法。

s參數是利用物件中的NumberFormatInfo格式資訊解析,針對當前文化。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo

此方法的超載 Byte.TryParse(String, Byte) 將參數中 s 的所有數字解讀為十進位數字。 要解析十六進位數的字串表示,呼叫 過 Byte.TryParse(String, NumberStyles, IFormatProvider, Byte) 載。

另請參閱

適用於

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

嘗試將一串字元解析成一個數值。

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

參數

s
ReadOnlySpan<Char>

要解析的字元範圍。

provider
IFormatProvider

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

result
Byte

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

傳回

trues 成功解析;否則, false

適用於

TryParse(String, IFormatProvider, Byte)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

嘗試將字串解析成一個值。

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

參數

s
String

要解析的字串。

provider
IFormatProvider

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

result
Byte

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

傳回

trues 成功解析;否則, false

適用於

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

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

嘗試將一串 UTF-8 字元解析成一個值。

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

參數

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

result
Byte

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

傳回

trueutf8Text 成功解析;否則, false

適用於

TryParse(ReadOnlySpan<Byte>, Byte)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

嘗試將包含數字字串表示的 UTF-8 字元區間轉換為其 8 位元無符號整數對應值。

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

參數

utf8Text
ReadOnlySpan<Byte>

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

result
Byte

當此方法回傳時,包含 8 位元無符號整數值,若轉換成功則等同於 中的 utf8Text 數字;若轉換失敗則為零。 此參數以未初始化的方式傳遞;結果中原本提供的值將被覆寫。

傳回

trueutf8Text成功轉換;否則,。 false

適用於

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

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

將特定風格及特定文化格式中的數字跨度表示轉換為其 Byte 等價版本。 回傳值表示轉換是否成功。

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

參數

s
ReadOnlySpan<Char>

一個包含要轉換數字字元的區間。 跨度是用風格 Integer 來詮釋的。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。 若 provider 為 , null則使用執行緒當前文化。

result
Byte

當此方法回傳時,包含 8 位元無符號整數值,若轉換成功則等同於 中的 s 數字;若轉換失敗則為零。 若s參數為nullEmpty或 ,格式不正確,或代表的數字小於 Byte.MinValue 或大於 Byte.MaxValue ,則轉換失敗。 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。

傳回

trues成功轉換;否則,。 false

適用於

TryParse(String, NumberStyles, IFormatProvider, Byte)

來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs
來源:
Byte.cs

將特定風格及文化特定格式中的數字字串表示轉換為其 Byte 等價值。 回傳值表示轉換是否成功。

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

參數

s
String

一個包含可轉換數字的字串。 字串的解譯方式由 所指定的 style樣式。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。 若 provider 為 , null則使用執行緒當前文化。

result
Byte

當此方法回傳時,包含 8 位元無符號整數值,若轉換成功則等同於 中的 s 數字;若轉換失敗則為零。 若s參數為nullEmpty或 ,格式不正確,或代表的數字小於 Byte.MinValue 或大於 Byte.MaxValue ,則轉換失敗。 此參數以未初始化的方式傳遞;原本輸入 result 的任何值都會被覆寫。

傳回

trues成功轉換;否則,。 false

例外狀況

style 不是一個 NumberStyles 數值。

-或-

style不是 和 HexNumber 的組合AllowHexSpecifier

範例

以下範例以多種不同字串值呼叫該 TryParse(String, NumberStyles, IFormatProvider, Byte) 方法。

using System;
using System.Globalization;

public class ByteConversion2
{
   public static void Main()
   {
      string byteString;
      NumberStyles styles;

      byteString = "1024";
      styles = NumberStyles.Integer;
      CallTryParse(byteString, styles);

      byteString = "100.1";
      styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
      CallTryParse(byteString, styles);

      byteString = "100.0";
      CallTryParse(byteString, styles);

      byteString = "+100";
      styles = NumberStyles.Integer | NumberStyles.AllowLeadingSign
               | NumberStyles.AllowTrailingSign;
      CallTryParse(byteString, styles);

      byteString = "-100";
      CallTryParse(byteString, styles);

      byteString = "000000000000000100";
      CallTryParse(byteString, styles);

      byteString = "00,100";
      styles = NumberStyles.Integer | NumberStyles.AllowThousands;
      CallTryParse(byteString, styles);

      byteString = "2E+3   ";
      styles = NumberStyles.Integer | NumberStyles.AllowExponent;
      CallTryParse(byteString, styles);

      byteString = "FF";
      styles = NumberStyles.HexNumber;
      CallTryParse(byteString, styles);

      byteString = "0x1F";
      CallTryParse(byteString, styles);
   }

   private static void CallTryParse(string stringToConvert, NumberStyles styles)
   {
      Byte byteValue;
      bool result = Byte.TryParse(stringToConvert, styles,
                                  null as IFormatProvider, out byteValue);
      if (result)
         Console.WriteLine("Converted '{0}' to {1}",
                        stringToConvert, byteValue);
      else
         Console.WriteLine("Attempted conversion of '{0}' failed.",
                           stringToConvert.ToString());
   }
}
// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
open System
open System.Globalization

let callTryParse (stringToConvert: string) (styles: NumberStyles) =
    match Byte.TryParse(stringToConvert, styles, null) with
    | true, byteValue ->
        printfn $"Converted '{stringToConvert}' to {byteValue}"
    | _ ->
        printfn $"Attempted conversion of '{stringToConvert}' failed."
                        
[<EntryPoint>]
let main _ =
    let byteString = "1024"
    let styles = NumberStyles.Integer
    callTryParse byteString styles

    let byteString = "100.1"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
    callTryParse byteString styles

    let byteString = "100.0"
    callTryParse byteString styles

    let byteString = "+100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign
    callTryParse byteString styles

    let byteString = "-100"
    callTryParse byteString styles

    let byteString = "000000000000000100"
    callTryParse byteString styles

    let byteString = "00,100"
    let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
    callTryParse byteString styles

    let byteString = "2E+3   "
    let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
    callTryParse byteString styles

    let byteString = "FF"
    let styles = NumberStyles.HexNumber
    callTryParse byteString styles

    let byteString = "0x1F"
    callTryParse byteString styles

    0

// The example displays the following output to the console:
//       Attempted conversion of '1024' failed.
//       Attempted conversion of '100.1' failed.
//       Converted '100.0' to 100
//       Converted '+100' to 100
//       Attempted conversion of '-100' failed.
//       Converted '000000000000000100' to 100
//       Converted '00,100' to 100
//       Attempted conversion of '2E+3   ' failed.
//       Converted 'FF' to 255
//       Attempted conversion of '0x1F' failed.
Imports System.Globalization

Module ByteConversion2
   Public Sub Main()
      Dim byteString As String 
      Dim styles As NumberStyles
      
      byteString = "1024"
      styles = NumberStyles.Integer
      CallTryParse(byteString, styles)
      
      byteString = "100.1"
      styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
      CallTryParse(byteString, styles)
      
      byteString = "100.0"
      CallTryParse(byteString, styles)
      
      byteString = "+100"
      styles = NumberStyles.Integer Or NumberStyles.AllowLeadingSign _
               Or NumberStyles.AllowTrailingSign
      CallTryParse(byteString, styles)
      
      byteString = "-100"
      CallTryParse(byteString, styles)
      
      byteString = "000000000000000100"
      CallTryParse(byteString, styles)
      
      byteString = "00,100"      
      styles = NumberStyles.Integer Or NumberStyles.AllowThousands
      CallTryParse(byteString, styles)
      
      byteString = "2E+3   "
      styles = NumberStyles.Integer Or NumberStyles.AllowExponent
      CallTryParse(byteString, styles)
      
      byteString = "FF"
      styles = NumberStyles.HexNumber
      CallTryParse(byteString, styles)
      
      byteString = "0x1F"
      CallTryParse(byteString, styles)
   End Sub
   
   Private Sub CallTryParse(stringToConvert As String, styles As NumberStyles)  
      Dim byteValue As Byte
      Dim result As Boolean = Byte.TryParse(stringToConvert, styles, Nothing, _
                                            byteValue)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}", _
                        stringToConvert, byteValue)
      Else
         If stringToConvert Is Nothing Then stringToConvert = ""
         Console.WriteLine("Attempted conversion of '{0}' failed.", _
                           stringToConvert.ToString())
      End If                        
   End Sub
End Module
' The example displays the following output to the console:
'       Attempted conversion of '1024' failed.
'       Attempted conversion of '100.1' failed.
'       Converted '100.0' to 100
'       Converted '+100' to 100
'       Attempted conversion of '-100' failed.
'       Converted '000000000000000100' to 100
'       Converted '00,100' to 100
'       Attempted conversion of '2E+3   ' failed.
'       Converted 'FF' to 255
'       Attempted conversion of '0x1F' failed.

備註

TryParse 方法與 方法 Parse 相同,但 TryParse 若轉換失敗,方法不會拋出例外。

s參數是利用該參數提供的provider物件格式資訊NumberFormatInfo進行解析。

樣式參數定義了允許 s 在解析操作中成功使用的樣式元素(如空白或正號)。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 根據 的 style值, s 參數可能包含以下元素:

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

或者,如果參數 style 包含 AllowHexSpecifier

[ws]六角數[ws]

方括號內的元素([ 和 ] )為可選。 下表說明每個元素。

元素 說明
ws 選擇性的空格符。 如果style包含NumberStyles.AllowLeadingWhite旗幟,則可在 的s開頭顯示空白;若風格包含NumberStyles.AllowTrailingWhite旗幟,則可在 的結尾出現空白。
$ 一種文化特定的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyPositivePattern參數方法NumberFormatInfo回傳的GetFormat物件性質provider所定義。 貨幣s符號可出現style在包含NumberStyles.AllowCurrencySymbol旗幟時。
簽署 一個可選的正面訊號。 (若 中s存在負號,則解析操作失敗。)標誌可以出現在 如果s包含旗幟的話開頭,或在包含旗幟的話結尾stylesNumberStyles.AllowTrailingSignNumberStyles.AllowLeadingSignstyle
數字 一串從0到9的數字序列。
. 一種文化特定的小數點符號。 provider若包含s旗幟,則可出現在 中styleNumberStyles.AllowDecimalPoint
fractional_digits 數字 0 出現一次或多次。 只有當 s 包含該旗幟時style,小數數字才能出現NumberStyles.AllowDecimalPoint在 中。
e e 或 E 字元,表示該值以指數符號表示。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。
六角位數 一串從0到f,或0到F的十六進位數字序列。

備註

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有十進位數字的字串(對應 NumberStyles.None 於樣式)總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不強制存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

非合成 NumberStyles 值 除了數字外,還允許在 s 中加入元素
NumberStyles.None 只用小數數字。
NumberStyles.AllowDecimalPoint 以及 以及 fractional_digits 元素。 然而, fractional_digits 必須只包含一個或多個 0 位數,否則方法會返回 false
NumberStyles.AllowExponent 參數 s 也可以使用指數符號。 如果 s 以指數符號表示一個數字,則必須代表資料類型範圍內 Byte 沒有非零分數成分的整數。
NumberStyles.AllowLeadingWhite 開頭sw 元素。
NumberStyles.AllowTrailingWhite 末尾s 元素。
NumberStyles.AllowLeadingSign 正號有時會出現在 數字之前。
NumberStyles.AllowTrailingSign 數字 後面可能會出現正號。
NumberStyles.AllowParentheses 雖然此標誌被支援,但若 中s有括號,該方法會回傳false
NumberStyles.AllowThousands 雖然群分隔符號可以出現在 s中,但前方只能有一個或多個 0 位數。
NumberStyles.AllowCurrencySymbol 元素 $

NumberStyles.AllowHexSpecifier 使用該旗標, s 必須為無前綴的十六進位值。 例如,「F3」能成功解析,但「0xF3」卻無法。 唯一可能存在於 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉採用複合數字風格, NumberStyles.HexNumber包含兩個空白旗。)

參數 provider 是一個 IFormatProvider 實作,例如 CultureInfo 物件或物件 NumberFormatInfo ,其 GetFormat 方法回傳一個 NumberFormatInfo 物件。 該 NumberFormatInfo 物件提供關於 格式 s的文化特定資訊。

另請參閱

適用於