Byte.Parse Metoda

Definicja

Konwertuje reprezentację ciągu liczby na jej Byte odpowiednik.

Przeciążenia

Parse(String, NumberStyles, IFormatProvider)

Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na Byte jego odpowiednik.

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na Byte jego odpowiednik.

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Analizuje zakres znaków UTF-8 w wartość.

Parse(String, IFormatProvider)

Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na Byte jego odpowiednik.

Parse(String, NumberStyles)

Konwertuje reprezentację ciągu liczby w określonym stylu na Byte jej odpowiednik.

Parse(ReadOnlySpan<Char>, IFormatProvider)

Analizuje zakres znaków w wartości.

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Analizuje zakres znaków UTF-8 w wartość.

Parse(String)

Konwertuje reprezentację ciągu liczby na Byte równoważną.

Parse(String, NumberStyles, IFormatProvider)

Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na Byte jego odpowiednik.

public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::Byte>::Parse;
public static byte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static byte Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Byte

Parametry

s
String

Ciąg zawierający liczbę, którą należy przekształcić. Ciąg jest interpretowany przy użyciu stylu określonego przez style.

style
NumberStyles

Bitowa kombinacja wartości wyliczenia, która wskazuje elementy stylu, które mogą być obecne w obiekcie s. Typową wartością do określenia jest Integer.

provider
IFormatProvider

Obiekt, który dostarcza informacje specyficzne dla kultury dotyczące formatu s. Jeśli provider jest to null, używana jest bieżąca kultura wątku.

Zwraca

Wartość bajtu równoważna liczbie zawartej w elemencie s.

Implementuje

Wyjątki

s nie jest poprawnym formatem.

s reprezentuje liczbę mniejszą niż Byte.MinValue lub większą niż Byte.MaxValue.

-lub-

s zawiera cyfry niezerowe, ułamkowe.

style nie jest wartością NumberStyles .

-lub-

style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .

Przykłady

Poniższy przykład kodu analizuje reprezentacje ciągów Byte wartości za pomocą tego przeciążenia Byte.Parse(String, NumberStyles, IFormatProvider) metody.

NumberStyles style;
CultureInfo^ culture;
String^ value;
Byte number;

// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles::Float;     
culture = CultureInfo::CreateSpecificCulture("fr-FR");
value = "12,000";

number = Byte::Parse(value, style, culture);
Console::WriteLine("Converted '{0}' to {1}.", value, number);

culture = CultureInfo::CreateSpecificCulture("en-GB");
try
{
   number = Byte::Parse(value, style, culture);
   Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", value); }   

value = "12.000";
number = Byte::Parse(value, style, culture);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
NumberStyles style;
CultureInfo culture;
string value;
byte number;

// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float;
culture = CultureInfo.CreateSpecificCulture("fr-FR");
value = "12,000";

number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);

culture = CultureInfo.CreateSpecificCulture("en-GB");
try
{
   number = Byte.Parse(value, style, culture);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", value); }

value = "12.000";
number = Byte.Parse(value, style, culture);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
// Parse number with decimals.
// NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
let style = NumberStyles.Float
let culture = CultureInfo.CreateSpecificCulture "fr-FR"
let value = "12,000"

let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

let culture = CultureInfo.CreateSpecificCulture "en-GB"
try
    let number = Byte.Parse(value, style, culture)
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."

let value = "12.000"
let number = Byte.Parse(value, style, culture)
printfn $"Converted '{value}' to {number}."

// The example displays the following output to the console:
//       Converted '12,000' to 12.
//       Unable to parse '12,000'.
//       Converted '12.000' to 12.
Dim style As NumberStyles
Dim culture As CultureInfo
Dim value As String
Dim number As Byte

' Parse number with decimals.
' NumberStyles.Float includes NumberStyles.AllowDecimalPoint.
style = NumberStyles.Float       
culture = CultureInfo.CreateSpecificCulture("fr-FR")
value = "12,000"

number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)

culture = CultureInfo.CreateSpecificCulture("en-GB")
Try
   number = Byte.Parse(value, style, culture)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)   
End Try      

value = "12.000"
number = Byte.Parse(value, style, culture)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' The example displays the following output to the console:
'       Converted '12,000' to 12.
'       Unable to parse '12,000'.
'       Converted '12.000' to 12.

