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
Parse(String, NumberStyles, IFormatProvider) |
Konwertuje reprezentację ciągu liczby w określonym stylu i formacie specyficznym dla kultury na odpowiednik liczby całkowitej ze znakiem 16-bitowym. |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Konwertuje reprezentację zakresu liczby w określonym stylu i formacie specyficznym dla kultury na odpowiednik liczby całkowitej ze znakiem 16-bitowym. |
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 odpowiednik liczby całkowitej ze znakiem 16-bitowym. |
Parse(String) |
Konwertuje reprezentację ciągu liczby na odpowiednik 16-bitowej liczby całkowitej 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 odpowiednik 16-bitowej liczby całkowitej ze znakiem. |
Parse(String, NumberStyles, IFormatProvider)
- Ź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 odpowiednik liczby całkowitej ze znakiem 16-bitowym.
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ę, która ma zostać przekształcona.
- style
- NumberStyles
Bitowa kombinacja wartości wyliczenia, która wskazuje elementy stylu, które mogą być obecne w obiekcie s
. Typową wartością do określenia jest Integer.
- provider
- IFormatProvider
Element IFormatProvider , który dostarcza informacje o formatowaniu specyficznym dla kultury dotyczące s
elementu .
Zwraca
Liczba całkowita podpisana 16-bitowo równoważna liczbie określonej w elemencie s
.
Implementuje
Wyjątki
s
to null
.
style
nie jest wartością NumberStyles .
-lub-
style
nie jest kombinacją AllowHexSpecifier wartości i HexNumber .
s
nie jest w formacie zgodnym z parametrem style
.
s
reprezentuje liczbę mniejszą niż Int16.MinValue lub większą niż Int16.MaxValue.
-lub-
s
zawiera niezerowe cyfry ułamkowe.
Przykłady
W poniższym przykładzie użyto różnych parametrów style
i provider
do analizowania reprezentacji ciągów Int16 wartości.
String^ value;
Int16 number;
NumberStyles style;
// Parse string using "." as the thousands separator
// and " " as the decimal separator.
value = "19 694,00";
style = NumberStyles::AllowDecimalPoint | NumberStyles::AllowThousands;
CultureInfo^ provider = gcnew 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 ^e)
{
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 = gcnew CultureInfo("en-GB");
try
{
number = Int16::Parse(value, style, CultureInfo::InvariantCulture);
Console::WriteLine("'{0}' converted to {1}.", value, number);
}
catch (FormatException ^e)
{
Console::WriteLine("Unable to parse '{0}'.", value);
}
// Displays:
// Unable to parse '$6,032.00'.
provider = gcnew CultureInfo("en-US");
number = Int16::Parse(value, style, provider);
Console::WriteLine("'{0}' converted to {1}.", value, number);
// Displays:
// '$6,032.00' converted to 6032.
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
operacji analizowania, aby operacja analizy zakończyła się pomyślnie. Musi być kombinacją flag bitowych z wyliczenia NumberStyles . W zależności od wartości parametr może style
s
zawierać następujące elementy:
[odstęp][$][znak][cyfry,]cyfry[.cyfry_ułamkowe][e[znak]cyfry][odstęp]
Lub, jeśli style
zawiera AllowHexSpecifier:
[odstęp]cyfry_szesnastkowe[odstęp]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.
Element | Opis |
---|---|
Ws | Opcjonalny odstęp. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite lub na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingWhite . |
$ | Symbol waluty specyficzny dla kultury. Jego pozycja w ciągu jest definiowana przez NumberFormatInfo.CurrencyPositivePattern właściwość i NumberFormatInfo.CurrencyNegativePattern bieżącej kultury. Symbol waluty bieżącej kultury może być wyświetlany, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol . |
sign | 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 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 dziesiętny specyficzny dla kultury. Symbol punktu dziesiętnego bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . |
fractional_digits | Sekwencja cyfry 0. Cyfry ułamkowe mogą być wyświetlane, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . Jeśli jakakolwiek cyfra inna niż 0 pojawia się w fractional_digits, 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 szesnastkowych od 0 do f lub od 0 do F. |
Uwaga
Wszelkie znaki zakończenia NUL (U+0000) w obiekcie s
są ignorowane przez operację analizowania, niezależnie od wartości argumentu style
.
Ciąg z cyframi (odpowiadający NumberStyles.None stylowi) zawsze jest analizowanych pomyślnie. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, ale nie muszą być obecne w tym ciągu wejściowym. Poniższa tabela wskazuje, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w elemecie s
.
Niezłożone wartości wyliczenia NumberStyles | Dodatkowe (poza cyframi) elementy dozwolone w parametrze s |
---|---|
NumberStyles.None | Tylko cyfry dziesiętne. |
NumberStyles.AllowDecimalPoint | Elementy . i fractional_digits . Jednak fractional_digits musi składać się tylko z jednej lub większej OverflowException liczby cyfr lub jest zgłaszana. |
NumberStyles.AllowExponent | Parametr s może również używać notacji wykładniczej. |
NumberStyles.AllowLeadingWhite | Element ws na początku s . |
NumberStyles.AllowTrailingWhite | Element ws na końcu s elementu . |
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 otaczających wartość liczbową. |
NumberStyles.AllowThousands | Element , . |
NumberStyles.AllowCurrencySymbol | Element $ . |
Jeśli jest używana flaga NumberStyles.AllowHexSpecifier , s
musi być reprezentacją ciągu wartości szesnastkowej bez prefiksu. Na przykład "9AF3" analizuje się pomyślnie, ale "0x9AF3" nie.. Jedynymi innymi flagami, które mogą być obecne, style
są NumberStyles.AllowLeadingWhite i NumberStyles.AllowTrailingWhite. (Wyliczenie NumberStyles ma styl liczb złożony, NumberStyles.HexNumber, który zawiera obie flagi odstępu).
Parametr provider
jest implementacją IFormatProvider , której GetFormat metoda uzyskuje NumberFormatInfo obiekt. Obiekt NumberFormatInfo udostępnia informacje specyficzne dla kultury dotyczące formatu .s
Jeśli provider
jest to null
, 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
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, która wskazuje elementy stylu, które mogą być obecne w obiekcie s
. Typową wartością do określenia jest Integer.
- provider
- IFormatProvider
Element IFormatProvider , który dostarcza informacje o formatowaniu specyficznym dla kultury dotyczące s
elementu .
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
Analizuje zakres znaków UTF-8 w wartości.
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 analizy.
- style
- NumberStyles
Bitowa kombinacja stylów liczbowych, które mogą być obecne w .utf8Text
- provider
- IFormatProvider
Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text
Zwraca
Wynik analizy utf8Text
.
Implementuje
Dotyczy
Parse(String, IFormatProvider)
- Ź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 odpowiednik liczby całkowitej ze znakiem 16-bitowym.
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ę, która ma zostać przekształcona.
- provider
- IFormatProvider
ElementIFormatProvider, który dostarcza informacje o formatowaniu specyficznym dla kultury.s
Zwraca
16-bitowa liczba całkowita ze znakiem równoważna liczbie określonej w elemencie s
.
Implementuje
Wyjątki
s
to null
.
s
nie ma poprawnego formatu.
s
reprezentuje liczbę mniejszą niż Int16.MinValue lub większą 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;
Int16 number;
stringToConvert = " 214 ";
try
{
number = Int16::Parse(stringToConvert, CultureInfo::InvariantCulture);
Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
}
catch (FormatException ^e)
{
Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException ^e)
{
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 ^e)
{
Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException ^e)
{
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 ^e)
{
Console::WriteLine("Unable to parse '{0}'.", stringToConvert);
}
catch (OverflowException ^e)
{
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.
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:
[odstęp][znak]cyfry[odstęp]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.
Element | Opis |
---|---|
ws | Opcjonalny biały znak. |
znak | Opcjonalny znak. |
cyfry | Sekwencja cyfr od 0 do 9. |
Parametr s
jest interpretowany przy użyciu NumberStyles.Integer stylu. Oprócz cyfr dziesiętnych tylko spacje wiodące i końcowe razem z wiodącym znakiem są dozwolone w .s
Aby jawnie zdefiniować elementy stylu wraz z informacjami o formatowaniu specyficznymi dla kultury, które mogą być obecne w s
programie , 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
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ę, która ma zostać przekształcona.
Zwraca
16-bitowa liczba całkowita ze znakiem równoważna liczbie zawartej w elemencie s
.
Wyjątki
s
to null
.
s
nie ma poprawnego formatu.
s
reprezentuje liczbę mniejszą niż Int16.MinValue lub większą 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.
String^ value;
Int16 number;
value = " 12603 ";
try
{
number = Int16::Parse(value);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException ^e)
{
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 ^e)
{
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 ^e)
{
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.
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:
[odstęp][znak]cyfry[odstęp]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.
Element | Opis |
---|---|
Ws | Opcjonalny odstęp. |
sign | Opcjonalny znak. |
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 s
programie , 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
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 analizy s
.
Implementuje
Dotyczy
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Źródło:
- Int16.cs
- Źródło:
- Int16.cs
Analizuje zakres znaków UTF-8 w wartości.
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 analizy.
- provider
- IFormatProvider
Obiekt, który udostępnia informacje o formatowaniu specyficznym dla kultury.utf8Text
Zwraca
Wynik analizy utf8Text
.
Implementuje
Dotyczy
Parse(String, NumberStyles)
- Ź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 odpowiednik 16-bitowej liczby całkowitej 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ę, która ma zostać przekształcona.
- style
- NumberStyles
Bitowa kombinacja wartości wyliczenia, która wskazuje elementy stylu, które mogą być obecne w .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
s
to null
.
style
nie jest wartością NumberStyles .
-lub-
style
nie jest kombinacją AllowHexSpecifier wartości i HexNumber .
s
nie jest w formacie zgodnym z parametrem style
.
s
reprezentuje liczbę mniejszą niż Int16.MinValue lub większą niż Int16.MaxValue.
-lub-
s
zawiera niezerowe cyfry ułamkowe.
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 namespace System;
using namespace System::Globalization;
ref 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;
ParseSample::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
{
Int16 number = Int16::Parse(value, style);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException ^e)
{
Console::WriteLine("Unable to parse '{0}' with style {1}.", value,
style);
}
catch (OverflowException ^e)
{
Console::WriteLine("'{0}' is out of range of the Int16 type.", value);
}
}
};
int main()
{
ParseSample::Main();
Console::ReadLine();
return 0;
}
// The example displays the following output:
// 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.
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
dla operacji analizowania pomyślnie. Musi to być kombinacja flag bitowych z NumberStyles wyliczenia. W zależności od wartości style
parametr może s
zawierać następujące elementy:
[odstęp][$][znak][cyfry,]cyfry[.cyfry_ułamkowe][e[znak]cyfry][odstęp]
Lub, jeśli style
zawiera AllowHexSpecifier:
[odstęp]cyfry_szesnastkowe[odstęp]
Elementy w nawiasach kwadratowych ([ i ]) są opcjonalne. W tabeli poniżej opisano każdy element.
Element | Opis |
---|---|
Ws | Opcjonalny odstęp. Białe znaki mogą pojawić się na początku s , jeśli style zawiera flagę NumberStyles.AllowLeadingWhite , lub na końcu s , jeśli style zawiera flagę NumberStyles.AllowTrailingWhite . |
$ | Symbol waluty specyficzny dla kultury. Jego pozycja w ciągu jest definiowana przez NumberFormatInfo.CurrencyPositivePattern właściwość i NumberFormatInfo.CurrencyNegativePattern bieżącej kultury. Symbol waluty bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowCurrencySymbol . |
sign | 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ć s do wskazywania wartości ujemnej, jeśli style zawiera flagę NumberStyles.AllowParentheses . |
Cyfr | Sekwencja cyfr 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 dziesiętny specyficzny dla kultury. Symbol punktu dziesiętnego bieżącej kultury może pojawić się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . |
fractional_digits | Sekwencja cyfry 0. Cyfry ułamkowe mogą pojawiać się, s jeśli style zawiera flagę NumberStyles.AllowDecimalPoint . Jeśli dowolna cyfra inna niż 0 jest wyświetlana w fractional_digits, metoda zgłasza wyjątek 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 szesnastkowych od 0 do f lub od 0 do F. |
Uwaga
Wszystkie znaki NUL zakończenia (U+0000) w s
obiekcie są ignorowane przez operację analizowania, niezależnie od wartości argumentu style
.
Ciąg z tylko cyframi (który odpowiada stylowi NumberStyles.None ) zawsze jest analizowanych pomyślnie. Większość pozostałych NumberStyles elementów członkowskich kontroluje elementy, które mogą być, ale nie muszą być obecne w tym ciągu wejściowym. W poniższej tabeli przedstawiono, jak poszczególne NumberStyles elementy członkowskie wpływają na elementy, które mogą być obecne w programie s
.
Niezłożone wartości wyliczenia NumberStyles | Dodatkowe (poza cyframi) elementy dozwolone w parametrze s |
---|---|
NumberStyles.None | Tylko cyfry dziesiętne. |
NumberStyles.AllowDecimalPoint | Elementy . i fractional_digits . Jednak fractional_digits musi składać się tylko z co najmniej jednej cyfry lub OverflowException jest zgłaszany. |
NumberStyles.AllowExponent | Parametr s może również używać notacji wykładniczej. |
NumberStyles.AllowLeadingWhite | Element ws na początku elementu s . |
NumberStyles.AllowTrailingWhite | Element ws na końcu elementu s . |
NumberStyles.AllowLeadingSign | Znak może pojawić się przed cyframi. |
NumberStyles.AllowTrailingSign | Znak może pojawić się po cyfrach. |
NumberStyles.AllowParentheses | Element znaku w postaci nawiasów otaczających wartość liczbową. |
NumberStyles.AllowThousands | Element , . |
NumberStyles.AllowCurrencySymbol | Element $ . |
Jeśli jest używana flaga NumberStyles.AllowHexSpecifier , s
musi być reprezentacją ciągu wartości szesnastkowej 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ą i NumberStyles.AllowLeadingWhiteNumberStyles.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) .
Zobacz też
Dotyczy
Opinia
https://aka.ms/ContentUserFeedback.
Dostępne już wkrótce: W 2024 r. będziemy stopniowo wycofywać zgłoszenia z serwisu GitHub jako mechanizm przesyłania opinii na temat zawartości i zastępować go nowym systemem opinii. Aby uzyskać więcej informacji, sprawdź:Prześlij i wyświetl opinię dla