Int16.Parse Metoda
Definicja
Ważne
Niektóre informacje odnoszą się do produktu w wersji wstępnej, który może zostać znacząco zmodyfikowany przed wydaniem. Firma Microsoft nie udziela żadnych gwarancji, jawnych lub domniemanych, w odniesieniu do informacji podanych w tym miejscu.
Konwertuje reprezentację ciągu liczby na 16-bitową liczbę całkowitą ze znakiem.
Przeciążenia
| Nazwa | Opis |
|---|---|
| Parse(String, NumberStyles, IFormatProvider) |
Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na 16-bitową liczbę całkowitą ze znakiem. |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na 16-bitową liczbę całkowitą ze znakiem. |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
Analizuje zakres znaków UTF-8 w wartość. |
| Parse(String, IFormatProvider) |
Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na 16-bitową liczbę całkowitą ze znakiem. |
| Parse(String) |
Konwertuje reprezentację ciągu liczby na 16-bitową liczbę całkowitą ze znakiem. |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
Analizuje zakres znaków w wartości. |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
Analizuje zakres znaków UTF-8 w wartość. |
| Parse(String, NumberStyles) |
Konwertuje reprezentację ciągu liczby w określonym stylu na 16-bitową liczbę całkowitą ze znakiem. |
Parse(String, NumberStyles, IFormatProvider)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na 16-bitową liczbę całkowitą ze znakiem.
public:
static short Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
static short Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<short>::Parse;
public static short Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static short Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> int16
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Short
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
- style
- NumberStyles
Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w elemecie s. Typową wartością do określenia jest Integer.
- provider
- IFormatProvider
Element IFormatProvider , który dostarcza informacje dotyczące formatowania specyficznego dla kultury na temat selementu .
Zwraca
16-bitowa liczba całkowita ze znakiem równoważna liczbie określonej w elemencie s.
Implementuje
Wyjątki
Parametr s ma wartość null.
style nie jest wartością NumberStyles .
-lub-
style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .
s nie jest w formacie zgodnym z style.
s reprezentuje liczbę mniejszą niż Int16.MinValue lub większa niż Int16.MaxValue.
-lub-
s zawiera cyfry ułamkowe niezerowe.
Przykłady
W poniższym przykładzie użyto różnych parametrów style i provider , aby przeanalizować reprezentacje ciągów Int16 wartości.
string value;
short number;
NumberStyles style;
CultureInfo provider;
// Parse string using "." as the thousands separator
// and " " as the decimal separator.
value = "19 694,00";
style = NumberStyles.AllowDecimalPoint | NumberStyles.AllowThousands;
provider = new CultureInfo("fr-FR");
number = Int16.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '19 694,00' converted to 19694.
try
{
number = Int16.Parse(value, style, CultureInfo.InvariantCulture);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '19 694,00'.
// Parse string using "$" as the currency symbol for en_GB and
// en-US cultures.
value = "$6,032.00";
style = NumberStyles.Number | NumberStyles.AllowCurrencySymbol;
provider = new CultureInfo("en-GB");
try
{
number = Int16.Parse(value, style, CultureInfo.InvariantCulture);
Console.WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '$6,032.00'.
provider = new CultureInfo("en-US");
number = Int16.Parse(value, style, provider);
Console.WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '$6,032.00' converted to 6032.
// Parse string using "." as the thousands separator
// and " " as the decimal separator.
let value = "19 694,00"
let style = NumberStyles.AllowDecimalPoint ||| NumberStyles.AllowThousands
let provider = CultureInfo "fr-FR"
let number = Int16.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
// '19 694,00' converted to 19694.
try
let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '19 694,00'.
// Parse string using "$" as the currency symbol for en_GB and
// en-US cultures.
let value = "$6,032.00"
let style = NumberStyles.Number ||| NumberStyles.AllowCurrencySymbol
try
let number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
printfn $"'{value}' converted to {number}."
with :? FormatException ->
printfn $"Unable to parse '{value}'."
// Displays:
// Unable to parse '$6,032.00'.
let provider = CultureInfo "en-US"
let number = Int16.Parse(value, style, provider)
printfn $"'{value}' converted to {number}."
// Displays:
// '$6,032.00' converted to 6032.
Dim value As String
Dim number As Short
Dim style As NumberStyles
Dim provider As CultureInfo
' Parse string using "." as the thousands separator
' and " " as the decimal separator.
value = "19 694,00"
style = NumberStyles.AllowDecimalPoint Or NumberStyles.AllowThousands
provider = New CultureInfo("fr-FR")
number = Int16.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '19 694,00' converted to 19694.
Try
number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '19 694,00'.
' Parse string using "$" as the currency symbol for en_GB and
' en-US cultures.
value = "$6,032.00"
style = NumberStyles.Number Or NumberStyles.AllowCurrencySymbol
provider = New CultureInfo("en-GB")
Try
number = Int16.Parse(value, style, CultureInfo.InvariantCulture)
Console.WriteLine("'{0}' converted to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", value)
End Try
' Displays:
' Unable to parse '$6,032.00'.
provider = New CultureInfo("en-US")
number = Int16.Parse(value, style, provider)
Console.WriteLine("'{0}' converted to {1}.", value, number)
' Displays:
' '$6,032.00' converted to 6032.
Uwagi
Parametr style definiuje elementy stylu (takie jak biały znak lub znak dodatni), które są dozwolone w parametrze s , aby operacja analizy zakończyła się powodzeniem. Musi to być kombinacja flag bitowych z NumberStyles wyliczenia. W zależności od wartości styleparametr może s zawierać następujące elementy:
[ws] [$] [podpis] [cyfry,]cyfry[.fractional_digits][e[znak]cyfry][ws]
Lub, jeśli style obejmuje AllowHexSpecifier:
[ws]hexdigits[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W poniższej tabeli opisano każdy element.
| Pierwiastek | Opis |
|---|---|
| Ws | Opcjonalne białe znaki. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite lub na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingWhite . |
| $ | Symbol waluty specyficznej dla kultury. Jego pozycja w ciągu jest definiowana przez NumberFormatInfo.CurrencyPositivePattern właściwości i NumberFormatInfo.CurrencyNegativePattern bieżącej kultury. Symbol waluty bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol . |
| podpis | Opcjonalny znak. Znak może pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingSign i może pojawić się na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingSign . Nawiasy mogą służyć do wskazywania wartości ujemnej, s jeśli style zawiera flagę NumberStyles.AllowParentheses . |
| Cyfr | Sekwencja cyfr z zakresu od 0 do 9. |
| , | Symbol separatora tysięcy specyficzny dla kultury. Symbol separatora tysięcy bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowThousands . |
| . | Symbol separatora dziesiętnego specyficznego dla kultury. Symbol dziesiętny bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . |
| fractional_digits | Sekwencja cyfry 0. Cyfry ułamkowe mogą być wyświetlane, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . Jeśli w fractional_digits pojawi się dowolna cyfra inna niż 0, metoda zgłasza błąd OverflowException. |
| e | Znak "e" lub "E", który wskazuje, że s może być reprezentowany w notacji wykładniczej. Parametr s może reprezentować liczbę w notacji wykładniczej, jeśli style zawiera flagę NumberStyles.AllowExponent .
s Jednak parametr musi reprezentować liczbę w zakresie Int16 typu danych i nie może mieć składnika ułamkowego innego niż zero. |
| hexdigits | Sekwencja cyfr szesnastkowa od 0 do f lub od 0 do F. |
Nuta
Wszystkie znaki NUL (U+0000) w obiekcie s są ignorowane przez operację analizowania, niezależnie od wartości argumentu style .
Ciąg zawierający tylko cyfry (odpowiadające NumberStyles.None stylowi) zawsze analizuje się pomyślnie. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, ale nie muszą być obecne w tym ciągu wejściowym. W poniższej tabeli przedstawiono, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w programie s.
| Nieskładne wartości NumberStyles | Elementy dozwolone w s oprócz cyfr |
|---|---|
| NumberStyles.None | Tylko cyfry dziesiętne. |
| NumberStyles.AllowDecimalPoint | Elementy . i fractional_digits . Jednak fractional_digits musi składać się tylko z co najmniej jednej cyfry lub OverflowException jest zgłaszana. |
| NumberStyles.AllowExponent | Parametr s może również używać notacji wykładniczej. |
| NumberStyles.AllowLeadingWhite | Element ws na początku elementu s. |
| NumberStyles.AllowTrailingWhite | Element ws na końcu elementu s. |
| NumberStyles.AllowLeadingSign | Znak może pojawić się przed cyframi. |
| NumberStyles.AllowTrailingSign | Znak może pojawić się po cyfrach. |
| NumberStyles.AllowParentheses | Element znaku w postaci nawiasów ujętą w wartość liczbową. |
| NumberStyles.AllowThousands | Element , . |
| NumberStyles.AllowCurrencySymbol | Element $ . |
Jeśli flaga NumberStyles.AllowHexSpecifier jest używana, s musi być ciągiem reprezentującym wartość szesnastkową bez prefiksu. Na przykład "9AF3" analizuje się pomyślnie, ale "0x9AF3" nie. Jedynymi innymi flagami, które mogą być obecne w systemie style , są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles ma styl liczby złożonej, NumberStyles.HexNumber, który zawiera obie flagi odstępu).
Parametr provider jest implementacją IFormatProvider , której GetFormat metoda uzyskuje NumberFormatInfo obiekt. Obiekt NumberFormatInfo zawiera informacje specyficzne dla kultury dotyczące formatu s. Jeśli provider ma nullwartość , NumberFormatInfo używany jest obiekt bieżącej kultury.
Zobacz też
Dotyczy
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na 16-bitową liczbę całkowitą ze znakiem.
public static short Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static short Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> int16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Short
Parametry
- s
- ReadOnlySpan<Char>
Zakres zawierający znaki reprezentujące liczbę do przekonwertowania.
- style
- NumberStyles
Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w elemecie s. Typową wartością do określenia jest Integer.
- provider
- IFormatProvider
Element IFormatProvider , który dostarcza informacje dotyczące formatowania specyficznego dla kultury na temat selementu .
Zwraca
16-bitowa liczba całkowita ze znakiem równoważna liczbie określonej w elemencie s.
Implementuje
Dotyczy
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Analizuje zakres znaków UTF-8 w wartość.
public static short Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> int16
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Short
Parametry
- utf8Text
- ReadOnlySpan<Byte>
Zakres znaków UTF-8 do przeanalizowania.
- style
- NumberStyles
Bitowa kombinacja stylów liczbowych, które mogą być obecne w pliku utf8Text.
- provider
- IFormatProvider
Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text
Zwraca
Wynik analizowania utf8Text.
Implementuje
Dotyczy
Parse(String, IFormatProvider)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Konwertuje reprezentację ciągu liczby w określonym formacie specyficznym dla kultury na 16-bitową liczbę całkowitą ze znakiem.
public:
static short Parse(System::String ^ s, IFormatProvider ^ provider);
public:
static short Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<short>::Parse;
public static short Parse(string s, IFormatProvider provider);
public static short Parse(string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> int16
Public Shared Function Parse (s As String, provider As IFormatProvider) As Short
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
- provider
- IFormatProvider
Element IFormatProvider , który dostarcza informacje dotyczące formatowania specyficznego dla kultury na temat selementu .
Zwraca
16-bitowa liczba całkowita ze znakiem równoważna liczbie określonej w elemencie s.
Implementuje
Wyjątki
Parametr s ma wartość null.
s nie jest w poprawnym formacie.
s reprezentuje liczbę mniejszą niż Int16.MinValue lub większa niż Int16.MaxValue.
Przykłady
Poniższy przykład analizuje reprezentacje ciągów Int16 wartości za pomocą Int16.Parse(String, IFormatProvider) metody .
string stringToConvert;
short number;
stringToConvert = " 214 ";
try
{
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
Console.WriteLine("'{0'} is out of range of the Int16 data type.",
stringToConvert);
}
stringToConvert = " + 214";
try
{
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
Console.WriteLine("'{0'} is out of range of the Int16 data type.",
stringToConvert);
}
stringToConvert = " +214 ";
try
{
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture);
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException)
{
Console.WriteLine("'{0'} is out of range of the Int16 data type.",
stringToConvert);
}
// The example displays the following output to the console:
// Converted ' 214 ' to 214.
// Unable to parse ' + 214'.
// Converted ' +214 ' to 214.
let stringToConvert = " 214 "
try
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is out of range of the Int16 data type."
let stringToConvert = " + 214"
try
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is out of range of the Int16 data type."
let stringToConvert = " +214 "
try
let number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
printfn $"Converted '{stringToConvert}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{stringToConvert}'."
| :? OverflowException ->
printfn $"'{stringToConvert}' is out of range of the Int16 data type."
// The example displays the following output to the console:
// Converted ' 214 ' to 214.
// Unable to parse ' + 214'.
// Converted ' +214 ' to 214.
Dim stringToConvert As String
Dim number As Short
stringToConvert = " 214 "
Try
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
stringToConvert)
End Try
stringToConvert = " + 214"
Try
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
stringToConvert)
End Try
stringToConvert = " +214 "
Try
number = Int16.Parse(stringToConvert, CultureInfo.InvariantCulture)
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}'.", stringToConvert)
Catch e As OverflowException
Console.WriteLine("'{0'} is out of range of the Int16 data type.", _
stringToConvert)
End Try
' The example displays the following output to the console:
' Converted ' 214 ' to 214.
' Unable to parse ' + 214'.
' Converted ' +214 ' to 214.
Uwagi
Parametr s zawiera liczbę formularzy:
[ws] [znak]cyfry[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W poniższej tabeli opisano każdy element.
| Pierwiastek | Opis |
|---|---|
| Ws | Opcjonalny biały znak. |
| znak | Opcjonalny znak. |
| Cyfr | Sekwencja cyfr od 0 do 9. |
Parametr s jest interpretowany przy użyciu NumberStyles.Integer stylu. Oprócz cyfr dziesiętnych dozwolone są tylko spacje wiodące i końcowe wraz z znakiem wiodącym.s Aby jawnie zdefiniować elementy stylu wraz z informacjami o formatowaniu specyficznymi dla kultury, które mogą być obecne w sprogramie , użyj Int16.Parse(String, NumberStyles, IFormatProvider) metody .
Parametr provider jest implementacją IFormatProvider , która uzyskuje NumberFormatInfo obiekt. Element NumberFormatInfo zawiera informacje specyficzne dla kultury dotyczące formatu s. Jeśli provider wartość to null, NumberFormatInfo używana jest wartość dla bieżącej kultury.
Zobacz też
Dotyczy
Parse(String)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Konwertuje reprezentację ciągu liczby na 16-bitową liczbę całkowitą ze znakiem.
public:
static short Parse(System::String ^ s);
public static short Parse(string s);
static member Parse : string -> int16
Public Shared Function Parse (s As String) As Short
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
Zwraca
16-bitowa liczba całkowita ze znakiem równoważna liczbie zawartej w elemencie s.
Wyjątki
Parametr s ma wartość null.
s nie jest w poprawnym formacie.
s reprezentuje liczbę mniejszą niż Int16.MinValue lub większa niż Int16.MaxValue.
Przykłady
W poniższym przykładzie pokazano, jak przekonwertować wartość ciągu na 16-bitową wartość całkowitą ze znakiem Int16.Parse(String) przy użyciu metody . Wynikowa wartość całkowita jest następnie wyświetlana w konsoli programu .
string value;
short number;
value = " 12603 ";
try
{
number = Int16.Parse(value);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.",
value);
}
value = " 16,054";
try
{
number = Int16.Parse(value);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.",
value);
}
value = " -17264";
try
{
number = Int16.Parse(value);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.",
value);
}
// The example displays the following output to the console:
// Converted ' 12603 ' to 12603.
// Unable to convert ' 16,054' to a 16-bit signed integer.
// Converted ' -17264' to -17264.
let value = " 12603 "
try
let number = Int16.Parse value
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn $"Unable to convert '{value}' to a 16-bit signed integer."
let value = " 16,054"
try
let number = Int16.Parse value
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn "Unable to convert '{value}' to a 16-bit signed integer."
let value = " -17264"
try
let number = Int16.Parse value
printfn $"Converted '{value}' to {number}."
with :? FormatException ->
printfn "Unable to convert '{value}' to a 16-bit signed integer."
// The example displays the following output to the console:
// Converted ' 12603 ' to 12603.
// Unable to convert ' 16,054' to a 16-bit signed integer.
// Converted ' -17264' to -17264.
Dim value As String
Dim number As Short
value = " 12603 "
Try
number = Short.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
value)
End Try
value = " 16,054"
Try
number = Short.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
value)
End Try
value = " -17264"
Try
number = Short.Parse(value)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to convert '{0}' to a 16-bit signed integer.", _
value)
End Try
' The example displays the following output to the console:
' Converted ' 12603 ' to 12603.
' Unable to convert ' 16,054' to a 16-bit signed integer.
' Converted ' -17264' to -17264.
Uwagi
Parametr s zawiera liczbę formularzy:
[ws] [znak]cyfry[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W poniższej tabeli opisano każdy element.
| Pierwiastek | Opis |
|---|---|
| Ws | Opcjonalne białe znaki. |
| podpis | Opcjonalny znak. |
| Cyfr | Sekwencja cyfr od 0 do 9. |
Parametr s jest interpretowany przy użyciu NumberStyles.Integer stylu. Oprócz cyfr dziesiętnych wartości całkowitej dozwolone są tylko spacje wiodące i końcowe wraz z znakiem wiodącym. Aby jawnie zdefiniować elementy stylu, które mogą być obecne w sprogramie , użyj Int16.Parse(String, NumberStyles) metody lub Parse .
Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie zainicjowanym NumberFormatInfo dla bieżącej kultury systemu. Aby uzyskać więcej informacji, zobacz CurrentInfo. Aby przeanalizować ciąg przy użyciu informacji o formatowaniu innej kultury, użyj Int16.Parse(String, IFormatProvider) metody lub Int16.Parse(String, NumberStyles, IFormatProvider) .
Zobacz też
Dotyczy
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Analizuje zakres znaków w wartości.
public:
static short Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<short>::Parse;
public static short Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> int16
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Short
Parametry
- s
- ReadOnlySpan<Char>
Zakres znaków do przeanalizowania.
- provider
- IFormatProvider
Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.s
Zwraca
Wynik analizowania s.
Implementuje
Dotyczy
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Analizuje zakres znaków UTF-8 w wartość.
public:
static short Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<short>::Parse;
public static short Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> int16
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Short
Parametry
- utf8Text
- ReadOnlySpan<Byte>
Zakres znaków UTF-8 do przeanalizowania.
- provider
- IFormatProvider
Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text
Zwraca
Wynik analizowania utf8Text.
Implementuje
Dotyczy
Parse(String, NumberStyles)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Konwertuje reprezentację ciągu liczby w określonym stylu na 16-bitową liczbę całkowitą ze znakiem.
public:
static short Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static short Parse(string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> int16
Public Shared Function Parse (s As String, style As NumberStyles) As Short
Parametry
- s
- String
Ciąg zawierający liczbę do przekonwertowania.
- style
- NumberStyles
Bitowa kombinacja wartości wyliczenia wskazująca elementy stylu, które mogą być obecne w elemecie s. Typową wartością do określenia jest Integer.
Zwraca
16-bitowa liczba całkowita ze znakiem równoważna liczbie określonej w elemencie s.
Wyjątki
Parametr s ma wartość null.
style nie jest wartością NumberStyles .
-lub-
style nie jest kombinacją AllowHexSpecifier wartości i HexNumber .
s nie jest w formacie zgodnym z style.
s reprezentuje liczbę mniejszą niż Int16.MinValue lub większa niż Int16.MaxValue.
-lub-
s zawiera cyfry ułamkowe niezerowe.
Przykłady
W poniższym przykładzie użyto Int16.Parse(String, NumberStyles) metody , aby przeanalizować reprezentacje ciągów Int16 wartości przy użyciu kultury en-US.
using System;
using System.Globalization;
public class ParseSample
{
public static void Main()
{
string value;
NumberStyles style;
// Parse a number with a thousands separator (throws an exception).
value = "14,644";
style = NumberStyles.None;
ParseToInt16(value, style);
style = NumberStyles.AllowThousands;
ParseToInt16(value, style);
// Parse a number with a thousands separator and decimal point.
value = "14,644.00";
style = NumberStyles.AllowThousands | NumberStyles.Integer |
NumberStyles.AllowDecimalPoint;
ParseToInt16(value, style);
// Parse a number with a fractional component (throws an exception).
value = "14,644.001";
ParseToInt16(value, style);
// Parse a number in exponential notation.
value = "145E02";
style = style | NumberStyles.AllowExponent;
ParseToInt16(value, style);
// Parse a number in exponential notation with a positive sign.
value = "145E+02";
ParseToInt16(value, style);
// Parse a number in exponential notation with a negative sign
// (throws an exception).
value = "145E-02";
ParseToInt16(value, style);
}
private static void ParseToInt16(string value, NumberStyles style)
{
try
{
short number = Int16.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}' with style {1}.", value,
style.ToString());
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int16 type.", value);
}
}
}
// The example displays the following output to the console:
// Unable to parse '14,644' with style None.
// Converted '14,644' to 14644.
// Converted '14,644.00' to 14644.
// '14,644.001' is out of range of the Int16 type.
// Converted '145E02' to 14500.
// Converted '145E+02' to 14500.
// '145E-02' is out of range of the Int16 type.
open System
open System.Globalization
let parseToInt16 (value: string) (style: NumberStyles) =
try
let number = Int16.Parse(value, style)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{value}' with style {style}."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int16 type."
[<EntryPoint>]
let main _ =
// Parse a number with a thousands separator (throws an exception).
let value = "14,644"
let style = NumberStyles.None
parseToInt16 value style
let style = NumberStyles.AllowThousands
parseToInt16 value style
// Parse a number with a thousands separator and decimal point.
let value = "14,644.00"
let style = NumberStyles.AllowThousands ||| NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
parseToInt16 value style
// Parse a number with a fractional component (throws an exception).
let value = "14,644.001"
parseToInt16 value style
// Parse a number in exponential notation.
let value = "145E02"
let style = style ||| NumberStyles.AllowExponent
parseToInt16 value style
// Parse a number in exponential notation with a positive sign.
let value = "145E+02"
parseToInt16 value style
// Parse a number in exponential notation with a negative sign
// (throws an exception).
let value = "145E-02"
parseToInt16 value style
0
// The example displays the following output to the console:
// Unable to parse '14,644' with style None.
// Converted '14,644' to 14644.
// Converted '14,644.00' to 14644.
// '14,644.001' is out of range of the Int16 type.
// Converted '145E02' to 14500.
// Converted '145E+02' to 14500.
// '145E-02' is out of range of the Int16 type.
Imports System.Globalization
Module ParseSample
Public Sub Main()
Dim value As String
Dim style As NumberStyles
' Parse a number with a thousands separator (throws an exception).
value = "14,644"
style = NumberStyles.None
ParseToInt16(value, style)
style = NumberStyles.AllowThousands
ParseToInt16(value, style)
' Parse a number with a thousands separator and decimal point.
value = "14,644.00"
style = NumberStyles.AllowThousands Or NumberStyles.Integer Or _
NumberStyles.AllowDecimalPoint
ParseToInt16(value, style)
' Parse a number with a fractional component (throws an exception).
value = "14,644.001"
ParseToInt16(value, style)
' Parse a number in exponential notation.
value = "145E02"
style = style Or NumberStyles.AllowExponent
ParseToInt16(value, style)
' Parse a number in exponential notation with a positive sign.
value = "145E+02"
ParseToInt16(value, style)
' Parse a number in exponential notation with a negative sign
' (throws an exception).
value = "145E-02"
ParseToInt16(value, style)
End Sub
Private Sub ParseToInt16(value As String, style As NumberStyles)
Try
Dim number As Short = Int16.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}' with style {1}.", value, _
style.ToString())
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int16 type.", value)
End Try
End Sub
End Module
' The example displays the following output to the console:
' Unable to parse '14,644' with style None.
' Converted '14,644' to 14644.
' Converted '14,644.00' to 14644.
' '14,644.001' is out of range of the Int16 type.
' Converted '145E02' to 14500.
' Converted '145E+02' to 14500.
' '145E-02' is out of range of the Int16 type.
Uwagi
Parametr style definiuje elementy stylu (takie jak biały znak lub symbol znaku), które są dozwolone w parametrze s , aby operacja analizy zakończyła się powodzeniem. Musi to być kombinacja flag bitowych z NumberStyles wyliczenia. W zależności od wartości styleparametr może s zawierać następujące elementy:
[ws] [$] [podpis] [cyfry,]cyfry[.fractional_digits][e[znak]cyfry][ws]
Lub, jeśli style obejmuje AllowHexSpecifier:
[ws]hexdigits[ws]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W poniższej tabeli opisano każdy element.
| Pierwiastek | Opis |
|---|---|
| Ws | Opcjonalne białe znaki. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite lub na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingWhite . |
| $ | Symbol waluty specyficznej dla kultury. Jego pozycja w ciągu jest definiowana przez NumberFormatInfo.CurrencyPositivePattern właściwości i NumberFormatInfo.CurrencyNegativePattern bieżącej kultury. Symbol waluty bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol . |
| podpis | Opcjonalny znak. Znak może pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingSign i może pojawić się na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingSign . Nawiasy mogą służyć do wskazywania wartości ujemnej, s jeśli style zawiera flagę NumberStyles.AllowParentheses . |
| Cyfr | Sekwencja cyfr z zakresu od 0 do 9. |
| , | Symbol separatora tysięcy specyficzny dla kultury. Symbol separatora tysięcy bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowThousands . |
| . | Symbol separatora dziesiętnego specyficznego dla kultury. Symbol dziesiętny bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . |
| fractional_digits | Sekwencja cyfry 0. Cyfry ułamkowe mogą być wyświetlane, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . Jeśli w fractional_digits pojawi się dowolna cyfra inna niż 0, metoda zgłasza błąd OverflowException. |
| e | Znak "e" lub "E", który wskazuje, że s może być reprezentowany w notacji wykładniczej. Parametr s może reprezentować liczbę w notacji wykładniczej, jeśli style zawiera flagę NumberStyles.AllowExponent .
s Jednak parametr musi reprezentować liczbę w zakresie Int16 typu danych i nie może mieć składnika ułamkowego innego niż zero. |
| hexdigits | Sekwencja cyfr szesnastkowa od 0 do f lub od 0 do F. |
Nuta
Wszystkie znaki NUL (U+0000) w obiekcie s są ignorowane przez operację analizowania, niezależnie od wartości argumentu style .
Ciąg zawierający tylko cyfry (odpowiadające NumberStyles.None stylowi) zawsze analizuje się pomyślnie. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, ale nie muszą być obecne w tym ciągu wejściowym. W poniższej tabeli przedstawiono, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w programie s.
| Nieskładne wartości NumberStyles | Elementy dozwolone w s oprócz cyfr |
|---|---|
| NumberStyles.None | Tylko cyfry dziesiętne. |
| NumberStyles.AllowDecimalPoint | Elementy . i fractional_digits . Jednak fractional_digits musi składać się tylko z co najmniej jednej cyfry lub OverflowException jest zgłaszana. |
| NumberStyles.AllowExponent | Parametr s może również używać notacji wykładniczej. |
| NumberStyles.AllowLeadingWhite | Element ws na początku elementu s. |
| NumberStyles.AllowTrailingWhite | Element ws na końcu elementu s. |
| NumberStyles.AllowLeadingSign | Znak może pojawić się przed cyframi. |
| NumberStyles.AllowTrailingSign | Znak może pojawić się po cyfrach. |
| NumberStyles.AllowParentheses | Element znaku w postaci nawiasów ujętą w wartość liczbową. |
| NumberStyles.AllowThousands | Element , . |
| NumberStyles.AllowCurrencySymbol | Element $ . |
Jeśli flaga NumberStyles.AllowHexSpecifier jest używana, s musi być ciągiem reprezentującym wartość szesnastkową bez prefiksu. Na przykład "9AF3" analizuje się pomyślnie, ale "0x9AF3" nie. Jedynymi innymi flagami, które mogą być obecne w systemie style , są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles ma styl liczby złożonej, NumberStyles.HexNumber, który zawiera obie flagi odstępu).
Parametr s jest analizowany przy użyciu informacji o formatowaniu w obiekcie zainicjowanym NumberFormatInfo dla bieżącej kultury systemu. Aby uzyskać więcej informacji, zobacz NumberFormatInfo.CurrentInfo. Aby przeanalizować s przy użyciu informacji o formatowaniu określonej kultury, wywołaj metodę Int16.Parse(String, NumberStyles, IFormatProvider) .