Uwagi

Parametr style definiuje elementy stylu (takie jak biały znak lub znak dodatni), które są dozwolone w parametrze s operacji analizowania, aby operacja analizy zakończyła się pomyślnie. Musi być kombinacją flag bitowych z wyliczenia NumberStyles . W zależności od wartości parametr może styles zawierać następujące elementy:

[ws] [$] [znak]cyfry[.fractional_digits][e[znak]cyfry][ws]

Lub, jeśli style parametr zawiera AllowHexSpecifier:

[odstęp]cyfry_szesnastkowe[odstęp]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.

Element Opis
Ws Opcjonalny odstęp. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite lub na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingWhite .
$ Symbol waluty specyficzny dla kultury. Jego pozycja w ciągu jest definiowana przez NumberFormatInfo.CurrencyPositivePattern właściwość NumberFormatInfo obiektu zwróconego przez GetFormat metodę parametru provider . Symbol waluty może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol .
sign Opcjonalny znak wartości dodatnich. (Metoda zgłasza błąd, OverflowException jeśli znak ujemny znajduje się w spliku ). Znak może pojawić się na początku, s jeśli style zawiera flagę NumberStyles.AllowLeadingSign lub na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingSign .
Cyfr Sekwencja cyfr od 0 do 9.
. Symbol dziesiętny specyficzny dla kultury. Symbol punktu dziesiętnego kultury określonej przez provider może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint .
fractional_digits Jedno lub więcej wystąpień cyfry 0. Cyfry ułamkowe mogą być wyświetlane tylko wtedy s , gdy style zawiera flagę NumberStyles.AllowDecimalPoint .
E Znak e lub E, który wskazuje, że wartość jest reprezentowana w notacji wykładniczej. Parametr s może reprezentować liczbę w notacji wykładniczej, jeśli style zawiera flagę NumberStyles.AllowExponent .
hexdigits Sekwencja cyfr szesnastkowych od 0 do f lub od 0 do F.

Uwaga

Wszelkie znaki zakończenia NUL (U+0000) w obiekcie s są ignorowane przez operację analizowania, niezależnie od wartości argumentu style .

Ciąg z cyframi dziesiętnymi (który odpowiada NumberStyles.None stylowi) zawsze analizuje się pomyślnie. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, ale nie muszą być obecne w tym ciągu wejściowym. Poniższa tabela wskazuje, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w elemecie s.

Niezłożone wartości wyliczenia NumberStyles Dodatkowe (poza cyframi) elementy dozwolone w parametrze s
NumberStyles.None Tylko cyfry dziesiętne.
NumberStyles.AllowDecimalPoint Elementy . i fractional_digits . Jednak fractional_digits musi składać się tylko z jednej lub większej OverflowException liczby cyfr lub jest zgłaszana.
NumberStyles.AllowExponent Parametr s może również używać notacji wykładniczej.
NumberStyles.AllowLeadingWhite Element ws na początku s.
NumberStyles.AllowTrailingWhite Element ws na końcu selementu .
NumberStyles.AllowLeadingSign Znak dodatni może pojawić się przed cyframi.
NumberStyles.AllowTrailingSign Znak dodatni może pojawić się po cyfrach.
NumberStyles.AllowParentheses Mimo że ta flaga jest obsługiwana, użycie nawiasów w s wynikach w elemecie OverflowException.
NumberStyles.AllowThousands Chociaż symbol separatora grup może być wyświetlany w spliku , może być poprzedzony tylko jedną lub większą 0 cyframi.
NumberStyles.AllowCurrencySymbol Element $ .

Jeśli flaga NumberStyles.AllowHexSpecifier jest używana, s musi być wartością szesnastkową bez prefiksu. Na przykład "F3" analizuje się pomyślnie, ale "0xF3" nie. Jedynymi innymi flagami, które mogą być obecne, styleNumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles ma styl liczb złożony, NumberStyles.HexNumber, który zawiera obie flagi odstępu).

Parametr provider jest implementacją IFormatProvider , taką jak NumberFormatInfo obiekt lub CultureInfo . Parametr provider dostarcza informacje specyficzne dla kultury używane w analizowaniu. Jeśli provider jest to null, używana jest bieżąca kultura wątku.

