Byte.TryParse Metoda

Definice

Pokusí se převést řetězcovou reprezentaci čísla na jeho Byte ekvivalent a vrátí hodnotu, která označuje, zda byl převod úspěšný.

Přetížení

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

Pokusí se parsovat rozsah znaků UTF-8 na hodnotu.

TryParse(ReadOnlySpan<Char>, Byte)

Pokusí se převést reprezentaci rozsahu čísla na jeho Byte ekvivalent a vrátí hodnotu, která označuje, zda byl převod úspěšný.

TryParse(String, Byte)

Pokusí se převést řetězcovou reprezentaci čísla na jeho Byte ekvivalent a vrátí hodnotu, která označuje, zda byl převod úspěšný.

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

Pokusí se parsovat rozsah znaků na hodnotu.

TryParse(String, IFormatProvider, Byte)

Pokusí se parsovat řetězec na hodnotu.

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

Pokusí se parsovat rozsah znaků UTF-8 na hodnotu.

TryParse(ReadOnlySpan<Byte>, Byte)

Pokusí se převést rozsah znaků UTF-8 obsahující řetězcovou reprezentaci čísla na jeho 8bitový celočíselný ekvivalent bez znaménka.

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

Převede reprezentaci rozsahu čísla v zadaném stylu a formátu specifickém pro jazykovou verzi na jeho Byte ekvivalent. Vrácená hodnota označuje, zda byl převod úspěšný, či nikoli.

TryParse(String, NumberStyles, IFormatProvider, Byte)

Převede řetězcovou reprezentaci čísla v zadaném stylu a formátu specifickém pro jazykovou verzi na jeho Byte ekvivalent. Vrácená hodnota označuje, zda byl převod úspěšný, či nikoli.

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs

Pokusí se parsovat rozsah znaků UTF-8 na hodnotu.

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

Parametry

utf8Text
ReadOnlySpan<Byte>

Rozsah znaků UTF-8, které se mají analyzovat.

provider
IFormatProvider

Objekt, který poskytuje informace o formátování specifické pro jazykovou verzi .utf8Text

result
Byte

Při vrácení obsahuje výsledek úspěšné analýzy utf8Text nebo nedefinovanou hodnotu při selhání.

Návraty

truepokud utf8Text byl úspěšně parsován, v opačném případě . false

Platí pro

