Byte.TryParse 方法

定義

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

多載

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

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

傳回

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

適用於

TryParse(ReadOnlySpan<Char>, Byte)

來源:
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 中原始提供的任何值都將遭到覆寫。

傳回

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

適用於

TryParse(String, Byte)

來源:
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 中原始提供的任何值都將遭到覆寫。

傳回

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

範例

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

using namespace System;

void main()
{
   array<String^>^ byteStrings = gcnew array<String^> { nullptr, String::Empty, 
                                                        "1024", "100.1", "100", 
                                                        "+100", "-100", "000000000000000100", 
                                                        "00,100", "   20   ", "FF", "0x1F" };
   Byte byteValue;
   for each (String^ byteString in byteStrings) {
      bool result = Byte::TryParse(byteString, byteValue);
      if (result)
         Console::WriteLine("Converted '{0}' to {1}", 
                            byteString, byteValue);
      else
         Console::WriteLine("Attempted conversion of '{0}' failed.", 
                            byteString);
   }
}
// The example displays the following output:
//       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.}
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 樣式來解譯。 除了位元組值的十進位數之外,只允許前置和尾端空格與前置符號。 (如果符號存在,則必須是正負號,否則方法會擲回 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

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

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

傳回

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

適用於

TryParse(String, IFormatProvider, Byte)

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

傳回

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

適用於

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

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

傳回

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

適用於

TryParse(ReadOnlySpan<Byte>, Byte)

來源:
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

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

傳回

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

適用於

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

來源:
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);
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, 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 的特定文化特性格式資訊。 如果 providernull,則會使用執行緒目前的文化特性。

result
Byte

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

傳回

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

適用於

TryParse(String, NumberStyles, IFormatProvider, Byte)

來源:
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 的特定文化特性格式資訊。 如果 providernull,則會使用執行緒目前的文化特性。

result
Byte

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

傳回

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

例外狀況

style 不是 NumberStyles 值。

-或-

style 不是 AllowHexSpecifierHexNumber 值的組合。

範例

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

using namespace System;
using namespace System::Globalization;

void CallTryParse(String^ byteString, NumberStyles styles);

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);
}

void CallTryParse(String^ stringToConvert, NumberStyles styles)
{  
   Byte byteValue;
   bool result = Byte::TryParse(stringToConvert, styles, 
                                 (IFormatProvider^) nullptr , byteValue);
   if (result)
      Console::WriteLine("Converted '{0}' to {1}", 
                     stringToConvert, byteValue);
   else
      Console::WriteLine("Attempted conversion of '{0}' failed.", 
                        stringToConvert);
}
// The example displays the following output:
//       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.}
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 格式資訊進行剖析。

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

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

或者,如果 style 參數包含 AllowHexSpecifier

[ws]hexdigits[ws]

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

元素 描述
ws 選擇性空白字元。 如果 style 包含 旗標, NumberStyles.AllowLeadingWhite 則空白字元可以出現在 的 s 開頭,如果樣式包含 旗標, NumberStyles.AllowTrailingWhite 則會出現在 結尾。
$ 特定文化特性的貨幣符號。 字串中的位置是由 NumberFormatInfo.CurrencyPositivePattern 參數的 方法 provider 所傳回之 物件的 屬性 NumberFormatInfoGetFormat 定義。 如果 style 包含 旗標, NumberStyles.AllowCurrencySymbol 貨幣符號就可以出現在 中 s
簽署 選擇性正負號。 (如果 .) 中存在負號, s 剖析作業就會失敗。如果 style 包含 旗標,則此符號可以出現在 的開頭 s ,或如果 style 包含 NumberStyles.AllowLeadingSignNumberStyles.AllowTrailingSign 旗標則出現在 結尾 s
數字 從 0 到 9 的數位序列。
. 特定文化特性的小數點符號。 如果包含 旗標,則 所 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 以指數標記法標記法表示數位,則必須在資料類型範圍內 Byte 代表整數,而不使用非零的小數部分。
NumberStyles.AllowLeadingWhite 開頭的 sws元素。
NumberStyles.AllowTrailingWhite 結尾處的 sws元素。
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 其中包含兩個空白字元旗標。)

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

另請參閱

適用於