Zobacz też

Dotyczy

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na Byte jego odpowiednik.

public static byte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static byte Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Byte

Parametry

s
ReadOnlySpan<Char>

Zakres zawierający znaki reprezentujące wartość do przekonwertowania.

style
NumberStyles

Bitowa kombinacja wartości wyliczenia, która wskazuje elementy stylu, które mogą być obecne w obiekcie s. Typową wartością do określenia jest Integer.

provider
IFormatProvider

Obiekt, który dostarcza informacje specyficzne dla kultury dotyczące formatu s. Jeśli provider jest to null, używana jest bieżąca kultura wątku.

Zwraca

Wartość bajtu równoważna liczbie zawartej w elemencie s.

Implementuje

Dotyczy

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

Analizuje zakres znaków UTF-8 w wartości.

public static byte Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> byte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Byte

Parametry

utf8Text
ReadOnlySpan<Byte>

Zakres znaków UTF-8 do analizy.

style
NumberStyles

Bitowa kombinacja stylów liczbowych, które mogą być obecne w .utf8Text

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text

Zwraca

Wynik analizy utf8Text.

Implementuje

Dotyczy

Parse(String, IFormatProvider)

Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na jego Byte odpowiednik.

public:
 static System::Byte Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static System::Byte Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::Byte>::Parse;
public static byte Parse (string s, IFormatProvider provider);
public static byte Parse (string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> byte
Public Shared Function Parse (s As String, provider As IFormatProvider) As Byte

Parametry

s
String

Ciąg zawierający liczbę, którą należy przekształcić. Ciąg jest interpretowany przy użyciu Integer stylu.

provider
IFormatProvider

Obiekt, który dostarcza informacje dotyczące analizy specyficznej dla kultury.s Jeśli provider parametr ma nullwartość , używana jest bieżąca kultura wątku.

Zwraca

Wartość bajtu, która jest równoważna liczbie zawartej w selemencie .

Implementuje

Wyjątki

s nie jest poprawnym formatem.

s reprezentuje liczbę mniejszą niż Byte.MinValue lub większą niż Byte.MaxValue.

Przykłady

Poniższy przykład analizuje reprezentacje ciągów Byte wartości za pomocą Parse metody .

String^ stringToConvert; 
Byte byteValue;

stringToConvert = " 214 ";
try {
   byteValue = Byte::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException^) {
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue); }

stringToConvert = " + 214 ";
try {
   byteValue = Byte::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException^) {
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue); }

stringToConvert = " +214 ";
try {
   byteValue = Byte::Parse(stringToConvert, CultureInfo::InvariantCulture);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException^) {
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue); }
// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
string stringToConvert;
byte byteValue;

stringToConvert = " 214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }

stringToConvert = " + 214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }

stringToConvert = " +214 ";
try {
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert); }
catch (OverflowException) {
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue); }
// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
let stringToConvert = " 214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

let stringToConvert = " + 214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

let stringToConvert = " +214 "
try
    let byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
    printfn $"Converted '{stringToConvert}' to {byteValue}." 
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}." 

// The example displays the following output to the console:
//       Converted ' 214 ' to 214.
//       Unable to parse ' + 214 '.
//       Converted ' +214 ' to 214.
Dim stringToConvert As String 
Dim byteValue As Byte

stringToConvert = " 214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  

stringToConvert = " + 214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  

stringToConvert = " +214 "
Try
   byteValue = Byte.Parse(stringToConvert, CultureInfo.InvariantCulture)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try
' The example displays the following output to the console:
'       Converted ' 214 ' to 214.
'       Unable to parse ' + 214 '.
'       Converted ' +214 ' to 214.

Uwagi

Parametr s zawiera liczbę formularzy:

[odstęp][znak]cyfry[odstęp]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.

Element Opis
Ws Opcjonalny odstęp.
sign Opcjonalny znak wartości dodatnich.
Cyfr Sekwencja cyfr od 0 do 9.