TryParse(ReadOnlySpan<Char>, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Pokusí se převést reprezentaci rozsahu čísla na jeho Byte ekvivalent a vrátí hodnotu, která označuje, zda byl převod úspěšný.

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

Parametry

s
ReadOnlySpan<Char>

Rozsah obsahující znaky představující číslo, které se má převést.

result
Byte

Když se tato metoda vrátí, obsahuje Byte hodnotu, která odpovídá číslu obsaženému v s , pokud byl převod úspěšný, nebo nula, pokud převod selhal. Tento parametr je předán neinicializován; jakákoli hodnota původně zadaná v result se přepíše.

Návraty

true pokud s byl převod úspěšně, v opačném případě false.

Platí pro

TryParse(String, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Pokusí se převést řetězcovou reprezentaci čísla na jeho Byte ekvivalent a vrátí hodnotu, která označuje, zda byl převod úspěšný.

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

Parametry

s
String

Řetězec obsahující číslo k převedení.

result
Byte

Když se tato metoda vrátí, obsahuje Byte hodnotu, která odpovídá číslu obsaženému v s , pokud byl převod úspěšný, nebo nula, pokud převod selhal. Tento parametr je předán neinicializován; jakákoli hodnota původně zadaná v result se přepíše.

Návraty

true pokud s byl převod úspěšně, v opačném případě false.

Příklady

Následující příklad volá metodu TryParse(String, Byte) s počtem různých řetězcových hodnot.

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.

Poznámky

Převod se nezdaří a metoda vrátí false , pokud s parametr není ve správném formátu, pokud je null nebo String.Empty, nebo pokud představuje číslo menší než MinValue nebo větší než MaxValue.

Metoda Byte.TryParse(String, Byte) je podobná Byte.Parse(String) metodě s tím rozdílem, že TryParse(String, Byte) nevyvolá výjimku, pokud převod selže.

Parametr s by měl být řetězcovou reprezentací čísla v následujícím tvaru:

[ws][sign]digits[ws]

Prvky v hranatých závorkách ([ a ]) jsou volitelné. Následující tabulka popisuje jednotlivé prvky.

Element Popis
Ws Volitelné prázdné místo.
sign Volitelné kladné znaménko podle vlastnosti NumberFormatInfo.PositiveSign aktuální jazykové verze.
Číslic Posloupnost desetinných číslic v rozsahu od 0 do 9.

Parametr s se interpretuje pomocí Integer stylu. Kromě desetinných číslic v bajtové hodnotě jsou povoleny pouze počáteční a koncové mezery společně s počátečním znaménkem. (Pokud znaménko existuje, musí to být kladné znaménko, jinak metoda vyvolá OverflowException.) Chcete-li explicitně definovat prvky stylu spolu s informacemi o formátování specifické pro jazykovou verzi, které mohou být přítomny v s, použijte metodu Byte.Parse(String, NumberStyles, IFormatProvider) .

Parametr s se analyzuje pomocí informací o formátování v objektu NumberFormatInfo pro aktuální jazykovou verzi. Další informace naleznete v tématu NumberFormatInfo.CurrentInfo.

Toto přetížení Byte.TryParse(String, Byte) metody interpretuje všechny číslice v parametru s jako desetinné číslice. Pokud chcete parsovat řetězcovou reprezentaci šestnáctkového čísla, zavolejte Byte.TryParse(String, NumberStyles, IFormatProvider, Byte) přetížení.

Viz také

Platí pro

TryParse(ReadOnlySpan<Char>, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Pokusí se parsovat rozsah znaků na hodnotu.

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

Parametry

s
ReadOnlySpan<Char>

Rozsah znaků, které se mají analyzovat.

provider
IFormatProvider

Objekt, který poskytuje informace o formátování specifické pro jazykovou verzi .s

result
Byte

Když se tato metoda vrátí, obsahuje výsledek úspěšné analýzy snebo nedefinovanou hodnotu při selhání.

Návraty

truepokud s byl úspěšně parsován, v opačném případě . false

Platí pro

TryParse(String, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Pokusí se parsovat řetězec na hodnotu.

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

Parametry

s
String

Řetězec, který chcete analyzovat.

provider
IFormatProvider

Objekt, který poskytuje informace o formátování specifické pro jazykovou verzi .s

result
Byte

Když se tato metoda vrátí, obsahuje výsledek úspěšné analýzy s nebo nedefinovanou hodnotu při selhání.

Návraty

truepokud s byl úspěšně parsován, v opačném případě . false

Platí pro

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

Source:
Byte.cs
Source:
Byte.cs

Pokusí se parsovat rozsah znaků UTF-8 na hodnotu.

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

Parametry

utf8Text
ReadOnlySpan<Byte>

Rozsah znaků UTF-8, které se mají analyzovat.

style
NumberStyles

Bitové kombinace stylů čísel, které se můžou vyskytovat v nástroji utf8Text.

provider
IFormatProvider

Objekt, který poskytuje informace o formátování specifické pro jazykovou verzi .utf8Text

result
Byte

Při vrácení obsahuje výsledek úspěšné analýzy utf8Text nebo nedefinovanou hodnotu při selhání.

Návraty

truepokud utf8Text byl úspěšně parsován, v opačném případě . false

Platí pro

TryParse(ReadOnlySpan<Byte>, Byte)

Source:
Byte.cs
Source:
Byte.cs

Pokusí se převést rozsah znaků UTF-8 obsahující řetězcovou reprezentaci čísla na jeho 8bitový celočíselný ekvivalent bez znaménka.

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

Parametry

utf8Text
ReadOnlySpan<Byte>

Rozsah obsahující znaky UTF-8 představující číslo, které se má převést.

result
Byte

Když tato metoda vrátí, obsahuje 8bitovou celočíselnou hodnotu bez znaménka, která odpovídá číslu obsaženému v případě úspěšného převodu, nebo nula, utf8Text pokud převod selhal. Tento parametr je předán neinicializován; jakákoli hodnota původně zadaná ve výsledku bude přepsána.

Návraty

true pokud utf8Text byl převod úspěšně, v opačném případě false.

Platí pro

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

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Převede reprezentaci rozsahu čísla v zadaném stylu a formátu specifickém pro jazykovou verzi na jeho Byte ekvivalent. Vrácená hodnota označuje, zda byl převod úspěšný, či nikoli.

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

Parametry

s
ReadOnlySpan<Char>

Rozsah obsahující znaky představující číslo, které se má převést. Rozsah se interpretuje pomocí Integer stylu.

style
NumberStyles

Bitové kombinace hodnot výčtu, která označuje prvky stylu, které mohou být přítomny v s. Typická hodnota, která se má zadat, je Integer.

provider
IFormatProvider

Objekt, který poskytuje informace o formátování specifické pro jazykovou verzi .s Pokud provider je null, použije se aktuální jazyková verze vlákna.

result
Byte

Když tato metoda vrátí, obsahuje 8bitovou celočíselnou hodnotu bez znaménka, která odpovídá číslu obsaženému v případě úspěšného převodu, nebo nula, s pokud převod selhal. Převod selže, pokud s parametr je null nebo Empty, nemá správný formát nebo představuje číslo menší než Byte.MinValue nebo větší než Byte.MaxValue. Tento parametr je předán neinicializován; jakákoli hodnota původně zadaná v result se přepíše.

Návraty

true pokud s byl převod úspěšně, v opačném případě false.

Platí pro

TryParse(String, NumberStyles, IFormatProvider, Byte)

Source:
Byte.cs
Source:
Byte.cs
Source:
Byte.cs

Převede řetězcovou reprezentaci čísla v zadaném stylu a formátu specifickém pro jazykovou verzi na jeho Byte ekvivalent. Vrácená hodnota označuje, zda byl převod úspěšný, či nikoli.

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

Parametry

s
String

Řetězec obsahující číslo k převedení. Řetězec se interpretuje pomocí stylu určeného nástrojem style.

style
NumberStyles

Bitové kombinace hodnot výčtu, která označuje prvky stylu, které mohou být přítomny v s. Typická hodnota, která se má zadat, je Integer.

provider
IFormatProvider

Objekt, který poskytuje informace o formátování specifické pro jazykovou verzi .s Pokud provider je null, použije se aktuální jazyková verze vlákna.

result
Byte

Když tato metoda vrátí, obsahuje 8bitovou celočíselnou hodnotu bez znaménka, která odpovídá číslu obsaženému v případě úspěšného převodu, nebo nula, s pokud převod selhal. Převod selže, pokud s parametr je null nebo Empty, nemá správný formát nebo představuje číslo menší než Byte.MinValue nebo větší než Byte.MaxValue. Tento parametr je předán neinicializován; jakákoli hodnota původně zadaná v result se přepíše.

Návraty

true pokud s byl převod úspěšně, v opačném případě false.

Výjimky

style není NumberStyles hodnota.

-nebo-

style není kombinací AllowHexSpecifier hodnot a HexNumber .

Příklady

Následující příklad volá metodu TryParse(String, NumberStyles, IFormatProvider, Byte) s počtem různých řetězcových hodnot.

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.

Poznámky

Metoda TryParse je podobná Parse metodě s tím rozdílem, že TryParse metoda nevyvolá výjimku, pokud převod selže.

Parametr se s analyzuje pomocí informací o formátování v objektu NumberFormatInfo zadaném parametrem provider .

Parametr style definuje prvky stylu (například prázdné znaky nebo kladné znaménko), které jsou povoleny v parametru s pro úspěšnou operaci parse. Musí se jednat o kombinaci bitových příznaků z výčtu NumberStyles . V závislosti na hodnotě styles může parametr obsahovat následující prvky:

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

Nebo pokud parametr style obsahuje AllowHexSpecifier:

[ws]hexdigits[ws]

Prvky v hranatých závorkách ( [ a ] ) jsou volitelné. Následující tabulka popisuje jednotlivé prvky.

Element Popis
Ws Volitelné prázdné místo. Prázdné znaky se můžou zobrazit na začátku, s pokud style příznak obsahuje NumberStyles.AllowLeadingWhite , nebo na konci znaku, pokud styl příznak obsahuje NumberStyles.AllowTrailingWhite .
$ Symbol měny pro konkrétní jazykovou verzi. Jeho pozice v řetězci je definována NumberFormatInfo.CurrencyPositivePattern vlastností objektu NumberFormatInfo vrácenou metodou GetFormat parametru provider . Symbol měny se může zobrazit v s , pokud style obsahuje NumberStyles.AllowCurrencySymbol příznak.
sign Volitelné kladné znaménko. (Operace parsování selže, pokud se v ssouboru nachází záporné znaménko.) Znaménko se může zobrazit na začátku, pokud obsahuje příznak, nebo na koncis, pokud style příznak obsahujeNumberStyles.AllowTrailingSign.NumberStyles.AllowLeadingSignstyles
Číslic Řada číslic od 0 do 9.
. Symbol desetinné čárky specifický pro jazykovou verzi. Symbol desetinné čárky jazykové verze zadané nástrojem provider se může zobrazit v s případě, že style obsahuje NumberStyles.AllowDecimalPoint příznak.
Fractional_digits Jeden nebo více výskytů číslice 0. Desetinné číslice se můžou zobrazit pouze v s případě, že style obsahuje NumberStyles.AllowDecimalPoint příznak.
E Znak e nebo E, který označuje, že hodnota je reprezentována v exponenciálním zápisu. Parametr s může představovat číslo v exponenciálním zápisuNumberStyles.AllowExponent, pokud style obsahuje příznak.
šestihranné Posloupnost šestnáctkových číslic od 0 do f nebo id 0 do F.

Poznámka

Operace analýzy ignoruje všechny ukončující ZNAKY NUL (U+0000) bez s ohledu na hodnotu argumentu style .

Řetězec pouze s desetinnými číslicemi (který odpovídá NumberStyles.None stylu) se vždy úspěšně parsuje. Většina zbývajících NumberStyles členů řídí prvky, které mohou být, ale nejsou nutné, aby byly přítomny v tomto vstupním řetězci. Následující tabulka uvádí, jak jednotlivé NumberStyles členy ovlivňují prvky, které mohou být přítomny v nástroji s.

Nesložené hodnoty NumberStyles Prvky, které jsou povoleny v s, kromě číslic
NumberStyles.None Pouze desítkové číslice.
NumberStyles.AllowDecimalPoint Elementy . a fractional_digits . Fractional_digits však musí obsahovat pouze jednu nebo více číslic 0 nebo metoda vrátí false.
NumberStyles.AllowExponent Parametr s může také použít exponenciální notaci. Pokud s představuje číslo v exponenciálním zápisu, musí představovat celé číslo v rozsahu datového Byte typu bez nenulové zlomkové komponenty.
NumberStyles.AllowLeadingWhite Element ws na začátku souboru s.
NumberStyles.AllowTrailingWhite Element ws na konci souboru s.
NumberStyles.AllowLeadingSign Před číslicemi se může zobrazit kladné znaménko.
NumberStyles.AllowTrailingSign Za číslicemi se může zobrazit kladné znaménko.
NumberStyles.AllowParentheses I když je tento příznak podporovaný, vrátí false metoda, pokud jsou v sobjektu k dispozici závorky.
NumberStyles.AllowThousands I když se symbol oddělovače skupin může zobrazit v s, může mu předcházet pouze jedna nebo více číslic 0.
NumberStyles.AllowCurrencySymbol Element $ .

Pokud se NumberStyles.AllowHexSpecifier použije příznak, s musí být šestnáctková hodnota bez předpony. Například "F3" parsuje úspěšně, ale "0xF3" ne. Jedinými dalšími příznaky, které se můžou vyskytovat v style systému, jsou NumberStyles.AllowLeadingWhite a NumberStyles.AllowTrailingWhite. (Výčet NumberStyles má složený styl čísla , NumberStyles.HexNumberkterý zahrnuje oba příznaky prázdných znaků.)

Parametr provider je IFormatProvider implementace, například CultureInfo objekt nebo NumberFormatInfo objekt, jehož GetFormat metoda vrací NumberFormatInfo objekt. Objekt NumberFormatInfo poskytuje informace specifické pro jazykovou verzi sformátu .

Viz také

Platí pro