UInt32.Parse Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Konverterar strängrepresentationen av ett tal till dess 32-bitars osignerade heltalsekvivalent.
Överlagringar
| Name | Description |
|---|---|
| Parse(String, NumberStyles, IFormatProvider) |
Konverterar strängrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 32-bitars osignerade heltalsmotsvarighet. |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Konverterar spanrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 32-bitars osignerade heltalsekvivalent. |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
Parsar ett intervall med UTF-8 tecken till ett värde. |
| Parse(String, IFormatProvider) |
Konverterar strängrepresentationen av ett tal i ett angivet kulturspecifikt format till dess 32-bitars osignerade heltalsekvivalent. |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
Parsar ett teckenintervall till ett värde. |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
Parsar ett intervall med UTF-8 tecken till ett värde. |
| Parse(String) |
Konverterar strängrepresentationen av ett tal till dess 32-bitars osignerade heltalsekvivalent. |
| Parse(String, NumberStyles) |
Konverterar strängrepresentationen av ett tal i ett angivet format till dess 32-bitars osignerade heltalsekvivalent. |
Parse(String, NumberStyles, IFormatProvider)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Konverterar strängrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 32-bitars osignerade heltalsmotsvarighet.
public:
static System::UInt32 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static System::UInt32 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::UInt32>::Parse;
[System.CLSCompliant(false)]
public static uint Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static uint Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static uint Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint32
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint32
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As UInteger
Parametrar
- s
- String
En sträng som representerar talet som ska konverteras. Strängen tolkas med hjälp av det format som anges av parametern style .
- style
- NumberStyles
En bitvis kombination av uppräkningsvärden som anger de formatelement som kan finnas i s. Ett typiskt värde att ange är Integer.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Ett 32-bitars osignerat heltal som motsvarar det tal som anges i s.
Implementeringar
- Attribut
Undantag
s är null.
style är inte ett NumberStyles värde.
-eller-
style är inte en kombination av AllowHexSpecifier och HexNumber värden.
s är inte i ett format som är kompatibelt med style.
s representerar ett tal som är mindre än UInt32.MinValue eller större än UInt32.MaxValue.
-eller-
s innehåller icke-noll, bråktalssiffror.
Exempel
I följande exempel används Parse(String, NumberStyles, IFormatProvider) metoden för att konvertera olika strängrepresentationer av tal till 32-bitars osignerade heltalsvärden.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] cultureNames= { "en-US", "fr-FR" };
NumberStyles[] styles= { NumberStyles.Integer,
NumberStyles.Integer | NumberStyles.AllowDecimalPoint };
string[] values = { "170209", "+170209.0", "+170209,0", "-103214.00",
"-103214,00", "104561.1", "104561,1" };
// Parse strings using each culture
foreach (string cultureName in cultureNames)
{
CultureInfo ci = new CultureInfo(cultureName);
Console.WriteLine("Parsing strings using the {0} culture",
ci.DisplayName);
// Use each style.
foreach (NumberStyles style in styles)
{
Console.WriteLine(" Style: {0}", style.ToString());
// Parse each numeric string.
foreach (string value in values)
{
try {
Console.WriteLine(" Converted '{0}' to {1}.", value,
UInt32.Parse(value, style, ci));
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the UInt32 type.",
value);
}
}
}
}
}
}
// The example displays the following output:
// Parsing strings using the English (United States) culture
// Style: Integer
// Converted '170209' to 170209.
// Unable to parse '+170209.0'.
// Unable to parse '+170209,0'.
// Unable to parse '-103214.00'.
// Unable to parse '-103214,00'.
// Unable to parse '104561.1'.
// Unable to parse '104561,1'.
// Style: Integer, AllowDecimalPoint
// Converted '170209' to 170209.
// Converted '+170209.0' to 170209.
// Unable to parse '+170209,0'.
// '-103214.00' is out of range of the UInt32 type.
// Unable to parse '-103214,00'.
// '104561.1' is out of range of the UInt32 type.
// Unable to parse '104561,1'.
// Parsing strings using the French (France) culture
// Style: Integer
// Converted '170209' to 170209.
// Unable to parse '+170209.0'.
// Unable to parse '+170209,0'.
// Unable to parse '-103214.00'.
// Unable to parse '-103214,00'.
// Unable to parse '104561.1'.
// Unable to parse '104561,1'.
// Style: Integer, AllowDecimalPoint
// Converted '170209' to 170209.
// Unable to parse '+170209.0'.
// Converted '+170209,0' to 170209.
// Unable to parse '-103214.00'.
// '-103214,00' is out of range of the UInt32 type.
// Unable to parse '104561.1'.
// '104561,1' is out of range of the UInt32 type.
open System
open System.Globalization
let cultureNames = [| "en-US"; "fr-FR" |]
let styles =
[| NumberStyles.Integer; NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint |]
let values =
[| "170209"; "+170209.0"; "+170209,0"; "-103214.00"; "-103214,00"; "104561.1"; "104561,1" |]
// Parse strings using each culture
for cultureName in cultureNames do
let ci = CultureInfo cultureName
printfn $"Parsing strings using the {ci.DisplayName} culture"
// Use each style.
for style in styles do
printfn $" Style: {style}"
// Parse each numeric string.
for value in values do
try
printfn $" Converted '{value}' to {UInt32.Parse(value, style, ci)}."
with
| :? FormatException ->
printfn $" Unable to parse '{value}'."
| :? OverflowException ->
printfn $" '{value}' is out of range of the UInt32 type."
// The example displays the following output:
// Parsing strings using the English (United States) culture
// Style: Integer
// Converted '170209' to 170209.
// Unable to parse '+170209.0'.
// Unable to parse '+170209,0'.
// Unable to parse '-103214.00'.
// Unable to parse '-103214,00'.
// Unable to parse '104561.1'.
// Unable to parse '104561,1'.
// Style: Integer, AllowDecimalPoint
// Converted '170209' to 170209.
// Converted '+170209.0' to 170209.
// Unable to parse '+170209,0'.
// '-103214.00' is out of range of the UInt32 type.
// Unable to parse '-103214,00'.
// '104561.1' is out of range of the UInt32 type.
// Unable to parse '104561,1'.
// Parsing strings using the French (France) culture
// Style: Integer
// Converted '170209' to 170209.
// Unable to parse '+170209.0'.
// Unable to parse '+170209,0'.
// Unable to parse '-103214.00'.
// Unable to parse '-103214,00'.
// Unable to parse '104561.1'.
// Unable to parse '104561,1'.
// Style: Integer, AllowDecimalPoint
// Converted '170209' to 170209.
// Unable to parse '+170209.0'.
// Converted '+170209,0' to 170209.
// Unable to parse '-103214.00'.
// '-103214,00' is out of range of the UInt32 type.
// Unable to parse '104561.1'.
// '104561,1' is out of range of the UInt32 type.
Imports System.Globalization
Module Example
Public Sub Main()
Dim cultureNames() As String = { "en-US", "fr-FR" }
Dim styles() As NumberStyles = { NumberStyles.Integer, _
NumberStyles.Integer Or NumberStyles.AllowDecimalPoint }
Dim values() As String = { "170209", "+170209.0", "+170209,0", "-103214.00", _
"-103214,00", "104561.1", "104561,1" }
' Parse strings using each culture
For Each cultureName As String In cultureNames
Dim ci As New CultureInfo(cultureName)
Console.WriteLine("Parsing strings using the {0} culture", ci.DisplayName)
' Use each style.
For Each style As NumberStyles In styles
Console.WriteLine(" Style: {0}", style.ToString())
' Parse each numeric string.
For Each value As String In values
Try
Console.WriteLine(" Converted '{0}' to {1}.", value, _
UInt32.Parse(value, style, ci))
Catch e As FormatException
Console.WriteLine(" Unable to parse '{0}'.", value)
Catch e As OverflowException
Console.WriteLine(" '{0}' is out of range of the UInt32 type.", _
value)
End Try
Next
Next
Next
End Sub
End Module
' The example displays the following output:
' Parsing strings using the English (United States) culture
' Style: Integer
' Converted '170209' to 170209.
' Unable to parse '+170209.0'.
' Unable to parse '+170209,0'.
' Unable to parse '-103214.00'.
' Unable to parse '-103214,00'.
' Unable to parse '104561.1'.
' Unable to parse '104561,1'.
' Style: Integer, AllowDecimalPoint
' Converted '170209' to 170209.
' Converted '+170209.0' to 170209.
' Unable to parse '+170209,0'.
' '-103214.00' is out of range of the UInt32 type.
' Unable to parse '-103214,00'.
' '104561.1' is out of range of the UInt32 type.
' Unable to parse '104561,1'.
' Parsing strings using the French (France) culture
' Style: Integer
' Converted '170209' to 170209.
' Unable to parse '+170209.0'.
' Unable to parse '+170209,0'.
' Unable to parse '-103214.00'.
' Unable to parse '-103214,00'.
' Unable to parse '104561.1'.
' Unable to parse '104561,1'.
' Style: Integer, AllowDecimalPoint
' Converted '170209' to 170209.
' Unable to parse '+170209.0'.
' Converted '+170209,0' to 170209.
' Unable to parse '-103214.00'.
' '-103214,00' is out of range of the UInt32 type.
' Unable to parse '104561.1'.
' '104561,1' is out of range of the UInt32 type.
Kommentarer
Parametern style definierar formatelementen (till exempel blanksteg eller symbolen för positiva eller negativa tecken) som tillåts i parametern s för att parsningsåtgärden ska lyckas. Det måste vara en kombination av bitflaggor från NumberStyles uppräkningen.
Beroende på värdet för stylekan parametern s innehålla följande element:
[ws][$][sign]digits[.fractional_digits][E[sign]exponential_digits][ws]
Element inom hakparenteser ([ och ]) är valfria. Om style innehåller NumberStyles.AllowHexSpecifierkan parametern s innehålla följande element:
[ws]hexdigits[ws]
I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Valfritt blanksteg. Tomt utrymme kan visas i början av s om style innehåller NumberStyles.AllowLeadingWhite flaggan, och det kan visas i slutet av s om style innehåller NumberStyles.AllowTrailingWhite flaggan. |
| $ | En kulturspecifik valutasymbol. Dess position i strängen definieras av CurrencyPositivePattern egenskapen för objektet NumberFormatInfo som returneras av GetFormat parameterns provider metod. Valutasymbolen kan visas i s om style den NumberStyles.AllowCurrencySymbol innehåller flaggan. |
| signera | Ett valfritt tecken. (Metoden genererar ett OverflowException om s innehåller ett negativt tecken och representerar ett tal som inte är noll.) Tecknet kan visas i början av s om style innehåller NumberStyles.AllowLeadingSign flaggan, och det kan visas i slutet av s om style innehåller NumberStyles.AllowTrailingSign flaggan. Parenteser kan användas i s för att ange ett negativt värde om style den NumberStyles.AllowParentheses innehåller flaggan. |
| Siffror | En sekvens med siffror från 0 till 9. |
| . | Ett kulturspecifikt decimaltecken. Den aktuella kulturens decimaltecken kan visas i s om style den NumberStyles.AllowDecimalPoint innehåller flaggan. |
| fractional_digits | En eller flera förekomster av siffran 0–9 om style den NumberStyles.AllowExponent innehåller flaggan, eller en eller flera förekomster av siffran 0 om den inte gör det. Bråksiffror kan bara visas i s om style den NumberStyles.AllowDecimalPoint innehåller flaggan. |
| E | Tecknet "e" eller "E", som anger att värdet representeras i exponentiell (vetenskaplig) notation. Parametern s kan representera ett tal i exponentiell notation om style den NumberStyles.AllowExponent innehåller flaggan. |
| exponential_digits | En sekvens med siffror från 0 till 9. Parametern s kan representera ett tal i exponentiell notation om style den NumberStyles.AllowExponent innehåller flaggan. |
| hexdigits | En sekvens med hexadecimala siffror från 0 till f eller 0 till och med F. |
Note
Alla avslutande NUL-tecken (U+0000) i s ignoreras av parsningsåtgärden, oavsett argumentets style värde.
En sträng med endast decimalsiffror (vilket motsvarar NumberStyles.None formatet) parsar alltid korrekt. De flesta av de återstående NumberStyles medlemmarna styr element som kan finnas, men som inte måste finnas i den här indatasträngen. Följande tabell visar hur enskilda NumberStyles medlemmar påverkar de element som kan finnas i s.
Icke-sammansatta NumberStyles värden |
Element som tillåts s utöver siffror |
|---|---|
| NumberStyles.None | Endast decimalsiffror. |
| NumberStyles.AllowDecimalPoint | Decimaltecknet (.) och fractional_digits element. Men om formatmallen NumberStyles.AllowExponent inte innehåller flaggan måste fractional_digits endast bestå av en eller flera 0 siffror. Annars genereras en OverflowException . |
| NumberStyles.AllowExponent | Tecknet "e" eller "E", som anger exponentiell notation, tillsammans med exponential_digits. |
| NumberStyles.AllowLeadingWhite |
WS-elementet i början av s. |
| NumberStyles.AllowTrailingWhite |
WS-elementet i slutet av s. |
| NumberStyles.AllowLeadingSign | Ett tecken före siffror. |
| NumberStyles.AllowTrailingSign | Ett tecken efter siffror. |
| NumberStyles.AllowParentheses | Parenteser före och efter siffror för att indikera ett negativt värde. |
| NumberStyles.AllowThousands | Gruppavgränsaren (,). |
| NumberStyles.AllowCurrencySymbol | Valutaelementet ($). |
NumberStyles.AllowHexSpecifier Om flaggan används s måste vara ett hexadecimalt värde. De enda andra flaggorna som kan kombineras med den är NumberStyles.AllowLeadingWhite och NumberStyles.AllowTrailingWhite. (Uppräkningen NumberStyles innehåller ett sammansatt talformat, NumberStyles.HexNumber, som innehåller båda blankstegsflaggor.)
Note
Om parametern s är strängrepresentationen av ett hexadecimalt tal kan den inte föregås av någon dekoration (till exempel 0x eller &h) som särskiljer den som ett hexadecimalt tal. Detta gör att parsningsåtgärden utlöser ett undantag.
Parametern provider är en IFormatProvider implementering vars GetFormat metod returnerar ett NumberFormatInfo objekt som tillhandahåller kulturspecifik information om formatet för s. Det finns tre sätt att använda parametern provider för att ange anpassad formateringsinformation till parsningsåtgärden:
Du kan skicka det faktiska NumberFormatInfo objektet som innehåller formateringsinformation. (Dess implementering av GetFormat returnerar helt enkelt sig själv.)
Du kan skicka ett CultureInfo objekt som anger den kultur vars formatering ska användas. Dess NumberFormat egenskap innehåller formateringsinformation.
Du kan genomföra en anpassad IFormatProvider implementering. Dess GetFormat metod måste instansiera och returnera det NumberFormatInfo objekt som tillhandahåller formateringsinformation.
Om provider är nullNumberFormatInfo används objektet för den aktuella kulturen.
Se även
Gäller för
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Viktigt!
Detta API uppfyller inte CLS.
Konverterar spanrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 32-bitars osignerade heltalsekvivalent.
public static uint Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static uint Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static uint Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint32
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint32
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As UInteger
Parametrar
- s
- ReadOnlySpan<Char>
Ett intervall som innehåller de tecken som representerar talet som ska konverteras. Intervallet tolkas med hjälp av det format som anges av parametern style .
- style
- NumberStyles
En bitvis kombination av uppräkningsvärden som anger de formatelement som kan finnas i s. Ett typiskt värde att ange är Integer.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Ett 32-bitars osignerat heltal som motsvarar det tal som anges i s.
Implementeringar
- Attribut
Gäller för
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Parsar ett intervall med UTF-8 tecken till ett värde.
public static uint Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> uint32
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As UInteger
Parametrar
- utf8Text
- ReadOnlySpan<Byte>
Intervallet för UTF-8 tecken att parsa.
- style
- NumberStyles
En bitvis kombination av talformat som kan finnas i utf8Text.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om utf8Text.
Returer
Resultatet av parsning utf8Text.
Implementeringar
Gäller för
Parse(String, IFormatProvider)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Konverterar strängrepresentationen av ett tal i ett angivet kulturspecifikt format till dess 32-bitars osignerade heltalsekvivalent.
public:
static System::UInt32 Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static System::UInt32 Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::UInt32>::Parse;
[System.CLSCompliant(false)]
public static uint Parse(string s, IFormatProvider provider);
public static uint Parse(string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static uint Parse(string s, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> uint32
static member Parse : string * IFormatProvider -> uint32
Public Shared Function Parse (s As String, provider As IFormatProvider) As UInteger
Parametrar
- s
- String
En sträng som representerar talet som ska konverteras.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Ett 32-bitars osignerat heltal som motsvarar det tal som anges i s.
Implementeringar
- Attribut
Undantag
s är null.
s är inte i rätt format.
s representerar ett tal som är mindre än UInt32.MinValue eller större än UInt32.MaxValue.
Exempel
Följande exempel är knappklickshändelsehanteraren för ett webbformulär. Den använder matrisen som returneras av HttpRequest.UserLanguages egenskapen för att fastställa användarens nationella inställningar. Det instansierar sedan ett CultureInfo objekt som motsvarar det nationella språket. Objektet NumberFormatInfo som tillhör objektet CultureInfo skickas sedan till Parse(String, IFormatProvider) metoden för att konvertera användarens indata till ett UInt32 värde.
protected void OkToUInteger_Click(object sender, EventArgs e)
{
string locale;
uint number;
CultureInfo culture;
// Return if string is empty
if (String.IsNullOrEmpty(this.inputNumber.Text))
return;
// Get locale of web request to determine possible format of number
if (Request.UserLanguages.Length == 0)
return;
locale = Request.UserLanguages[0];
if (String.IsNullOrEmpty(locale))
return;
// Instantiate CultureInfo object for the user's locale
culture = new CultureInfo(locale);
// Convert user input from a string to a number
try
{
number = UInt32.Parse(this.inputNumber.Text, culture.NumberFormat);
}
catch (FormatException)
{
return;
}
catch (Exception)
{
return;
}
// Output number to label on web form
this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OKToUInteger_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToUInteger.Click
Dim locale As String
Dim culture As CultureInfo
Dim number As UInteger
' Return if string is empty
If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub
' Get locale of web request to determine possible format of number
If Request.UserLanguages.Length = 0 Then Exit Sub
locale = Request.UserLanguages(0)
If String.IsNullOrEmpty(locale) Then Exit Sub
' Instantiate CultureInfo object for the user's locale
culture = New CultureInfo(locale)
' Convert user input from a string to a number
Try
number = UInt32.Parse(Me.inputNumber.Text, culture.NumberFormat)
Catch ex As FormatException
Exit Sub
Catch ex As Exception
Exit Sub
End Try
' Output number to label on web form
Me.outputNumber.Text = "Number is " & number.ToString()
End Sub
Kommentarer
Parametern s innehåller ett antal av formuläret:
[ws][sign]digits[ws]
Objekt inom hakparenteser ([ och ]) är valfria. I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Valfritt blanksteg. |
| signera | Ett valfritt tecken eller ett negativt tecken om s representerar värdet noll. |
| Siffror | En sekvens med siffror mellan 0 och 9. |
Parametern s tolkas med formatet NumberStyles.Integer . Förutom det osignerade heltalsvärdets decimalsiffror tillåts endast inledande och avslutande blanksteg tillsammans med ett inledande tecken. (Om det negativa tecknet finns s måste det representera ett värde på noll, eller så genererar metoden en OverflowException.) Om du vill definiera formatelementen explicit tillsammans med den kulturspecifika formateringsinformation som kan finnas i sanvänder du Parse(String, NumberStyles, IFormatProvider) metoden .
Parametern provider är en IFormatProvider implementering vars GetFormat metod returnerar ett NumberFormatInfo objekt som tillhandahåller kulturspecifik information om formatet för s. Det finns tre sätt att använda parametern provider för att ange anpassad formateringsinformation till parsningsåtgärden:
Du kan skicka det faktiska NumberFormatInfo objektet som innehåller formateringsinformation. (Dess implementering av GetFormat returnerar helt enkelt sig själv.)
Du kan skicka ett CultureInfo objekt som anger den kultur vars formatering ska användas. Dess NumberFormat egenskap innehåller formateringsinformation.
Du kan genomföra en anpassad IFormatProvider implementering. Dess GetFormat metod måste instansiera och returnera det NumberFormatInfo objekt som tillhandahåller formateringsinformation.
Om provider är nullNumberFormatInfo används för den aktuella kulturen.
Se även
Gäller för
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Parsar ett teckenintervall till ett värde.
public:
static System::UInt32 Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::UInt32>::Parse;
public static uint Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> uint32
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As UInteger
Parametrar
- s
- ReadOnlySpan<Char>
Det intervall med tecken som ska parsas.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Resultatet av parsning s.
Implementeringar
Gäller för
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Parsar ett intervall med UTF-8 tecken till ett värde.
public:
static System::UInt32 Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::UInt32>::Parse;
public static uint Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> uint32
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As UInteger
Parametrar
- utf8Text
- ReadOnlySpan<Byte>
Intervallet för UTF-8 tecken att parsa.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om utf8Text.
Returer
Resultatet av parsning utf8Text.
Implementeringar
Gäller för
Parse(String)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Konverterar strängrepresentationen av ett tal till dess 32-bitars osignerade heltalsekvivalent.
public:
static System::UInt32 Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static uint Parse(string s);
public static uint Parse(string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> uint32
static member Parse : string -> uint32
Public Shared Function Parse (s As String) As UInteger
Parametrar
- s
- String
En sträng som representerar talet som ska konverteras.
Returer
Ett 32-bitars osignerat heltal som motsvarar talet i s.
- Attribut
Undantag
Parametern s är null.
Parametern s har inte rätt format.
Parametern s representerar ett tal som är mindre än UInt32.MinValue eller större än UInt32.MaxValue.
Exempel
I följande exempel används Parse(String) metoden för att parsa en matris med strängvärden.
string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "2147483648",
"14065839182", "16e07", "134985.0", "-12034" };
foreach (string value in values)
{
try {
uint number = UInt32.Parse(value);
Console.WriteLine("{0} --> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("{0}: Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("{0}: Overflow", value);
}
}
// The example displays the following output:
// +13230 --> 13230
// -0 --> 0
// 1,390,146: Bad Format
// $190,235,421,127: Bad Format
// 0xFA1B: Bad Format
// 163042 --> 163042
// -10: Overflow
// 2147483648 --> 2147483648
// 14065839182: Overflow
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034: Overflow
open System
let values =
[| "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
"0xFA1B"; "163042"; "-10"; "2147483648"
"14065839182"; "16e07"; "134985.0"; "-12034" |]
for value in values do
try
let number = UInt32.Parse value
printfn $"{value} --> {number}"
with
| :? FormatException ->
printfn $"{value}: Bad Format"
| :? OverflowException ->
printfn $"{value}: Overflow"
// The example displays the following output:
// +13230 --> 13230
// -0 --> 0
// 1,390,146: Bad Format
// $190,235,421,127: Bad Format
// 0xFA1B: Bad Format
// 163042 --> 163042
// -10: Overflow
// 2147483648 --> 2147483648
// 14065839182: Overflow
// 16e07: Bad Format
// 134985.0: Bad Format
// -12034: Overflow
Dim values() As String = { "+13230", "-0", "1,390,146", "$190,235,421,127",
"0xFA1B", "163042", "-10", "2147483648",
"14065839182", "16e07", "134985.0", "-12034" }
For Each value As String In values
Try
Dim number As UInteger = UInt32.Parse(value)
Console.WriteLine("{0} --> {1}", value, number)
Catch e As FormatException
Console.WriteLine("{0}: Bad Format", value)
Catch e As OverflowException
Console.WriteLine("{0}: Overflow", value)
End Try
Next
' The example displays the following output:
' +13230 --> 13230
' -0 --> 0
' 1,390,146: Bad Format
' $190,235,421,127: Bad Format
' 0xFA1B: Bad Format
' 163042 --> 163042
' -10: Overflow
' 2147483648 --> 2147483648
' 14065839182: Overflow
' 16e07: Bad Format
' 134985.0: Bad Format
' -12034: Overflow
Kommentarer
Parametern s ska vara strängrepresentationen av ett tal i följande formulär.
[ws][sign]digits[ws]
Element inom hakparenteser ([ och ]) är valfria. I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Valfritt blanksteg. |
| signera | Ett valfritt tecken. Giltiga teckentecken bestäms av NumberFormatInfo.NegativeSign egenskaperna och NumberFormatInfo.PositiveSign för den aktuella kulturen. Den negativa teckensymbolen kan dock endast användas med noll. annars genererar metoden en OverflowException. |
| Siffror | En sekvens med siffror mellan 0 och 9. Inledande nollor ignoreras. |
Note
Strängen som anges av parametern s tolkas med hjälp NumberStyles.Integer av formatet . Den får inte innehålla några gruppavgränsare eller decimaltecken och får inte ha en decimaldel.
Parametern s parsas med hjälp av formateringsinformationen i ett System.Globalization.NumberFormatInfo objekt som initieras för den aktuella systemkulturen. Mer information finns i NumberFormatInfo.CurrentInfo. Använd metoden för att parsa en sträng med hjälp av formateringsinformationen Parse(String, IFormatProvider) för en viss kultur.
Se även
Gäller för
Parse(String, NumberStyles)
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
- Källa:
- UInt32.cs
Konverterar strängrepresentationen av ett tal i ett angivet format till dess 32-bitars osignerade heltalsekvivalent.
public:
static System::UInt32 Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static uint Parse(string s, System.Globalization.NumberStyles style);
public static uint Parse(string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint32
static member Parse : string * System.Globalization.NumberStyles -> uint32
Public Shared Function Parse (s As String, style As NumberStyles) As UInteger
Parametrar
- s
- String
En sträng som representerar talet som ska konverteras. Strängen tolkas med hjälp av det format som anges av parametern style .
- style
- NumberStyles
En bitvis kombination av uppräkningsvärdena som anger det tillåtna formatet för s. Ett typiskt värde att ange är Integer.
Returer
Ett 32-bitars osignerat heltal som motsvarar det tal som anges i s.
- Attribut
Undantag
s är null.
style är inte ett NumberStyles värde.
-eller-
style är inte en kombination av AllowHexSpecifier och HexNumber värden.
s är inte i ett format som är kompatibelt med style.
s representerar ett tal som är mindre än UInt32.MinValue eller större än UInt32.MaxValue.
-eller-
s innehåller icke-noll, bråktalssiffror.
Exempel
I följande exempel försöker parsa varje element i en strängmatris med hjälp av ett antal NumberStyles värden.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values= { " 214309 ", "1,064,181", "(0)", "10241+", " + 21499 ",
" +21499 ", "122153.00", "1e03ff", "91300.0e-2" };
NumberStyles whitespace = NumberStyles.AllowLeadingWhite | NumberStyles.AllowTrailingWhite;
NumberStyles[] styles= { NumberStyles.None, whitespace,
NumberStyles.AllowLeadingSign | NumberStyles.AllowTrailingSign | whitespace,
NumberStyles.AllowThousands | NumberStyles.AllowCurrencySymbol,
NumberStyles.AllowExponent | NumberStyles.AllowDecimalPoint };
// Attempt to convert each number using each style combination.
foreach (string value in values)
{
Console.WriteLine("Attempting to convert '{0}':", value);
foreach (NumberStyles style in styles)
{
try {
uint number = UInt32.Parse(value, style);
Console.WriteLine(" {0}: {1}", style, number);
}
catch (FormatException) {
Console.WriteLine(" {0}: Bad Format", style);
}
catch (OverflowException)
{
Console.WriteLine(" {0}: Overflow", value);
}
}
Console.WriteLine();
}
}
}
// The example displays the following output:
// Attempting to convert ' 214309 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: 214309
// Integer, AllowTrailingSign: 214309
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '1,064,181':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064181
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '(0)':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '10241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 10241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' + 21499 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' +21499 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 21499
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '122153.00':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 122153
//
// Attempting to convert '1e03ff':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '91300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 913
open System
open System.Globalization
let values =
[| " 214309 "; "1,064,181"; "(0)"; "10241+"; " + 21499 "
" +21499 "; "122153.00"; "1e03ff"; "91300.0e-2" |]
let whitespace = NumberStyles.AllowLeadingWhite ||| NumberStyles.AllowTrailingWhite
let styles =
[| NumberStyles.None; whitespace
NumberStyles.AllowLeadingSign ||| NumberStyles.AllowTrailingSign ||| whitespace
NumberStyles.AllowThousands ||| NumberStyles.AllowCurrencySymbol
NumberStyles.AllowExponent ||| NumberStyles.AllowDecimalPoint |]
// Attempt to convert each number using each style combination.
for value in values do
printfn $"Attempting to convert '{value}':"
for style in styles do
try
let number = UInt32.Parse(value, style)
printfn $" {style}: {number}"
with
| :? FormatException ->
printfn $" {style}: Bad Format"
| :? OverflowException ->
printfn $" {value}: Overflow"
printfn ""
// The example displays the following output:
// Attempting to convert ' 214309 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: 214309
// Integer, AllowTrailingSign: 214309
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '1,064,181':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064181
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '(0)':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '10241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 10241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' + 21499 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' +21499 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 21499
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '122153.00':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 122153
//
// Attempting to convert '1e03ff':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '91300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 913
Imports System.Globalization
Module Example
Public Sub Main()
Dim values() As String = { " 214309 ", "1,064,181", "(0)", "10241+", _
" + 21499 ", " +21499 ", "122153.00", _
"1e03ff", "91300.0e-2" }
Dim whitespace As NumberStyles = NumberStyles.AllowLeadingWhite Or NumberStyles.AllowTrailingWhite
Dim styles() As NumberStyles = { NumberStyles.None, _
whitespace, _
NumberStyles.AllowLeadingSign Or NumberStyles.AllowTrailingSign Or whitespace, _
NumberStyles.AllowThousands Or NumberStyles.AllowCurrencySymbol, _
NumberStyles.AllowExponent Or NumberStyles.AllowDecimalPoint }
' Attempt to convert each number using each style combination.
For Each value As String In values
Console.WriteLine("Attempting to convert '{0}':", value)
For Each style As NumberStyles In styles
Try
Dim number As UInteger = UInt32.Parse(value, style)
Console.WriteLine(" {0}: {1}", style, number)
Catch e As FormatException
Console.WriteLine(" {0}: Bad Format", style)
Catch e As OverflowException
Console.WriteLine(" {0}: Overflow", value)
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Attempting to convert ' 214309 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: 214309
' Integer, AllowTrailingSign: 214309
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '1,064,181':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: 1064181
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '(0)':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '10241+':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: 10241
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert ' + 21499 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert ' +21499 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: 21499
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '122153.00':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 122153
'
' Attempting to convert '1e03ff':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '91300.0e-2':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 913
Kommentarer
Parametern style definierar formatelementen (till exempel blanksteg, symbolen för positiva eller negativa tecken, gruppavgränsningssymbolen eller decimaltecknet) som tillåts i parametern s för att parsningsåtgärden ska lyckas.
style måste vara en kombination av bitflaggor från NumberStyles uppräkningen. Parametern style gör den här metoden användbar när s den innehåller strängrepresentationen av ett hexadecimalt värde, när talsystemet (decimal eller hexadecimalt) som representeras av s endast är känt vid körning, eller när du vill neka tomt utrymme eller en teckensymbol i s.
Beroende på värdet för stylekan parametern s innehålla följande element:
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
Element inom hakparenteser ([ och ]) är valfria. Om style innehåller NumberStyles.AllowHexSpecifierkan parametern s innehålla följande element:
[ws]hexdigits[ws]
I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Valfritt blanksteg. Tomt utrymme kan visas i början av s om style innehåller NumberStyles.AllowLeadingWhite flaggan, och det kan visas i slutet av s om style innehåller NumberStyles.AllowTrailingWhite flaggan. |
| $ | En kulturspecifik valutasymbol. Dess position i strängen definieras av NumberFormatInfo.CurrencyNegativePattern egenskaperna och NumberFormatInfo.CurrencyPositivePattern för den aktuella kulturen. Den aktuella kulturens valutasymbol kan visas i s om style den NumberStyles.AllowCurrencySymbol innehåller flaggan. |
| signera | Ett valfritt tecken. Tecknet kan visas i början av s om style innehåller NumberStyles.AllowLeadingSign flaggan, och det kan visas i slutet av s om style innehåller NumberStyles.AllowTrailingSign flaggan. Parenteser kan användas i s för att ange ett negativt värde om style den NumberStyles.AllowParentheses innehåller flaggan. Den negativa teckensymbolen kan dock endast användas med noll. annars genererar metoden en OverflowException. |
|
Siffror fractional_digits exponential_digits |
En sekvens med siffror från 0 till 9. För fractional_digits är endast siffran 0 giltig. |
| , | En kulturspecifik gruppavgränsare. Den aktuella kulturens gruppavgränsare kan visas i s om style den NumberStyles.AllowThousands innehåller flaggan. |
| . | Ett kulturspecifikt decimaltecken. Den aktuella kulturens decimaltecken kan visas i s om style den NumberStyles.AllowDecimalPoint innehåller flaggan. Endast siffran 0 kan visas som en bråktalssiffra för att parsningsåtgärden ska lyckas. Om fractional_digits innehåller någon annan siffra genereras en FormatException . |
| E | Tecknet "e" eller "E", som anger att värdet representeras i exponentiell (vetenskaplig) notation. Parametern s kan representera ett tal i exponentiell notation om style den NumberStyles.AllowExponent innehåller flaggan. |
| hexdigits | En sekvens med hexadecimala siffror från 0 till f eller 0 till och med F. |
Note
Alla avslutande NUL-tecken (U+0000) i s ignoreras av parsningsåtgärden, oavsett argumentets style värde.
En sträng med endast siffror (vilket motsvarar NumberStyles.None formatet) parsar alltid korrekt om den är i typintervallet UInt32 . De flesta av de återstående NumberStyles medlemmarna styr element som kan finnas, men som inte krävs för att finnas, i indatasträngen. Följande tabell visar hur enskilda NumberStyles medlemmar påverkar de element som kan finnas i s.
NumberStyles värde |
Element som tillåts s utöver siffror |
|---|---|
| None | Endast elementet digits . |
| AllowDecimalPoint | Decimaltecknet (.) och bråksiffriga element. |
| AllowExponent | Tecknet "e" eller "E", som anger exponentiell notation, tillsammans med exponential_digits. |
| AllowLeadingWhite |
WS-elementet i början av s. |
| AllowTrailingWhite |
WS-elementet i slutet av s. |
| AllowLeadingSign |
Teckenelementet i början av s. |
| AllowTrailingSign |
Teckenelementet i slutet av s. |
| AllowParentheses | Teckenelementet i form av parenteser som omger det numeriska värdet. |
| AllowThousands | Gruppavgränsaren (,). |
| AllowCurrencySymbol | Valutaelementet ($). |
| Currency | Alla element. Kan dock s inte representera ett hexadecimalt tal eller ett tal i exponentiell notation. |
| Float |
WS-elementet i början eller slutet av s, signerar i början av soch decimaltecknet (.). Parametern s kan också använda exponentiell notation. |
| Number | Elementen ws, sign, gruppavgränsare (,) och decimaltecken (.). |
| Any | Alla element. Kan dock s inte representera ett hexadecimalt tal. |
Till skillnad från de andra NumberStyles värdena, som tillåter, men inte kräver, innebär förekomsten av vissa formatelement i s, NumberStyles.AllowHexSpecifier att de enskilda numeriska tecknen i s alltid tolkas som hexadecimala tecken. Giltiga hexadecimala tecken är 0-9, A-F och a-f. Ett prefix, till exempel "0x", tillåts inte. De enda andra flaggorna som kan kombineras med parametern style är NumberStyles.AllowLeadingWhite och NumberStyles.AllowTrailingWhite. (Uppräkningen NumberStyles innehåller ett sammansatt talformat, NumberStyles.HexNumber, som innehåller båda blankstegsflaggor.)
De enda andra flaggorna som kan kombineras med parametern style är NumberStyles.AllowLeadingWhite och NumberStyles.AllowTrailingWhite. (Uppräkningen NumberStyles innehåller ett sammansatt talformat, NumberStyles.HexNumber, som innehåller båda blankstegsflaggor.)
Note
Om s är strängrepresentationen av ett hexadecimalt tal kan det inte föregås av någon dekoration (till exempel 0x eller &h) som särskiljer det som ett hexadecimalt tal. Detta gör att konverteringen misslyckas.
Parametern s parsas med hjälp av formateringsinformationen i ett NumberFormatInfo objekt som initieras för den aktuella systemkulturen. Om du vill ange den kultur vars formateringsinformation används för parsningsåtgärden anropar du överlagringen Parse(String, NumberStyles, IFormatProvider) .