Parametr s jest interpretowany przy użyciu Integer stylu. Oprócz cyfr dziesiętnych wartości bajtów dozwolone są tylko spacje wiodące i końcowe wraz z znakiem wiodącym. (Jeśli znak jest obecny, musi to być znak dodatni lub metoda zgłasza OverflowExceptionznak ).) Aby jawnie zdefiniować elementy stylu wraz z informacjami o formatowaniu specyficznymi dla kultury, które mogą być obecne w sprogramie , użyj Byte.Parse(String, NumberStyles, IFormatProvider) metody .

Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie dostarczonym NumberFormatInfo przez providerprogram . Parametr provider jest implementacją, IFormatProvider taką jak NumberFormatInfo obiekt lub CultureInfo . Parametr provider dostarcza informacje specyficzne dla kultury używane podczas analizowania. Jeśli provider parametr ma nullwartość , używana jest bieżąca kultura wątku.

Zobacz też

Dotyczy

Parse(String, NumberStyles)

Konwertuje reprezentację ciągu liczby w określonym stylu na jego Byte odpowiednik.

public:
 static System::Byte Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static byte Parse (string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> byte
Public Shared Function Parse (s As String, style As NumberStyles) As Byte

Parametry

s
String

Ciąg zawierający liczbę, którą należy przekształcić. Ciąg jest interpretowany przy użyciu stylu określonego przez style.

style
NumberStyles

Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w obiekcie s. Typową wartością do określenia jest Integer.

Zwraca

Wartość bajtu, która jest równoważna liczbie zawartej w selemencie .

Wyjątki

s nie jest poprawnym formatem.

s reprezentuje liczbę mniejszą niż Byte.MinValue lub większą niż Byte.MaxValue.

-lub-

s zawiera cyfry niezerowe, ułamkowe.

style nie jest wartością NumberStyles .

-lub-

style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .

Przykłady

Poniższy przykład analizuje reprezentacje ciągów Byte wartości za pomocą Byte.Parse(String, NumberStyles) metody . W przykładzie bieżącą kulturą jest en-US.

String^ value;
NumberStyles style;
Byte number;

// Parse value with no styles allowed.
style = NumberStyles::None;
value = " 241 ";
try
{
   number = Byte::Parse(value, style);
   Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException^) {
   Console::WriteLine("Unable to parse '{0}'.", value); }   

// Parse value with trailing sign.
style = NumberStyles::Integer | NumberStyles::AllowTrailingSign;
value = " 163+";
number = Byte::Parse(value, style);
Console::WriteLine("Converted '{0}' to {1}.", value, number);

// Parse value with leading sign.
value = "   +253  ";
number = Byte::Parse(value, style);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
string value;
NumberStyles style;
byte number;

// Parse value with no styles allowed.
style = NumberStyles.None;
value = " 241 ";
try
{
   number = Byte.Parse(value, style);
   Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
   Console.WriteLine("Unable to parse '{0}'.", value); }

// Parse value with trailing sign.
style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
value = " 163+";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);

// Parse value with leading sign.
value = "   +253  ";
number = Byte.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
// Parse value with no styles allowed.
let style = NumberStyles.None
let value = " 241 "
try
    let number = Byte.Parse(value, style);
    printfn $"Converted '{value}' to {number}."
with :? FormatException ->
    printfn $"Unable to parse '{value}'."

// Parse value with trailing sign.
let style = NumberStyles.Integer ||| NumberStyles.AllowTrailingSign
let value = " 163+"
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// Parse value with leading sign.
let value = "   +253  "
let number = Byte.Parse(value, style)
printfn $"Converted '{value}' to {number}."

// This example displays the following output to the console:
//       Unable to parse ' 241 '.
//       Converted ' 163+' to 163.
//       Converted '   +253  ' to 253.
Dim value As String
Dim style As NumberStyles
Dim number As Byte

' Parse value with no styles allowed.
style = NumberStyles.None
value = " 241 "
Try
   number = Byte.Parse(value, style)
   Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", value)   
End Try
  
' Parse value with trailing sign.
style = NumberStyles.Integer Or NumberStyles.AllowTrailingSign
value = " 163+"
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)

' Parse value with leading sign.
value = "   +253  "
number = Byte.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
' This example displays the following output to the console:
'       Unable to parse ' 241 '.
'       Converted ' 163+' to 163.
'       Converted '   +253  ' to 253.

Uwagi

