UInt16.Parse Methode
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Konvertiert die Zeichenfolgendarstellung einer Zahl in die entsprechende ganze 16-Bit-Zahl ohne Vorzeichen.
Überlädt
Parse(String, NumberStyles, IFormatProvider) |
Konvertiert die Zeichenfolgendarstellung einer Zahl in einem angegebenen Stil und einem kulturabhängigen Format in die entsprechende 16-Bit-Ganzzahl ohne Vorzeichen. |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Konvertiert die Spannendarstellung einer Zahl in einem angegebenen Stil und einem kulturabhängigen Format in die entsprechende 16-Bit-Ganzzahl ohne Vorzeichen. |
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
Analysiert eine Spanne von UTF-8-Zeichen in einen Wert. |
Parse(String, IFormatProvider) |
Konvertiert die Zeichenfolgendarstellung einer Zahl in einem angegebenen kulturspezifischen Format in die entsprechende ganze 16-Bit-Zahl ohne Vorzeichen. |
Parse(String, NumberStyles) |
Konvertiert die Zeichenfolgendarstellung einer Zahl in einem angegebenen Stil in die entsprechende 16-Bit-Ganzzahl ohne Vorzeichen. Diese Methode ist nicht CLS-kompatibel. Die CLS-kompatible Alternative ist Parse(String, NumberStyles). |
Parse(ReadOnlySpan<Char>, IFormatProvider) |
Analysiert eine Spanne von Zeichen in einen Wert. |
Parse(ReadOnlySpan<Byte>, IFormatProvider) |
Analysiert eine Spanne von UTF-8-Zeichen in einen Wert. |
Parse(String) |
Konvertiert die Zeichenfolgendarstellung einer Zahl in die entsprechende ganze 16-Bit-Zahl ohne Vorzeichen. |
Parse(String, NumberStyles, IFormatProvider)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Konvertiert die Zeichenfolgendarstellung einer Zahl in einem angegebenen Stil und einem kulturabhängigen Format in die entsprechende 16-Bit-Ganzzahl ohne Vorzeichen.
public:
static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<System::UInt16>::Parse;
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static ushort Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint16
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As UShort
Parameter
- s
- String
Eine Zeichenfolge, die die zu konvertierende Zahl darstellt. Die Zeichenfolge wird unter Verwendung des durch den style
-Parameter angegebenen Stils interpretiert.
- style
- NumberStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente angeben, die in s
vorhanden sein können. Ein häufig angegebener Wert ist Integer.
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen zu s
bereitstellt.
Gibt zurück
Eine 16-Bit-Ganzzahl ohne Vorzeichen, die der Zahl in s
entspricht.
Implementiert
- Attribute
Ausnahmen
s
ist null
.
style
ist kein NumberStyles-Wert.
- oder -
style
ist keine Kombination von AllowHexSpecifier- und HexNumber-Werten.
s
weist kein mit style
kompatibles Format auf.
s
stellt eine Zahl dar, die kleiner als UInt16.MinValue oder größer als UInt16.MaxValue ist.
- oder -
s
enthält Dezimalstellen ungleich 0 (null).
Beispiele
Im folgenden Beispiel wird die Parse(String, NumberStyles, IFormatProvider) -Methode verwendet, um verschiedene Zeichenfolgendarstellungen von Zahlen in 16-Bit-Ganzzahlwerte ohne Vorzeichen zu konvertieren.
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 = { "1702", "+1702.0", "+1702,0", "-1032.00",
"-1032,00", "1045.1", "1045,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,
UInt16.Parse(value, style, ci));
}
catch (FormatException) {
Console.WriteLine(" Unable to parse '{0}'.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is out of range of the UInt16 type.",
value);
}
}
}
}
}
}
// The example displays the following output:
// Parsing strings using the English (United States) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Converted '+1702.0' to 1702.
// Unable to parse '+1702,0'.
// '-1032.00' is out of range of the UInt16 type.
// Unable to parse '-1032,00'.
// '1045.1' is out of range of the UInt16 type.
// Unable to parse '1045,1'.
// Parsing strings using the French (France) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Converted '+1702,0' to 1702.
// Unable to parse '-1032.00'.
// '-1032,00' is out of range of the UInt16 type.
// Unable to parse '1045.1'.
// '1045,1' is out of range of the UInt16 type.
open System
open System.Globalization
let cultureNames = [| "en-US"; "fr-FR" |]
let styles =
[| NumberStyles.Integer; NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint |]
let values =
[| "1702"; "+1702.0"; "+1702,0"; "-1032.00"; "-1032,00"; "1045.1"; "1045,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 {UInt16.Parse(value, style, ci)}."
with
| :? FormatException ->
printfn $" Unable to parse '{value}'."
| :? OverflowException ->
printfn $" '{value}' is out of range of the UInt16 type."
// The example displays the following output:
// Parsing strings using the English (United States) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Converted '+1702.0' to 1702.
// Unable to parse '+1702,0'.
// '-1032.00' is out of range of the UInt16 type.
// Unable to parse '-1032,00'.
// '1045.1' is out of range of the UInt16 type.
// Unable to parse '1045,1'.
// Parsing strings using the French (France) culture
// Style: Integer
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Unable to parse '+1702,0'.
// Unable to parse '-1032.00'.
// Unable to parse '-1032,00'.
// Unable to parse '1045.1'.
// Unable to parse '1045,1'.
// Style: Integer, AllowDecimalPoint
// Converted '1702' to 1702.
// Unable to parse '+1702.0'.
// Converted '+1702,0' to 1702.
// Unable to parse '-1032.00'.
// '-1032,00' is out of range of the UInt16 type.
// Unable to parse '1045.1'.
// '1045,1' is out of range of the UInt16 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 = { "1702", "+1702.0", "+1702,0", "-1032.00", _
"-1032,00", "1045.1", "1045,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, _
UInt16.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 UInt16 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 '1702' to 1702.
' Unable to parse '+1702.0'.
' Unable to parse '+1702,0'.
' Unable to parse '-1032.00'.
' Unable to parse '-1032,00'.
' Unable to parse '1045.1'.
' Unable to parse '1045,1'.
' Style: Integer, AllowDecimalPoint
' Converted '1702' to 1702.
' Converted '+1702.0' to 1702.
' Unable to parse '+1702,0'.
' '-1032.00' is out of range of the UInt16 type.
' Unable to parse '-1032,00'.
' '1045.1' is out of range of the UInt16 type.
' Unable to parse '1045,1'.
' Parsing strings using the French (France) culture
' Style: Integer
' Converted '1702' to 1702.
' Unable to parse '+1702.0'.
' Unable to parse '+1702,0'.
' Unable to parse '-1032.00'.
' Unable to parse '-1032,00'.
' Unable to parse '1045.1'.
' Unable to parse '1045,1'.
' Style: Integer, AllowDecimalPoint
' Converted '1702' to 1702.
' Unable to parse '+1702.0'.
' Converted '+1702,0' to 1702.
' Unable to parse '-1032.00'.
' '-1032,00' is out of range of the UInt16 type.
' Unable to parse '1045.1'.
' '1045,1' is out of range of the UInt16 type.
Hinweise
Der style
Parameter definiert die Stilelemente (z. B. Leerzeichen oder das positive oder negative Zeichensymbol), die im s
Parameter zulässig sind, damit der Analysevorgang erfolgreich ist. Es muss sich um eine Kombination von Bitflags aus der NumberStyles Enumeration handeln.
Abhängig vom Wert von style
kann der s
Parameter die folgenden Elemente enthalten:
[ws] [$][Zeichen]Ziffern[.fractional_digits][E[sign]exponential_digits][ws]
Elemente in eckigen Klammern ([ und ]) sind optional. Wenn style
enthält NumberStyles.AllowHexSpecifier, kann der s
Parameter die folgenden Elemente enthalten:
[ws] hexdigits[ws]
In der folgenden Tabelle wird jedes Element beschrieben.
Element | Beschreibung |
---|---|
ws | Optionaler Leerraum. Leerzeichen können am Anfang von s angezeigt werden, wenn style das NumberStyles.AllowLeadingWhite Flag enthalten ist, und es kann am Ende von s angezeigt werden, wenn style das NumberStyles.AllowTrailingWhite Flag enthält. |
$ | Ein kulturspezifisches Währungssymbol. Seine Position in der Zeichenfolge wird durch die CurrencyPositivePattern -Eigenschaft des NumberFormatInfo -Objekts definiert, das von der GetFormat -Methode des provider Parameters zurückgegeben wird. Das Währungssymbol kann in s angezeigt werden, wenn style das NumberStyles.AllowCurrencySymbol Flag enthält. |
sign | Ein optionales Zeichen. (Die -Methode löst ein OverflowException aus, wenn s ein negatives Vorzeichen enthält und eine Zahl ungleich 0 darstellt.) Das Zeichen kann am Anfang von s angezeigt werden, wenn style das NumberStyles.AllowLeadingSign Flag enthält, und es kann das Ende von s angezeigt werden, wenn style das NumberStyles.AllowTrailingSign Flag enthält. Klammern können in s verwendet werden, um einen negativen Wert anzugeben, wenn style das NumberStyles.AllowParentheses Flag enthalten ist. |
Zahlen | Eine Sequenz von Ziffern von 0 bis 9. |
. | Ein kulturspezifisches Dezimaltrennzeichen. Das Dezimalzeichensymbol der aktuellen Kultur kann in s angezeigt werden, wenn style das NumberStyles.AllowDecimalPoint Flag enthält. |
fractional_digits | Mindestens ein Vorkommen der Ziffer 0-9, wenn style das NumberStyles.AllowExponent Flag enthalten ist, oder mindestens ein Vorkommen der Ziffer 0, wenn dies nicht der Fall ist. Bruchstellen können nur in s angezeigt werden, wenn style das NumberStyles.AllowDecimalPoint Flag enthalten ist. |
E | Das Zeichen "e" oder "E", das angibt, dass der Wert in exponentieller (wissenschaftlicher) Notation dargestellt wird. Der s Parameter kann eine Zahl in exponentieller Notation darstellen, wenn style das NumberStyles.AllowExponent Flag enthalten ist. |
exponential_digits | Eine Sequenz von Ziffern von 0 bis 9. Der s Parameter kann eine Zahl in exponentieller Notation darstellen, wenn style das NumberStyles.AllowExponent Flag enthalten ist. |
hexdigits | Eine Sequenz von Hexadezimalstellen von 0 bis f oder 0 bis F. |
Hinweis
Alle endenden NUL-Zeichen (U+0000) in s
werden vom Analysevorgang ignoriert, unabhängig vom Wert des style
Arguments.
Eine Zeichenfolge nur mit Dezimalstellen (die der NumberStyles.None Formatvorlage entspricht) analysiert immer erfolgreich. Die meisten der verbleibenden NumberStyles Member steuert Elemente, die in dieser Eingabezeichenfolge möglicherweise vorhanden sind, aber nicht vorhanden sein müssen. Die folgende Tabelle gibt an, wie sich einzelne NumberStyles Member auf die Elemente auswirken, die in s
vorhanden sein können.
Nicht zusammengesetzte NumberStyles Werte |
Zusätzlich zu Ziffern zulässige s Elemente |
---|---|
NumberStyles.None | Nur Dezimalstellen. |
NumberStyles.AllowDecimalPoint | Das Dezimaltrennzeichen (.) und fractional_digits Elemente. Wenn die NumberStyles.AllowExponent Formatvorlage jedoch das Flag nicht enthält, muss fractional_digits nur aus einer oder mehreren 0 Ziffern bestehen. Andernfalls wird eine OverflowException ausgelöst. |
NumberStyles.AllowExponent | Das Zeichen "e" oder "E", das die exponentielle Notation angibt, zusammen mit exponential_digits. |
NumberStyles.AllowLeadingWhite | Das ws-Element am Anfang von s . |
NumberStyles.AllowTrailingWhite | Das ws-Element am Ende von s . |
NumberStyles.AllowLeadingSign | Ein Zeichen vor Ziffern. |
NumberStyles.AllowTrailingSign | Ein Zeichen nach Ziffern. |
NumberStyles.AllowParentheses | Klammern vor und nach Ziffern , um einen negativen Wert anzugeben. |
NumberStyles.AllowThousands | Das Gruppentrennzeichen (,)-Element. |
NumberStyles.AllowCurrencySymbol | Das Währungselement ($). |
Wenn das NumberStyles.AllowHexSpecifier Flag verwendet wird, s
muss ein Hexadezimalwert sein. Gültige Hexadezimalstellen sind 0 bis 9, a bis f und A bis F. Ein Präfix, z. B. "0x", wird nicht unterstützt und führt dazu, dass der Analysevorgang fehlschlägt. Die einzigen anderen Flags, die mit NumberStyles.AllowHexSpecifier kombiniert werden können, sind NumberStyles.AllowLeadingWhite und NumberStyles.AllowTrailingWhite. (Die NumberStyles Enumeration enthält eine zusammengesetzte Zahlenformatvorlage, NumberStyles.HexNumber, die beide Leerzeichenflags enthält.)
Hinweis
Wenn der s
Parameter die Zeichenfolgendarstellung einer Hexadezimalzahl ist, kann ihm keine Dekoration (z 0x
. B. oder &h
) vorangestellt werden, die ihn als hexadezimale Zahl unterscheidet. Dies führt dazu, dass der Analysevorgang eine Ausnahme auslöst.
Der provider
Parameter ist eine IFormatProvider Implementierung, deren GetFormat Methode ein NumberFormatInfo -Objekt zurückgibt, das kulturspezifische Informationen zum Format von s
bereitstellt. Es gibt drei Möglichkeiten, den provider
Parameter zu verwenden, um benutzerdefinierte Formatierungsinformationen für den Analysevorgang anzugeben:
Sie können das tatsächliche NumberFormatInfo Objekt übergeben, das Formatierungsinformationen bereitstellt. (Seine Implementierung von GetFormat gibt sich einfach selbst zurück.)
Sie können ein CultureInfo -Objekt übergeben, das die Kultur angibt, deren Formatierung verwendet werden soll. Die - NumberFormat Eigenschaft stellt Formatierungsinformationen bereit.
Sie können eine benutzerdefinierte IFormatProvider Implementierung übergeben. Die GetFormat -Methode muss das -Objekt instanziieren und zurückgeben, das NumberFormatInfo Formatierungsinformationen bereitstellt.
Wenn provider
ist null
, wird das NumberFormatInfo -Objekt für die aktuelle Kultur verwendet.
Weitere Informationen
Gilt für:
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Wichtig
Diese API ist nicht CLS-kompatibel.
Konvertiert die Spannendarstellung einer Zahl in einem angegebenen Stil und einem kulturabhängigen Format in die entsprechende 16-Bit-Ganzzahl ohne Vorzeichen.
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
[System.CLSCompliant(false)]
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
[System.CLSCompliant(false)]
public static ushort Parse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint16
[<System.CLSCompliant(false)>]
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As UShort
Parameter
- s
- ReadOnlySpan<Char>
Eine Zeichenspanne, die die zu konvertierende Zahl darstellt Die Zeichenspanne wird unter Verwendung des durch den style
-Parameter angegebenen Stils interpretiert.
- style
- NumberStyles
Eine bitweise Kombination von Enumerationswerten, die die Stilelemente angeben, die in s
vorhanden sein können. Ein häufig angegebener Wert ist Integer.
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen zu s
bereitstellt.
Gibt zurück
Eine 16-Bit-Ganzzahl ohne Vorzeichen, die der Zahl in s
entspricht.
Implementiert
- Attribute
Gilt für:
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Analysiert eine Spanne von UTF-8-Zeichen in einen Wert.
public static ushort Parse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> uint16
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As UShort
Parameter
- utf8Text
- ReadOnlySpan<Byte>
Die Spanne der zu analysierenden UTF-8-Zeichen.
- style
- NumberStyles
Eine bitweise Kombination von Zahlenformatvorlagen, die in utf8Text
vorhanden sein können.
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen zu utf8Text
bereitstellt.
Gibt zurück
Das Ergebnis der Analyse utf8Text
von .
Implementiert
Gilt für:
Parse(String, IFormatProvider)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Konvertiert die Zeichenfolgendarstellung einer Zahl in einem angegebenen kulturspezifischen Format in die entsprechende ganze 16-Bit-Zahl ohne Vorzeichen.
public:
static System::UInt16 Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static System::UInt16 Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<System::UInt16>::Parse;
[System.CLSCompliant(false)]
public static ushort Parse (string s, IFormatProvider provider);
public static ushort Parse (string s, IFormatProvider? provider);
[System.CLSCompliant(false)]
public static ushort Parse (string s, IFormatProvider? provider);
[<System.CLSCompliant(false)>]
static member Parse : string * IFormatProvider -> uint16
static member Parse : string * IFormatProvider -> uint16
Public Shared Function Parse (s As String, provider As IFormatProvider) As UShort
Parameter
- s
- String
Eine Zeichenfolge, die die zu konvertierende Zahl darstellt.
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen zu s
bereitstellt.
Gibt zurück
Eine 16-Bit-Ganzzahl ohne Vorzeichen, die der Zahl in s
entspricht.
Implementiert
- Attribute
Ausnahmen
s
ist null
.
s
weist nicht das richtige Format auf.
s
stellt eine Zahl dar, die kleiner als UInt16.MinValue oder größer als UInt16.MaxValue ist.
Beispiele
Im folgenden Beispiel wird eine benutzerdefinierte Kultur instanziiert, die zwei Pluszeichen (++) als positives Vorzeichen verwendet. Anschließend wird die Parse(String, IFormatProvider) -Methode aufgerufen, um ein Array von Zeichenfolgen mithilfe CultureInfo von -Objekten zu analysieren, die sowohl diese benutzerdefinierte Kultur als auch die invariante Kultur darstellen.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
// Define a custom culture that uses "++" as a positive sign.
CultureInfo ci = new CultureInfo("");
ci.NumberFormat.PositiveSign = "++";
// Create an array of cultures.
CultureInfo[] cultures = { ci, CultureInfo.InvariantCulture };
// Create an array of strings to parse.
string[] values = { "++1403", "-0", "+0", "+16034",
Int16.MinValue.ToString(), "14.0", "18012" };
// Parse the strings using each culture.
foreach (CultureInfo culture in cultures)
{
Console.WriteLine("Parsing with the '{0}' culture.", culture.Name);
foreach (string value in values)
{
try {
ushort number = UInt16.Parse(value, culture);
Console.WriteLine(" Converted '{0}' to {1}.", value, number);
}
catch (FormatException) {
Console.WriteLine(" The format of '{0}' is invalid.", value);
}
catch (OverflowException) {
Console.WriteLine(" '{0}' is outside the range of a UInt16 value.", value);
}
}
}
}
}
// The example displays the following output:
// Parsing with the culture.
// Converted '++1403' to 1403.
// Converted '-0' to 0.
// The format of '+0' is invalid.
// The format of '+16034' is invalid.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
// Parsing with the '' culture.
// The format of '++1403' is invalid.
// Converted '-0' to 0.
// Converted '+0' to 0.
// Converted '+16034' to 16034.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
open System
open System.Globalization
// Define a custom culture that uses "++" as a positive sign.
let ci = CultureInfo ""
ci.NumberFormat.PositiveSign <- "++"
// Create an array of cultures.
let cultures = [| ci; CultureInfo.InvariantCulture |]
// Create an array of strings to parse.
let values =
[| "++1403"; "-0"; "+0"; "+16034"
string Int16.MinValue; "14.0"; "18012" |]
// Parse the strings using each culture.
for culture in cultures do
printfn $"Parsing with the '{culture.Name}' culture."
for value in values do
try
let number = UInt16.Parse(value, culture)
printfn $" Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $" The format of '{value}' is invalid."
| :? OverflowException ->
printfn $" '{value}' is outside the range of a UInt16 value."
// The example displays the following output:
// Parsing with the culture.
// Converted '++1403' to 1403.
// Converted '-0' to 0.
// The format of '+0' is invalid.
// The format of '+16034' is invalid.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
// Parsing with the '' culture.
// The format of '++1403' is invalid.
// Converted '-0' to 0.
// Converted '+0' to 0.
// Converted '+16034' to 16034.
// '-32768' is outside the range of a UInt16 value.
// The format of '14.0' is invalid.
// Converted '18012' to 18012.
Imports System.Globalization
Module Example
Public Sub Main()
' Define a custom culture that uses "++" as a positive sign.
Dim ci As CultureInfo = New CultureInfo("")
ci.NumberFormat.PositiveSign = "++"
' Create an array of cultures.
Dim cultures() As CultureInfo = { ci, CultureInfo.InvariantCulture }
' Create an array of strings to parse.
Dim values() As String = { "++1403", "-0", "+0", "+16034", _
Int16.MinValue.ToString(), "14.0", "18012" }
' Parse the strings using each culture.
For Each culture As CultureInfo In cultures
Console.WriteLine("Parsing with the '{0}' culture.", culture.Name)
For Each value As String In values
Try
Dim number As UShort = UInt16.Parse(value, culture)
Console.WriteLine(" Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine(" The format of '{0}' is invalid.", value)
Catch e As OverflowException
Console.WriteLine(" '{0}' is outside the range of a UInt16 value.", value)
End Try
Next
Next
End Sub
End Module
' The example displays the following output:
' Parsing with the culture.
' Converted '++1403' to 1403.
' Converted '-0' to 0.
' The format of '+0' is invalid.
' The format of '+16034' is invalid.
' '-32768' is outside the range of a UInt16 value.
' The format of '14.0' is invalid.
' Converted '18012' to 18012.
' Parsing with the '' culture.
' The format of '++1403' is invalid.
' Converted '-0' to 0.
' Converted '+0' to 0.
' Converted '+16034' to 16034.
' '-32768' is outside the range of a UInt16 value.
' The format of '14.0' is invalid.
' Converted '18012' to 18012.
Hinweise
Der s
Parameter enthält eine Nummer des Formulars:
[ws] [sign] digits[ws]
Elemente in eckigen Klammern ([ und ]) sind optional. In der folgenden Tabelle wird jedes Element beschrieben.
Element | BESCHREIBUNG |
---|---|
ws | Optionaler Leerraum. |
Signieren | Ein optionales Zeichen oder ein negatives Zeichen, wenn s den Wert 0 (null) darstellt. |
Zahlen | Eine Sequenz von Ziffern im Bereich von 0 bis 9. |
Der s-Parameter wird mithilfe des Stils NumberStyles.Integer interpretiert. Zusätzlich zu den Dezimalstellen des Bytewerts sind nur führende und nachfolgende Leerzeichen zusammen mit einem führenden Zeichen zulässig. (Wenn das negative Vorzeichen vorhanden ist, muss ein Wert von null dargestellt werden, s
oder die Methode löst einen ausOverflowException.) Verwenden Sie die -Methode, um die Stilelemente explizit zusammen mit den kulturspezifischen Formatierungsinformationen zu definieren, die Parse(String, NumberStyles, IFormatProvider) in s
vorhanden sein können.
Der provider
Parameter ist eine IFormatProvider Implementierung, deren GetFormat Methode ein NumberFormatInfo -Objekt zurückgibt, das kulturspezifische Informationen zum Format von s
bereitstellt. Es gibt drei Möglichkeiten, den provider
Parameter zu verwenden, um benutzerdefinierte Formatierungsinformationen für den Analysevorgang anzugeben:
Sie können das tatsächliche NumberFormatInfo Objekt übergeben, das Formatierungsinformationen bereitstellt. (Seine Implementierung von GetFormat gibt sich einfach selbst zurück.)
Sie können ein CultureInfo -Objekt übergeben, das die Kultur angibt, deren Formatierung verwendet werden soll. Die - NumberFormat Eigenschaft stellt Formatierungsinformationen bereit.
Sie können eine benutzerdefinierte IFormatProvider Implementierung übergeben. Die GetFormat -Methode muss das -Objekt instanziieren und zurückgeben, das NumberFormatInfo Formatierungsinformationen bereitstellt.
Wenn provider
ist null
, wird für NumberFormatInfo die aktuelle Kultur verwendet.
Weitere Informationen
Gilt für:
Parse(String, NumberStyles)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Wichtig
Diese API ist nicht CLS-kompatibel.
Konvertiert die Zeichenfolgendarstellung einer Zahl in einem angegebenen Stil in die entsprechende 16-Bit-Ganzzahl ohne Vorzeichen.
Diese Methode ist nicht CLS-kompatibel. Die CLS-kompatible Alternative ist Parse(String, NumberStyles).
public:
static System::UInt16 Parse(System::String ^ s, System::Globalization::NumberStyles style);
[System.CLSCompliant(false)]
public static ushort Parse (string s, System.Globalization.NumberStyles style);
public static ushort Parse (string s, System.Globalization.NumberStyles style);
[<System.CLSCompliant(false)>]
static member Parse : string * System.Globalization.NumberStyles -> uint16
static member Parse : string * System.Globalization.NumberStyles -> uint16
Public Shared Function Parse (s As String, style As NumberStyles) As UShort
Parameter
- s
- String
Eine Zeichenfolge, die die zu konvertierende Zahl darstellt. Die Zeichenfolge wird unter Verwendung des durch den style
-Parameter angegebenen Stils interpretiert.
- style
- NumberStyles
Eine bitweise Kombination der Enumerationswerte, die das zulässige Format von s
angeben. Ein häufig angegebener Wert ist Integer.
Gibt zurück
Eine 16-Bit-Ganzzahl ohne Vorzeichen, die der Zahl in s
entspricht.
- Attribute
Ausnahmen
s
ist null
.
style
ist kein NumberStyles-Wert.
- oder -
style
ist keine Kombination von AllowHexSpecifier- und HexNumber-Werten.
s
weist kein mit style
kompatibles Format auf.
s
stellt eine Zahl dar, die kleiner als UInt16.MinValue oder größer als UInt16.MaxValue ist.
- oder -
s
enthält Dezimalstellen ungleich 0 (null).
Beispiele
Im folgenden Beispiel wird versucht, jedes Element in einem Zeichenfolgenarray mithilfe einer Reihe von NumberStyles Werten zu analysieren.
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string[] values = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.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 {
ushort number = UInt16.Parse(value, style);
Console.WriteLine(" {0}: {1}", style, number);
}
catch (FormatException) {
Console.WriteLine(" {0}: Bad Format", style);
}
}
Console.WriteLine();
}
}
}
// The example display the following output:
// Attempting to convert ' 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: 214
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '1,064':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064
// 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 '1241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 1241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' + 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' +214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '2153.0':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 2153
//
// Attempting to convert '1e03':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 1000
//
// Attempting to convert '1300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 13
open System
open System.Globalization
let values = [| " 214 "; "1,064"; "(0)"; "1241+"; " + 214 "; " +214 "; "2153.0"; "1e03"; "1300.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 = UInt16.Parse(value, style)
printfn $" {style}: {number}"
with :? FormatException ->
printfn $" {style}: Bad Format"
printfn ""
// The example display the following output:
// Attempting to convert ' 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: 214
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '1,064':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: 1064
// 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 '1241+':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 1241
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' + 214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert ' +214 ':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: 214
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: Bad Format
//
// Attempting to convert '2153.0':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 2153
//
// Attempting to convert '1e03':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 1000
//
// Attempting to convert '1300.0e-2':
// None: Bad Format
// AllowLeadingWhite, AllowTrailingWhite: Bad Format
// Integer, AllowTrailingSign: Bad Format
// AllowThousands, AllowCurrencySymbol: Bad Format
// AllowDecimalPoint, AllowExponent: 13
Imports System.Globalization
Module Example
Public Sub Main()
Dim values() As String = { " 214 ", "1,064", "(0)", "1241+", " + 214 ", " +214 ", "2153.0", "1e03", "1300.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 UShort = UInt16.Parse(value, style)
Console.WriteLine(" {0}: {1}", style, number)
Catch e As FormatException
Console.WriteLine(" {0}: Bad Format", style)
End Try
Next
Console.WriteLine()
Next
End Sub
End Module
' The example displays the following output:
' Attempting to convert ' 214 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: 214
' Integer, AllowTrailingSign: 214
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '1,064':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: 1064
' 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 '1241+':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: 1241
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert ' + 214 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert ' +214 ':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: 214
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: Bad Format
'
' Attempting to convert '2153.0':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 2153
'
' Attempting to convert '1e03':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 1000
'
' Attempting to convert '1300.0e-2':
' None: Bad Format
' AllowLeadingWhite, AllowTrailingWhite: Bad Format
' Integer, AllowTrailingSign: Bad Format
' AllowThousands, AllowCurrencySymbol: Bad Format
' AllowDecimalPoint, AllowExponent: 13
Hinweise
Der style
Parameter definiert die Stilelemente (z. B. Leerzeichen, positives oder negatives Vorzeichen, Gruppentrennzeichensymbol oder Dezimaltrennzeichen), die im s
Parameter zulässig sind, damit der Analysevorgang erfolgreich ist. style
muss eine Kombination von Bitflags aus der NumberStyles Enumeration sein. Der style
-Parameter macht diese Methodenüberladung nützlich, wenn s
die Zeichenfolgendarstellung eines Hexadezimalwerts enthält, wenn das Zahlensystem (dezimal oder hexadezimal) nur s
zur Laufzeit bekannt ist oder wenn Sie Leerzeichen oder ein Zeichensymbol in s
nicht zulassen möchten.
Abhängig vom Wert von style
kann der s
Parameter die folgenden Elemente enthalten:
[ws] [$][Zeichen][Ziffern,]Ziffern[.fractional_digits][E[sign]exponential_digits][ws]
Elemente in eckigen Klammern ([ und ]) sind optional. Wenn style
enthält NumberStyles.AllowHexSpecifier, kann der s
Parameter die folgenden Elemente enthalten:
[ws] hexdigits[ws]
In der folgenden Tabelle wird jedes Element beschrieben.
Element | Beschreibung |
---|---|
ws | Optionaler Leerraum. Leerzeichen können am Anfang von s angezeigt werden, wenn style das NumberStyles.AllowLeadingWhite Flag enthalten ist, und es kann am Ende von s angezeigt werden, wenn style das NumberStyles.AllowTrailingWhite Flag enthält. |
$ | Ein kulturspezifisches Währungssymbol. Seine Position in der Zeichenfolge wird durch die NumberFormatInfo.CurrencyNegativePattern Eigenschaften und NumberFormatInfo.CurrencyPositivePattern der aktuellen Kultur definiert. Das Währungssymbol der aktuellen Kultur kann in s angezeigt werden, wenn style das NumberStyles.AllowCurrencySymbol Flag enthält. |
sign | Ein optionales Zeichen. Das Zeichen kann am Anfang von s angezeigt werden, wenn style das NumberStyles.AllowLeadingSign Flag enthält, und es kann am Ende von s angezeigt werden, wenn style das NumberStyles.AllowTrailingSign Flag enthält. Klammern können in s verwendet werden, um einen negativen Wert anzugeben, wenn style das NumberStyles.AllowParentheses Flag enthalten ist. Das negative Vorzeichensymbol kann jedoch nur mit 0 (null) verwendet werden. Andernfalls löst die -Methode eine aus OverflowException. |
Zahlen fractional_digits exponential_digits |
Eine Sequenz von Ziffern von 0 bis 9. Für fractional_digits ist nur die Ziffer 0 gültig. |
, | Ein kulturspezifisches Gruppentrennzeichensymbol. Das Gruppentrennzeichen der aktuellen Kultur kann in s angezeigt werden, wenn style das NumberStyles.AllowThousands Flag enthält. |
. | Ein kulturspezifisches Dezimaltrennzeichen. Das Dezimalzeichensymbol der aktuellen Kultur kann in s angezeigt werden, wenn style das NumberStyles.AllowDecimalPoint Flag enthält. Nur die Ziffer 0 kann als Bruchziffer angezeigt werden, damit der Analysevorgang erfolgreich ist. wenn fractional_digits eine andere Ziffer enthält, wird eine FormatException ausgelöst. |
E | Das Zeichen "e" oder "E", das angibt, dass der Wert in exponentieller (wissenschaftlicher) Notation dargestellt wird. Der s Parameter kann eine Zahl in exponentieller Notation darstellen, wenn style das NumberStyles.AllowExponent Flag enthalten ist. |
hexdigits | Eine Sequenz von Hexadezimalstellen von 0 bis f oder 0 bis F. |
Hinweis
Alle endenden NUL-Zeichen (U+0000) in s
werden vom Analysevorgang ignoriert, unabhängig vom Wert des style
Arguments.
Eine Zeichenfolge nur mit Ziffern (die der NumberStyles.None Formatvorlage entspricht) analysiert immer erfolgreich, wenn sie sich im Bereich des UInt16 Typs befindet. Die meisten der verbleibenden NumberStyles Member steuern Elemente, die in der Eingabezeichenfolge möglicherweise vorhanden sind, aber nicht vorhanden sein müssen. Die folgende Tabelle gibt an, wie sich einzelne NumberStyles Member auf die Elemente auswirken, die in s
vorhanden sein können.
Wert vom Typ NumberStyles |
Zusätzlich zu Ziffern zulässige s Elemente |
---|---|
None | Nur das Digits-Element . |
AllowDecimalPoint | Das Dezimaltrennzeichen (.) und die Bruchstellenelemente . |
AllowExponent | Das Zeichen "e" oder "E", das die exponentielle Notation angibt, zusammen mit exponential_digits. |
AllowLeadingWhite | Das ws-Element am Anfang von s . |
AllowTrailingWhite | Das ws-Element am Ende von s . |
AllowLeadingSign | Das Zeichenelement am Anfang von s . |
AllowTrailingSign | Das Zeichenelement am Ende von s . |
AllowParentheses | Das Zeichenelement in Form von Klammern, die den numerischen Wert einschließen. |
AllowThousands | Das Gruppentrennzeichen (,)-Element. |
AllowCurrencySymbol | Das Währungselement ($). |
Currency | Alle Elemente. Kann jedoch s keine hexadezimale Zahl oder eine Zahl in exponentieller Notation darstellen. |
Float | Das ws-Element am Anfang oder Ende von s , signieren am Anfang von s und das Dezimaltrennzeichen (.). Der s Parameter kann auch exponentielle Notation verwenden. |
Number | Die ws Elemente , , sign Gruppierungstrennzeichen (,) und Dezimalstellen (.). |
Any | Alle Elemente. Kann jedoch s keine Hexadezimalzahl darstellen. |
Im Gegensatz zu den anderen NumberStyles Werten, die bestimmte Stilelemente in s
zulassen, aber nicht erfordern, bedeutet der NumberStyles.AllowHexSpecifier Formatvorlagenwert, dass die einzelnen numerischen Zeichen in s
immer als Hexadezimalzeichen interpretiert werden. Gültige Hexadezimalzeichen sind 0-9, A-F und a-f. Ein Präfix, z. B. "0x", wird nicht unterstützt und führt dazu, dass der Analysevorgang fehlschlägt. Die einzigen anderen Flags, die mit dem style
-Parameter kombiniert werden können, sind NumberStyles.AllowLeadingWhite und NumberStyles.AllowTrailingWhite. (Die NumberStyles Enumeration enthält eine zusammengesetzte Zahlenformatvorlage, NumberStyles.HexNumber, die beide Leerzeichenflags enthält.)
Hinweis
Wenn s
die Zeichenfolgendarstellung einer Hexadezimalzahl ist, kann ihr keine Dekoration (z 0x
. B. oder &h
) vorangestellt werden, die sie als hexadezimale Zahl unterscheidet. Dies führt dazu, dass die Konvertierung fehlschlägt.
Der s
Parameter wird mithilfe der Formatierungsinformationen in einem NumberFormatInfo Objekt analysiert, das für die aktuelle Systemkultur initialisiert wird. Rufen Sie die -Überladung auf, um die Kultur anzugeben, deren Formatierungsinformationen für den Parse(String, NumberStyles, IFormatProvider) Analysevorgang verwendet werden.
Weitere Informationen
Gilt für:
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Analysiert eine Spanne von Zeichen in einen Wert.
public:
static System::UInt16 Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<System::UInt16>::Parse;
public static ushort Parse (ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> uint16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As UShort
Parameter
- s
- ReadOnlySpan<Char>
Die Spanne der zu analysierenden Zeichen.
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen zu s
bereitstellt.
Gibt zurück
Das Ergebnis der Analyse s
von .
Implementiert
Gilt für:
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Analysiert eine Spanne von UTF-8-Zeichen in einen Wert.
public:
static System::UInt16 Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<System::UInt16>::Parse;
public static ushort Parse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> uint16
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As UShort
Parameter
- utf8Text
- ReadOnlySpan<Byte>
Die Spanne der zu analysierenden UTF-8-Zeichen.
- provider
- IFormatProvider
Ein Objekt, das kulturspezifische Formatierungsinformationen zu utf8Text
bereitstellt.
Gibt zurück
Das Ergebnis der Analyse utf8Text
von .
Implementiert
Gilt für:
Parse(String)
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
- Quelle:
- UInt16.cs
Konvertiert die Zeichenfolgendarstellung einer Zahl in die entsprechende ganze 16-Bit-Zahl ohne Vorzeichen.
public:
static System::UInt16 Parse(System::String ^ s);
[System.CLSCompliant(false)]
public static ushort Parse (string s);
public static ushort Parse (string s);
[<System.CLSCompliant(false)>]
static member Parse : string -> uint16
static member Parse : string -> uint16
Public Shared Function Parse (s As String) As UShort
Parameter
- s
- String
Eine Zeichenfolge, die die zu konvertierende Zahl darstellt.
Gibt zurück
Eine 16-Bit-Ganzzahl ohne Vorzeichen, die der Zahl in s
entspricht.
- Attribute
Ausnahmen
s
ist null
.
s
weist nicht das richtige Format auf.
s
stellt eine Zahl dar, die kleiner als UInt16.MinValue oder größer als UInt16.MaxValue ist.
Beispiele
Im folgenden Beispiel wird die Parse(String) -Methode aufgerufen, um jedes Element in einem Zeichenfolgenarray in eine 16-Bit-Ganzzahl ohne Vorzeichen zu konvertieren.
using System;
public class Example
{
public static void Main()
{
string[] values = { "-0", "17", "-12", "185", "66012", "+0",
"", null, "16.1", "28.0", "1,034" };
foreach (string value in values)
{
try {
ushort number = UInt16.Parse(value);
Console.WriteLine("'{0}' --> {1}", value, number);
}
catch (FormatException) {
Console.WriteLine("'{0}' --> Bad Format", value);
}
catch (OverflowException) {
Console.WriteLine("'{0}' --> OverflowException", value);
}
catch (ArgumentNullException) {
Console.WriteLine("'{0}' --> Null", value);
}
}
}
}
// The example displays the following output:
// '-0' --> 0
// '17' --> 17
// '-12' --> OverflowException
// '185' --> 185
// '66012' --> OverflowException
// '+0' --> 0
// '' --> Bad Format
// '' --> Null
// '16.1' --> Bad Format
// '28.0' --> Bad Format
// '1,034' --> Bad Format
open System
let values =
[| "-0"; "17"; "-12"; "185"; "66012"; "+0"
""; null; "16.1"; "28.0"; "1,034" |]
for value in values do
try
let number = UInt16.Parse value
printfn $"'{value}' --> {number}"
with
| :? FormatException ->
printfn $"'{value}' --> Bad Format"
| :? OverflowException ->
printfn $"'{value}' --> OverflowException"
| :? ArgumentNullException ->
printfn $"'{value}' --> Null"
// The example displays the following output:
// '-0' --> 0
// '17' --> 17
// '-12' --> OverflowException
// '185' --> 185
// '66012' --> OverflowException
// '+0' --> 0
// '' --> Bad Format
// '' --> Null
// '16.1' --> Bad Format
// '28.0' --> Bad Format
// '1,034' --> Bad Format
Module Example
Public Sub Main()
Dim values() As String = { "-0", "17", "-12", "185", "66012", _
"+0", "", Nothing, "16.1", "28.0", _
"1,034" }
For Each value As String In values
Try
Dim number As UShort = UInt16.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}' --> OverflowException", value)
Catch e As ArgumentNullException
Console.WriteLine("'{0}' --> Null", value)
End Try
Next
End Sub
End Module
' The example displays the following output:
' '-0' --> 0
' '17' --> 17
' '-12' --> OverflowException
' '185' --> 185
' '66012' --> OverflowException
' '+0' --> 0
' '' --> Bad Format
' '' --> Null
' '16.1' --> Bad Format
' '28.0' --> Bad Format
' '1,034' --> Bad Format
Hinweise
Der s
Parameter sollte die Zeichenfolgendarstellung einer Zahl im folgenden Format sein.
[ws] [sign] digits[ws]
Elemente in eckigen Klammern ([ und ]) sind optional. In der folgenden Tabelle wird jedes Element beschrieben.
Element | Beschreibung |
---|---|
ws | Optionaler Leerraum. |
sign | Ein optionales Zeichen. Gültige Vorzeichen werden durch die NumberFormatInfo.NegativeSign Eigenschaften und NumberFormatInfo.PositiveSign der aktuellen Kultur bestimmt. Das negative Vorzeichensymbol kann jedoch nur mit 0 (null) verwendet werden. Andernfalls löst die -Methode eine aus OverflowException. |
Zahlen | Eine Sequenz von Ziffern im Bereich von 0 bis 9. Alle führenden Nullen werden ignoriert. |
Hinweis
Die durch den s
-Parameter angegebene Zeichenfolge wird mithilfe des Stils NumberStyles.Integer interpretiert. Sie kann keine Gruppentrennzeichen oder Dezimaltrennzeichen enthalten und keinen Dezimalteil haben.
Der s
Parameter wird mithilfe der Formatierungsinformationen in einem System.Globalization.NumberFormatInfo Objekt analysiert, das für die aktuelle Systemkultur initialisiert wird. Weitere Informationen finden Sie unter NumberFormatInfo.CurrentInfo. Verwenden Parse(String, IFormatProvider) Sie die -Methode, um eine Zeichenfolge mithilfe der Formatierungsinformationen einer bestimmten Kultur zu analysieren.
Weitere Informationen
Gilt für:
Feedback
https://aka.ms/ContentUserFeedback.
Bald verfügbar: Im Laufe des Jahres 2024 werden wir GitHub-Issues stufenweise als Feedbackmechanismus für Inhalte abbauen und durch ein neues Feedbacksystem ersetzen. Weitere Informationen finden Sie unterFeedback senden und anzeigen für