Int16.TryParse Metoda
Definicja
Konwertuje ciąg reprezentujący liczbę na odpowiadającą jej 16-bitową liczbę całkowitą ze znakiem.Converts the string representation of a number to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed.
Przeciążenia
TryParse(String, Int16) |
Konwertuje ciąg reprezentujący liczbę na odpowiadającą jej 16-bitową liczbę całkowitą ze znakiem.Converts the string representation of a number to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed. |
TryParse(ReadOnlySpan<Char>, Int16) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na jego 16-bitową liczbę całkowitą ze znakiem.Converts the span representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed. |
TryParse(String, NumberStyles, IFormatProvider, Int16) |
Konwertuje ciąg reprezentujący liczbę w określonym stylu i formacie specyficznym dla kultury na jego 16-bitową liczbę całkowitą ze znakiem.Converts the string representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed. |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na jego 16-bitową liczbę całkowitą ze znakiem.Converts the span representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed. |
TryParse(String, Int16)
Konwertuje ciąg reprezentujący liczbę na odpowiadającą jej 16-bitową liczbę całkowitą ze znakiem.Converts the string representation of a number to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed.
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (string s, out short result);
public static bool TryParse (string? s, out short? result);
static member TryParse : string * int16 -> bool
Public Shared Function TryParse (s As String, ByRef result As Short) As Boolean
Parametry
- s
- String
Ciąg zawierający liczbę, która ma zostać przekształcona.A string containing a number to convert.
- result
- Int16
Gdy ta metoda zwraca, zawiera 16-bitową liczbę całkowitą ze znakiem, odpowiadającą liczbie zawartej w s
, jeśli konwersja zakończyła się powodzeniem, lub zero, jeśli konwersja nie powiodła się.When this method returns, contains the 16-bit signed integer value equivalent to the number contained in s
, if the conversion succeeded, or zero if the conversion failed. Konwersja nie powiedzie się s
, jeśli parametr ma wartość null
lub Empty , ma niepoprawny format lub reprezentuje liczbę mniejszą MinValue lub równą MaxValue .The conversion fails if the s
parameter is null
or Empty, is not of the correct format, or represents a number less than MinValue or greater than MaxValue. Ten parametr jest przekazaniem niezainicjowanym; Każda wartość pokazana w programie result
zostanie nadpisywana.This parameter is passed uninitialized; any value originally supplied in result
will be overwritten.
Zwraca
true
Jeśli s
został przekonwertowany pomyślnie; w przeciwnym razie, false
.true
if s
was converted successfully; otherwise, false
.
Przykłady
Poniższy przykład wywołuje Int16.TryParse(String, Int16) metodę z liczbą różnych wartości ciągu.The following example calls the Int16.TryParse(String, Int16) method with a number of different string values.
using System;
public class StringParsing
{
public static void Main()
{
TryToParse(null);
TryToParse("16051");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
private static void TryToParse(string value)
{
short number;
bool result = Int16.TryParse(value, out number);
if (result)
{
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
else
{
if (value == null) value = "";
Console.WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
}
// The example displays the following output to the console:
// Attempted conversion of '' failed.
// Converted '16051' to 16051.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100)' failed.
// Attempted conversion of '01FA' failed.
Module StringParsing
Public Sub Main()
TryToParse(Nothing)
TryToParse("16051")
TryToParse("9432.0")
TryToParse("16,667")
TryToParse(" -322 ")
TryToParse("+4302")
TryToParse("(100)")
TryToParse("01FA")
End Sub
Private Sub TryToParse(value As String)
Dim number As Int16
Dim result As Boolean = Int16.TryParse(value, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
If value Is Nothing Then value = ""
Console.WriteLine("Attempted conversion of '{0}' failed.", value)
End If
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '' failed.
' Converted '16051' to 16051.
' Attempted conversion of '9432.0' failed.
' Attempted conversion of '16,667' failed.
' Converted ' -322 ' to -322.
' Converted '+4302' to 4302.
' Attempted conversion of '(100)' failed.
' Attempted conversion of '01FA' failed.
Niektóre ciągi, których TryParse(String, Int16) Metoda nie może konwertować w tym przykładzie:Some of the strings that the TryParse(String, Int16) method is unable to convert in this example are:
"9432,0"."9432.0". Konwersja nie powiodła się, ponieważ ciąg nie może zawierać separatora dziesiętnego; musi zawierać tylko cyfry całkowite.The conversion fails because the string cannot contain a decimal separator; it must contain integral digits only.
"16 667"."16,667". Konwersja nie powiodła się, ponieważ ciąg nie może zawierać separatorów grup; musi zawierać tylko cyfry całkowite.The conversion fails because the string cannot contain group separators; it must contain integral digits only.
"(100)"."(100)". Konwersja nie powiodła się, ponieważ ciąg nie może zawierać znaku ujemnego innego niż określony przez bieżącą kulturę NumberFormatInfo.NegativeSign i NumberFormatInfo.NumberNegativePattern właściwości.The conversion fails because the string cannot contain a negative sign other than the one defined by the current culture's NumberFormatInfo.NegativeSign and NumberFormatInfo.NumberNegativePattern properties.
"01FA"."01FA". Konwersja nie powiodła się, ponieważ ciąg nie może zawierać cyfr szesnastkowych; musi zawierać tylko cyfry dziesiętne.The conversion fails because the string cannot contain hexadecimal digits; it must contain decimal digits only.
Uwagi
Int16.TryParse(String, Int16)Metoda różni się od Int16.Parse(String) metody przez zwrócenie wartości logicznej wskazującej, czy operacja analizy zakończyła się powodzeniem, zamiast zwracać przeanalizowanej Int16 wartości.The Int16.TryParse(String, Int16) method differs from the Int16.Parse(String) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed Int16 value. Eliminuje to konieczność użycia obsługi wyjątków do testowania dla FormatException zdarzenia, które s
jest nieprawidłowe i nie można go pomyślnie przeanalizować.It eliminates the need to use exception handling to test for a FormatException in the event that s
is invalid and cannot be successfully parsed.
s
Parametr powinien być ciągiem reprezentującym liczbę w postaci:The s
parameter should be the string representation of a number in the form:
[odstęp][znak]cyfry[odstęp][ws][sign]digits[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne.Items in square brackets ([ and ]) are optional. W tabeli poniżej opisano każdy element.The following table describes each element.
ElementElement | OpisDescription |
---|---|
wsws | Opcjonalny odstęp.Optional white space. |
signsign | Opcjonalny znak.An optional sign. |
cyfrydigits | Sekwencja cyfr od 0 do 9.A sequence of digits ranging from 0 to 9. |
s
Parametr jest interpretowany przy użyciu NumberStyles.Integer stylu.The s
parameter is interpreted using the NumberStyles.Integer style. Oprócz cyfr dziesiętnych dozwolone są tylko spacje wiodące i końcowe razem z wiodącym znakiem.In addition to the decimal digits, only leading and trailing spaces together with a leading sign are allowed. Aby jawnie zdefiniować elementy stylu wraz z informacjami o formatowaniu specyficznym dla kultury, które mogą być obecne w s
, użyj Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) metody.To explicitly define the style elements together with the culture-specific formatting information that can be present in s
, use the Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) method.
s
Parametr jest analizowany przy użyciu informacji o formatowaniu w NumberFormatInfo obiekcie, który jest zainicjowany dla bieżącej kultury systemowej.The s
parameter is parsed using the formatting information in a NumberFormatInfo object that is initialized for the current system culture. Aby uzyskać więcej informacji, zobacz CurrentInfo.For more information, see CurrentInfo.
To Przeciążenie TryParse metody interpretuje wszystkie cyfry w s
parametrze jako cyfry dziesiętne.This overload of the TryParse method interprets all digits in the s
parameter as decimal digits. Aby przeanalizować ciąg reprezentujący liczbę szesnastkową, wywołaj Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) Przeciążenie.To parse the string representation of a hexadecimal number, call the Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) overload.
Zobacz też
- Parse(String)
- ToString()
- Analizowanie ciągów liczbowych w programie .NETParsing Numeric Strings in .NET
- Przykład: Narzędzie formatowania programu .NET Core WinForms (C#)Sample: .NET Core WinForms Formatting Utility (C#)
- Przykład: Narzędzie formatowania programu .NET Core WinForms (Visual Basic)Sample: .NET Core WinForms Formatting Utility (Visual Basic)
Dotyczy
TryParse(ReadOnlySpan<Char>, Int16)
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na jego 16-bitową liczbę całkowitą ze znakiem.Converts the span representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed.
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<char> s, out short result);
static member TryParse : ReadOnlySpan<char> * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Short) As Boolean
Parametry
- s
- ReadOnlySpan<Char>
Zakres zawierający znaki reprezentujące liczbę do przekonwertowania.A span containing the characters representing the number to convert.
- result
- Int16
Gdy ta metoda zwraca, zawiera 16-bitową liczbę całkowitą ze znakiem, odpowiadającą liczbie zawartej w s
, jeśli konwersja zakończyła się powodzeniem, lub zero, jeśli konwersja nie powiodła się.When this method returns, contains the 16-bit signed integer value equivalent to the number contained in s
, if the conversion succeeded, or zero if the conversion failed. Konwersja nie powiedzie się, jeśli s
parametr ma wartość null
lub Empty , nie jest w formacie zgodnym z lub style
reprezentuje liczbę mniejszą MinValue lub równą MaxValue .The conversion fails if the s
parameter is null
or Empty, is not in a format compliant with style
, or represents a number less than MinValue or greater than MaxValue. Ten parametr jest przekazaniem niezainicjowanym; Każda wartość pokazana w programie result
zostanie nadpisywana.This parameter is passed uninitialized; any value originally supplied in result
will be overwritten.
Zwraca
true
Jeśli s
został przekonwertowany pomyślnie; w przeciwnym razie, false
.true
if s
was converted successfully; otherwise, false
.
Dotyczy
TryParse(String, NumberStyles, IFormatProvider, Int16)
Konwertuje ciąg reprezentujący liczbę w określonym stylu i formacie specyficznym dla kultury na jego 16-bitową liczbę całkowitą ze znakiem.Converts the string representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed.
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short? result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
Parametry
- s
- String
Ciąg zawierający liczbę, która ma zostać przekształcona.A string containing a number to convert. Ciąg jest interpretowany przy użyciu stylu określonego przez style
.The string is interpreted using the style specified by style
.
- style
- NumberStyles
Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w s
.A bitwise combination of enumeration values that indicates the style elements that can be present in s
. Typową wartością do określenia jest Integer .A typical value to specify is Integer.
- provider
- IFormatProvider
Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury s
.An object that supplies culture-specific formatting information about s
.
- result
- Int16
Gdy ta metoda zwraca, zawiera 16-bitową liczbę całkowitą ze znakiem, odpowiadającą liczbie zawartej w s
, jeśli konwersja zakończyła się powodzeniem, lub zero, jeśli konwersja nie powiodła się.When this method returns, contains the 16-bit signed integer value equivalent to the number contained in s
, if the conversion succeeded, or zero if the conversion failed. Konwersja nie powiedzie się, jeśli s
parametr ma wartość null
lub Empty , nie jest w formacie zgodnym z lub style
reprezentuje liczbę mniejszą MinValue lub równą MaxValue .The conversion fails if the s
parameter is null
or Empty, is not in a format compliant with style
, or represents a number less than MinValue or greater than MaxValue. Ten parametr jest przekazaniem niezainicjowanym; Każda wartość pokazana w programie result
zostanie nadpisywana.This parameter is passed uninitialized; any value originally supplied in result
will be overwritten.
Zwraca
true
Jeśli s
został przekonwertowany pomyślnie; w przeciwnym razie, false
.true
if s
was converted successfully; otherwise, false
.
Wyjątki
style
nie jest NumberStyles wartością.style
is not a NumberStyles value.
-lub--or-
style
nie jest kombinacją AllowHexSpecifier wartości i HexNumber .style
is not a combination of AllowHexSpecifier and HexNumber values.
Przykłady
Poniższy przykład wywołuje Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) metodę z liczbą różnych wartości ciągu.The following example calls the Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) method with a number of different string values.
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "10677";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
short number;
bool result = Int16.TryParse(stringToConvert, styles,
CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
else
Console.WriteLine("Attempted conversion of '{0}' failed.",
Convert.ToString(stringToConvert));
}
}
// The example displays the following output to the console:
// Converted '10677' to 10677.
// Attempted conversion of '-30677' failed.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to -32575.
// Attempted conversion of '0x80C1' failed.
Imports System.Globalization
Module StringParsing
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "10677"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-30677"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "10345.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "22,593"
styles = NumberStyles.Integer Or NumberStyles.AllowThousands
CallTryParse(numericString, styles)
numericString = "12E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "12E03"
CallTryParse(numericString, styles)
numericString = "80c1"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x80C1"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As Short
Dim result As Boolean = Int16.TryParse(stringToConvert, styles, _
CultureInfo.InvariantCulture, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.", _
Convert.ToString(stringToConvert))
End If
End Sub
End Module
' The example displays the following output to the console:
' Converted '10677' to 10677.
' Attempted conversion of '-30677' failed.
' Converted '10345.00' to 10345.
' Attempted conversion of '10345.72' failed.
' Converted '22,593' to 22593.
' Attempted conversion of '12E-01' failed.
' Converted '12E03' to 12000.
' Converted '80c1' to -32575.
' Attempted conversion of '0x80C1' failed.
Uwagi
Int16.TryParse(String, NumberStyles, IFormatProvider, Int16)Metoda różni się od Int16.Parse(String, NumberStyles, IFormatProvider) metody przez zwrócenie wartości logicznej wskazującej, czy operacja analizy zakończyła się powodzeniem, zamiast zwracać przeanalizowanej Int16 wartości.The Int16.TryParse(String, NumberStyles, IFormatProvider, Int16) method differs from the Int16.Parse(String, NumberStyles, IFormatProvider) method by returning a Boolean value that indicates whether the parse operation succeeded instead of returning the parsed Int16 value. Eliminuje to konieczność użycia obsługi wyjątków do testowania dla FormatException zdarzenia, które s
jest nieprawidłowe i nie można go pomyślnie przeanalizować.It eliminates the need to use exception handling to test for a FormatException in the event that s
is invalid and cannot be successfully parsed.
style
Parametr definiuje elementy stylu (takie jak odstępy czy znak dodatni lub ujemny), które są dozwolone w s
parametrze dla operacji analizy.The style
parameter defines the style elements (such as white space or a positive or negative sign) that are allowed in the s
parameter for the parse operation to succeed. Musi to być kombinacja flag bitowych z NumberStyles wyliczenia.It must be a combination of bit flags from the NumberStyles enumeration. W zależności od wartości stylu s
parametr może zawierać następujące elementy:Depending on the value of style, the s
parameter may include the following elements:
[odstęp][$][znak][cyfry,]cyfry[.cyfry_ułamkowe][e[znak]cyfry][odstęp][ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
Lub, jeśli style
parametr zawiera NumberStyles.AllowHexSpecifier :Or, if the style
parameter includes NumberStyles.AllowHexSpecifier:
[odstęp]cyfry_szesnastkowe[odstęp][ws]hexdigits[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne.Items in square brackets ([ and ]) are optional. W tabeli poniżej opisano każdy element.The following table describes each element.
ElementElement | OpisDescription |
---|---|
wsws | Opcjonalny odstęp.Optional white space. Biały znak może pojawić się na początku s style , jeśli zawiera NumberStyles.AllowLeadingWhite flagę lub na końcu elementu s if style zawiera NumberStyles.AllowTrailingWhite flagę.White space can appear at the beginning of s if style includes the NumberStyles.AllowLeadingWhite flag, or at the end of s if style includes the NumberStyles.AllowTrailingWhite flag. |
$ | Symbol waluty specyficzny dla kultury.A culture-specific currency symbol. Jego pozycja w ciągu jest definiowana przez CurrencyPositivePattern Właściwość NumberFormatInfo obiektu zwracanego przez GetFormat metodę provider parametru.Its position in the string is defined by the CurrencyPositivePattern property of the NumberFormatInfo object returned by the GetFormat method of the provider parameter. Symbol waluty może pojawić się s w style , jeśli zawiera NumberStyles.AllowCurrencySymbol flagę.The currency symbol can appear in s if style includes the NumberStyles.AllowCurrencySymbol flag. |
signsign | Opcjonalny znak.An optional sign. |
cyfrydigits | Sekwencja cyfr od 0 do 9.A sequence of digits from 0 through 9. |
,, | Separator tysięcy specyficzny dla kultury.A culture-specific thousands separator. Separator tysięcy kultury określonej przez provider może pojawić się w parametrze s if style zawiera NumberStyles.AllowThousands flagę.The thousands separator of the culture specified by provider can appear in s if style includes the NumberStyles.AllowThousands flag. |
.. | Symbol dziesiętny specyficzny dla kultury.A culture-specific decimal point symbol. Symbol separatora dziesiętnego kultury określonej przez provider może pojawić się w parametrze s if style zawiera NumberStyles.AllowDecimalPoint flagę.The decimal point symbol of the culture specified by provider can appear in s if style includes the NumberStyles.AllowDecimalPoint flag. |
fractional_digitsfractional_digits | Jedno lub więcej wystąpień cyfry 0.One or more occurrences of the digit 0. Cyfry ułamkowe mogą pojawiać się s tylko wtedy style , gdy zawierają NumberStyles.AllowDecimalPoint flagę.Fractional digits can appear in s only if style includes the NumberStyles.AllowDecimalPoint flag. |
adrese | Znak „e” lub „E”, który wskazuje, że wartość jest reprezentowana w zapisie wykładniczym.The 'e' or 'E' character, which indicates that the value is represented in exponential notation. s Parametr może reprezentować liczbę w notacji wykładniczej style , jeśli zawiera NumberStyles.AllowExponent flagę.The s parameter can represent a number in exponential notation if style includes the NumberStyles.AllowExponent flag. |
hexdigitshexdigits | Sekwencja cyfr szesnastkowych od 0 do f lub od 0 do F.A sequence of hexadecimal digits from 0 through f, or 0 through F. |
Uwaga
Wszelkie kończące się znaki NUL (U + 0000) w programie s
są ignorowane przez operację analizowania, niezależnie od wartości style
argumentu.Any terminating NUL (U+0000) characters in s
are ignored by the parsing operation, regardless of the value of the style
argument.
Ciąg z tylko cyframi dziesiętnymi (które odnosi się do NumberStyles.None flagi) zawsze analizuje się prawidłowo.A string with decimal digits only (which corresponds to the NumberStyles.None flag) always parses successfully. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, ale nie muszą być obecne w tym ciągu wejściowym.Most of the remaining NumberStyles members control elements that may be but are not required to be present in this input string. Poniższa tabela zawiera informacje o tym, w jaki sposób poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w s
.The following table indicates how individual NumberStyles members affect the elements that may be present in s
.
Niezłożone wartości wyliczenia NumberStylesNon-composite NumberStyles values | Dodatkowe (poza cyframi) elementy dozwolone w parametrze sElements permitted in s in addition to digits |
---|---|
NumberStyles.None | Tylko cyfry dziesiętne.Decimal digits only. |
NumberStyles.AllowDecimalPoint | Element .The . elementy fractional_digits i.and fractional_digits elements. Jednak fractional_digits muszą zawierać tylko jedną lub więcej cyfr 0 lub metodę zwracaną false .However, fractional_digits must consist of only one or more 0 digits or the method returns false . |
NumberStyles.AllowExponent | s Parametr może również używać notacji wykładniczej.The s parameter can also use exponential notation. Jeśli s reprezentuje numer w notacji wykładniczej, musi reprezentować liczbę całkowitą w zakresie Int16 typu danych bez składnika niezerowego.If s represents a number in exponential notation, it must represent an integer within the range of the Int16 data type without a non-zero fractional component. |
NumberStyles.AllowLeadingWhite | Element WS na początku s.The ws element at the beginning of s. |
NumberStyles.AllowTrailingWhite | Element WS na końcu s.The ws element at the end of s. |
NumberStyles.AllowLeadingSign | Znak może występować przed cyframi.A sign can appear before digits. |
NumberStyles.AllowTrailingSign | Znak może występować po cyfrach.A sign can appear after digits. |
NumberStyles.AllowParentheses | Element Sign w postaci nawiasów otaczających wartość liczbową.The sign element in the form of parentheses enclosing the numeric value. |
NumberStyles.AllowThousands | Element , .The , element. |
NumberStyles.AllowCurrencySymbol | $ Element.The $ element. |
NumberStyles.Currency | Wszystkie elementy.All elements. Parametr s nie może reprezentować liczby szesnastkowej ani liczby w notacji wykładniczej.The s parameter cannot represent a hexadecimal number or a number in exponential notation. |
NumberStyles.Float | Element WS na początku lub na końcu s , znak na początku s i .The ws element at the beginning or end of s , sign at the beginning of s , and the . symboliczn.symbol. s Parametr może również używać notacji wykładniczej.The s parameter can also use exponential notation. |
NumberStyles.Number | Elementy WS, Sign, separator tysięcy (,) i punkt dziesiętny (.).The ws, sign, thousands separator (,), and decimal point (.) elements. |
NumberStyles.Any | Wszystkie style, z wyjątkiem s nie mogą reprezentować liczby szesnastkowej.All styles, except s cannot represent a hexadecimal number. |
Jeśli NumberStyles.AllowHexSpecifier flaga jest używana, s
musi być wartością szesnastkową bez prefiksu.If the NumberStyles.AllowHexSpecifier flag is used, s
must be a hexadecimal value without a prefix. Na przykład "9AF3" pomyślnie Analizuje, ale "0x9AF3" nie.For example, "9AF3" parses successfully, but "0x9AF3" does not. Jedyne inne flagi, które mogą być obecne w style
to NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite .The only other flags that can be present in style
are NumberStyles.AllowLeadingWhite and NumberStyles.AllowTrailingWhite. ( NumberStyles Wyliczenie ma styl złożony, NumberStyles.HexNumber który zawiera obie flagi odstępu).(The NumberStyles enumeration has a composite style, NumberStyles.HexNumber, that includes both white space flags.)
provider
Parametr jest IFormatProvider implementacją, taką jak CultureInfo obiekt lub NumberFormatInfo obiekt, którego GetFormat Metoda zwraca NumberFormatInfo obiekt.The provider
parameter is an IFormatProvider implementation, such as a CultureInfo object or a NumberFormatInfo object, whose GetFormat method returns a NumberFormatInfo object. NumberFormatInfoObiekt zawiera informacje specyficzne dla kultury o formacie s
.The NumberFormatInfo object provides culture-specific information about the format of s
. Jeśli provider
jest null
, NumberFormatInfo używany jest obiekt dla bieżącej kultury.If provider
is null
, the NumberFormatInfo object for the current culture is used.
Zobacz też
- Parse
- NumberStyles
- ToString()
- Analizowanie ciągów liczbowych w programie .NETParsing Numeric Strings in .NET
Dotyczy
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int16)
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na jego 16-bitową liczbę całkowitą ze znakiem.Converts the span representation of a number in a specified style and culture-specific format to its 16-bit signed integer equivalent. Zwracana wartość wskazuje, czy konwersja powiodła się czy nie.A return value indicates whether the conversion succeeded or failed.
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] short % result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out short result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out short result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * int16 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Short) As Boolean
Parametry
- s
- ReadOnlySpan<Char>
Zakres zawierający znaki reprezentujące liczbę do przekonwertowania.A span containing the characters representing the number to convert. Zakres jest interpretowany przy użyciu stylu określonego przez style
.The span is interpreted using the style specified by style
.
- style
- NumberStyles
Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w s
.A bitwise combination of enumeration values that indicates the style elements that can be present in s
. Typową wartością do określenia jest Integer .A typical value to specify is Integer.
- provider
- IFormatProvider
Obiekt, który dostarcza informacje o formatowaniu specyficzne dla kultury s
.An object that supplies culture-specific formatting information about s
.
- result
- Int16
Gdy ta metoda zwraca, zawiera 16-bitową liczbę całkowitą ze znakiem, odpowiadającą liczbie zawartej w s
, jeśli konwersja zakończyła się powodzeniem, lub zero, jeśli konwersja nie powiodła się.When this method returns, contains the 16-bit signed integer value equivalent to the number contained in s
, if the conversion succeeded, or zero if the conversion failed. Konwersja nie powiedzie się, jeśli s
parametr ma wartość null
lub Empty , nie jest w formacie zgodnym z lub style
reprezentuje liczbę mniejszą MinValue lub równą MaxValue .The conversion fails if the s
parameter is null
or Empty, is not in a format compliant with style
, or represents a number less than MinValue or greater than MaxValue. Ten parametr jest przekazaniem niezainicjowanym; Każda wartość pokazana w programie result
zostanie nadpisywana.This parameter is passed uninitialized; any value originally supplied in result
will be overwritten.
Zwraca
true
Jeśli s
został przekonwertowany pomyślnie; w przeciwnym razie, false
.true
if s
was converted successfully; otherwise, false
.