Parametr style definiuje elementy stylu (takie jak biały znak lub znak dodatni), które są dozwolone w parametrze s , aby operacja analizy zakończyła się pomyślnie. Musi to być kombinacja flag bitowych z NumberStyles wyliczenia. W zależności od wartości styleparametr może s zawierać następujące elementy:

[ws] [$] [znak]cyfry[.fractional_digits][e[znak]cyfry][ws]

Lub, jeśli style zawiera AllowHexSpecifier:

[odstęp]cyfry_szesnastkowe[odstęp]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.

Element Opis
Ws Opcjonalny odstęp. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite , lub na końcu s, jeśli styl zawiera flagę NumberStyles.AllowTrailingWhite .
$ Symbol waluty specyficzny dla kultury. Jego pozycja w ciągu jest definiowana NumberFormatInfo.CurrencyPositivePattern przez właściwość bieżącej kultury. Symbol waluty bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol .
sign Opcjonalny znak wartości dodatnich. (Metoda zgłasza wyjątek , jeśli znak ujemny OverflowException znajduje się w elemecie s. Znak może pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingSign , lub na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingSign .
Cyfr Sekwencja cyfr od 0 do 9.
. Symbol dziesiętny specyficzny dla kultury. Symbol punktu dziesiętnego bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint .
fractional_digits Jedno lub więcej wystąpień cyfry 0. Cyfry ułamkowe mogą być wyświetlane tylko wtedy s , gdy style zawiera flagę NumberStyles.AllowDecimalPoint .
E Znak e lub E, który wskazuje, że wartość jest reprezentowana w notacji wykładniczej. Parametr s może reprezentować liczbę w notacji wykładniczej, jeśli style zawiera flagę NumberStyles.AllowExponent .
hexdigits Sekwencja cyfr szesnastkowych od 0 do f lub od 0 do F.

Uwaga

Wszystkie znaki NUL zakończenia (U+0000) w s obiekcie są ignorowane przez operację analizowania, niezależnie od wartości argumentu style .

Ciąg z cyframi dziesiętnymi (odpowiadający NumberStyles.None stylowi) zawsze jest analizowanych pomyślnie. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, ale nie muszą być obecne w tym ciągu wejściowym. W poniższej tabeli przedstawiono, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w programie s.

Niezłożone wartości wyliczenia NumberStyles Dodatkowe (poza cyframi) elementy dozwolone w parametrze s
NumberStyles.None Tylko cyfry dziesiętne.
NumberStyles.AllowDecimalPoint Elementy . i fractional_digits . Jednak fractional_digits musi składać się tylko z co najmniej jednej cyfry lub OverflowException jest zgłaszany.
NumberStyles.AllowExponent Parametr s może również używać notacji wykładniczej.
NumberStyles.AllowLeadingWhite Element ws na początku elementu s.
NumberStyles.AllowTrailingWhite Element ws na końcu elementu s.
NumberStyles.AllowLeadingSign Przed cyframi może pojawić się znak dodatni.
NumberStyles.AllowTrailingSign Po cyfrach może pojawić się znak dodatni.
NumberStyles.AllowParentheses Mimo że ta flaga jest obsługiwana, użycie nawiasów w s wyniku jest wynikiem OverflowException.
NumberStyles.AllowThousands Chociaż symbol separatora grup może być wyświetlany w selemecie , może być poprzedzony tylko jedną lub 0 cyframi.
NumberStyles.AllowCurrencySymbol Element $ .

Jeśli flaga NumberStyles.AllowHexSpecifier jest używana, s musi być wartością szesnastkową bez prefiksu. Na przykład "F3" analizuje się pomyślnie, ale "0xF3" nie. Jedynymi innymi flagami, które można połączyć z nim, są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles zawiera styl liczby złożonej, NumberStyles.HexNumber, który zawiera obie flagi odstępu).

Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie zainicjowanym NumberFormatInfo dla bieżącej kultury systemu. Aby użyć informacji o formatowaniu innej kultury, wywołaj Byte.Parse(String, NumberStyles, IFormatProvider) przeciążenie.

Zobacz też

Dotyczy

Parse(ReadOnlySpan<Char>, IFormatProvider)

Analizuje zakres znaków w wartości.

public:
 static System::Byte Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::Byte>::Parse;
public static byte Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> byte
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Byte

Parametry

s
ReadOnlySpan<Char>

Zakres znaków do przeanalizowania.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.s

Zwraca

Wynik analizy s.

Implementuje

Dotyczy

Parse(ReadOnlySpan<Byte>, IFormatProvider)

Analizuje zakres znaków UTF-8 w wartości.

public:
 static System::Byte Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::Byte>::Parse;
public static byte Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> byte
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Byte

Parametry

utf8Text
ReadOnlySpan<Byte>

Zakres znaków UTF-8 do analizy.

provider
IFormatProvider

Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text

Zwraca

Wynik analizy utf8Text.

Implementuje

Dotyczy

Parse(String)

Konwertuje reprezentację ciągu liczby na jej Byte odpowiednik.

public:
 static System::Byte Parse(System::String ^ s);
public static byte Parse (string s);
static member Parse : string -> byte
Public Shared Function Parse (s As String) As Byte

Parametry

s
String

Ciąg zawierający liczbę, którą należy przekształcić. Ciąg jest interpretowany przy użyciu Integer stylu.

Zwraca

Wartość bajtu, która jest równoważna liczbie zawartej w selemencie .

Wyjątki

s nie jest poprawnym formatem.

s reprezentuje liczbę mniejszą niż Byte.MinValue lub większą niż Byte.MaxValue.

Przykłady

W poniższym przykładzie pokazano, jak przekonwertować wartość ciągu na wartość bajtową Byte.Parse(String) przy użyciu metody . Wynikowa wartość bajtu jest następnie wyświetlana w konsoli programu .

String^ stringToConvert = " 162";
Byte byteValue;
try
{
   byteValue = Byte::Parse(stringToConvert);
   Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}   
catch (FormatException^)
{
   Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException^)
{
   Console::WriteLine("'{0}' is greater than {1} or less than {2}.", 
                     stringToConvert, Byte::MaxValue, Byte::MinValue);
}  
// The example displays the following output to the console:
//       Converted ' 162' to 162.
string stringToConvert = " 162";
byte byteValue;
try
{
   byteValue = Byte.Parse(stringToConvert);
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue);
}
catch (FormatException)
{
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.",
                     stringToConvert, Byte.MaxValue, Byte.MinValue);
}
// The example displays the following output to the console:
//       Converted ' 162' to 162.
let stringToConvert = " 162"
try
    let byteValue = Byte.Parse stringToConvert
    printfn $"Converted '{stringToConvert}' to {byteValue}."
with
| :? FormatException ->
    printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
    printfn $"'{stringToConvert}' is greater than {Byte.MaxValue} or less than {Byte.MinValue}."

// The example displays the following output to the console:
//       Converted ' 162' to 162.
Dim stringToConvert As String = " 162"
Dim byteValue As Byte
Try
   byteValue = Byte.Parse(stringToConvert)
   Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, byteValue)
Catch e As FormatException
   Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
   Console.WriteLine("'{0}' is greater than {1} or less than {2}.", _
                     stringToConvert, Byte.MaxValue, Byte.MinValue)
End Try  
' The example displays the following output to the console:
'       Converted ' 162' to 162.

Uwagi

Parametr s zawiera liczbę formularzy:

[odstęp][znak]cyfry[odstęp]

Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.

Element Opis
Ws Opcjonalny odstęp.
sign Opcjonalny znak dodatni lub ujemny.
Cyfr Sekwencja cyfr od 0 do 9.

Parametr s jest interpretowany przy użyciu NumberStyles.Integer stylu. Oprócz cyfr dziesiętnych wartości bajtów dozwolone są tylko spacje wiodące i końcowe wraz z znakiem wiodącym. (Jeśli znak jest obecny, musi to być znak dodatni lub metoda zgłasza OverflowExceptionznak ).) Aby jawnie zdefiniować elementy stylu, które mogą być obecne w sprogramie , użyj Byte.Parse(String, NumberStyles) metody lub Byte.Parse(String, NumberStyles, IFormatProvider) .

Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie zainicjowanym NumberFormatInfo dla bieżącej kultury systemu. Aby uzyskać więcej informacji, zobacz CurrentInfo. Aby przeanalizować ciąg przy użyciu informacji o formatowaniu innej kultury, użyj Byte.Parse(String, NumberStyles, IFormatProvider) metody .

Zobacz też

Dotyczy