Int16.Parse Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Konverterar strängrepresentationen av ett tal till dess 16-bitars signerade heltalsekvivalent.
Överlagringar
| Name | Description |
|---|---|
| Parse(String) |
Konverterar strängrepresentationen av ett tal till dess 16-bitars signerade heltalsekvivalent. |
| Parse(ReadOnlySpan<Byte>, IFormatProvider) |
Parsar ett intervall med UTF-8 tecken till ett värde. |
| Parse(ReadOnlySpan<Char>, IFormatProvider) |
Parsar ett teckenintervall till ett värde. |
| Parse(String, NumberStyles) |
Konverterar strängrepresentationen av ett tal i ett angivet format till dess 16-bitars signerade heltalsmotsvarighet. |
| Parse(String, IFormatProvider) |
Konverterar strängrepresentationen av ett tal i ett angivet kulturspecifikt format till dess 16-bitars signerade heltalsekvivalent. |
| Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider) |
Parsar ett intervall med UTF-8 tecken till ett värde. |
| Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
Konverterar spanrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 16-bitars signerade heltalsekvivalent. |
| Parse(String, NumberStyles, IFormatProvider) |
Konverterar strängrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 16-bitars signerade heltalsekvivalent. |
Parse(String)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Konverterar strängrepresentationen av ett tal till dess 16-bitars signerade heltalsekvivalent.
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
Parametrar
- s
- String
En sträng som innehåller ett tal som ska konverteras.
Returer
Ett 16-bitars signerat heltal som motsvarar talet i s.
Undantag
s är null.
s är inte i rätt format.
s representerar ett tal som är mindre än Int16.MinValue eller större än Int16.MaxValue.
Exempel
I följande exempel visas hur du konverterar ett strängvärde till ett 16-bitars signerat heltalsvärde med hjälp av Int16.Parse(String) metoden. Det resulterande heltalsvärdet visas sedan för konsolen.
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.
Kommentarer
Parametern s innehåller ett antal av formuläret:
[ws][sign]digits[ws]
Element inom hakparenteser ([ och ]) är valfria. I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Valfritt blanksteg. |
| signera | Ett valfritt tecken. |
| Siffror | En sekvens med siffror mellan 0 och 9. |
Parametern s tolkas med formatet NumberStyles.Integer . Förutom heltalsvärdets decimalsiffror tillåts endast inledande och avslutande blanksteg tillsammans med ett inledande tecken. Om du uttryckligen vill definiera de formatelement som kan finnas i sanvänder du antingen Int16.Parse(String, NumberStyles) metoden eller Parse .
Parametern s parsas med formateringsinformationen i ett NumberFormatInfo objekt som initieras för den aktuella systemkulturen. Mer information finns i CurrentInfo. Om du vill parsa en sträng med formateringsinformationen för någon annan kultur använder du Int16.Parse(String, IFormatProvider) metoden eller Int16.Parse(String, NumberStyles, IFormatProvider) .
Se även
Gäller för
Parse(ReadOnlySpan<Byte>, IFormatProvider)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Parsar ett intervall med UTF-8 tecken till ett värde.
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
Parametrar
- utf8Text
- ReadOnlySpan<Byte>
Intervallet för UTF-8 tecken att parsa.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om utf8Text.
Returer
Resultatet av parsning utf8Text.
Implementeringar
Gäller för
Parse(ReadOnlySpan<Char>, IFormatProvider)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Parsar ett teckenintervall till ett värde.
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
Parametrar
- s
- ReadOnlySpan<Char>
Det intervall med tecken som ska parsas.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Resultatet av parsning s.
Implementeringar
Gäller för
Parse(String, NumberStyles)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Konverterar strängrepresentationen av ett tal i ett angivet format till dess 16-bitars signerade heltalsmotsvarighet.
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
Parametrar
- s
- String
En sträng som innehåller ett tal som ska konverteras.
- style
- NumberStyles
En bitvis kombination av uppräkningsvärdena som anger de formatelement som kan finnas i s. Ett typiskt värde att ange är Integer.
Returer
Ett 16-bitars signerat heltal som motsvarar det nummer som anges i s.
Undantag
s är null.
style är inte ett NumberStyles värde.
-eller-
style är inte en kombination av AllowHexSpecifier och HexNumber värden.
s är inte i ett format som är kompatibelt med style.
s representerar ett tal som är mindre än Int16.MinValue eller större än Int16.MaxValue.
-eller-
s innehåller bråksiffror som inte är noll.
Exempel
I följande exempel används Int16.Parse(String, NumberStyles) metoden för att parsa strängrepresentationer av värden med hjälp av Int16 en-US kultur.
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.
Kommentarer
Parametern style definierar formatelementen (till exempel blanksteg eller en teckensymbol) som tillåts i parametern s för att parsningsåtgärden ska lyckas. Det måste vara en kombination av bitflaggor från NumberStyles uppräkningen. Beroende på värdet för stylekan parametern s innehålla följande element:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
Eller, om style inkluderar AllowHexSpecifier:
[ws]hexdigits[ws]
Objekt inom hakparenteser ([ och ]) är valfria. I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Valfritt blanksteg. Tomt utrymme kan visas i början av s om style innehåller NumberStyles.AllowLeadingWhite flaggan, eller i slutet av s om style innehåller NumberStyles.AllowTrailingWhite flaggan. |
| $ | En kulturspecifik valutasymbol. Dess position i strängen definieras av NumberFormatInfo.CurrencyPositivePattern egenskaperna och NumberFormatInfo.CurrencyNegativePattern för den aktuella kulturen. Den aktuella kulturens valutasymbol kan visas i s om style den NumberStyles.AllowCurrencySymbol innehåller flaggan. |
| signera | Ett valfritt tecken. Tecknet kan visas i början av s om style innehåller NumberStyles.AllowLeadingSign flaggan, och det kan visas i slutet av s om style innehåller NumberStyles.AllowTrailingSign flaggan. Parenteser kan användas i s för att ange ett negativt värde om style den NumberStyles.AllowParentheses innehåller flaggan. |
| Siffror | En sekvens med siffror från 0 till 9. |
| , | En kulturspecifik tusentalsavgränsare. Den aktuella kulturens tusentalsavgränsarsymbol kan visas i s om style den NumberStyles.AllowThousands innehåller flaggan. |
| . | Ett kulturspecifikt decimaltecken. Den aktuella kulturens decimaltecken kan visas i s om style den NumberStyles.AllowDecimalPoint innehåller flaggan. |
| fractional_digits | En sekvens med 0-siffran. Bråksiffror kan visas i s om style den NumberStyles.AllowDecimalPoint innehåller flaggan. Om någon annan siffra än 0 visas i fractional_digits genererar metoden en OverflowException. |
| e | Tecknet "e" eller "E", som anger att s det kan representeras i exponentiell notation. Parametern s kan representera ett tal i exponentiell notation om style den NumberStyles.AllowExponent innehåller flaggan. Parametern s måste dock representera ett tal i datatypens Int16 intervall och får inte ha en bråkkomponent som inte är noll. |
| hexdigits | En sekvens med hexadecimala siffror från 0 till f eller 0 till och med F. |
Note
Alla avslutande NUL-tecken (U+0000) i s ignoreras av parsningsåtgärden, oavsett argumentets style värde.
En sträng med endast siffror (vilket motsvarar NumberStyles.None formatet) parsar alltid korrekt. De flesta av de återstående NumberStyles medlemmarna styr element som kan vara men som inte måste finnas i den här indatasträngen. Följande tabell visar hur enskilda NumberStyles medlemmar påverkar de element som kan finnas i s.
| Icke-sammansatta NumberStyles-värden | Element som tillåts i s utöver siffror |
|---|---|
| NumberStyles.None | Endast decimalsiffror. |
| NumberStyles.AllowDecimalPoint | Elementen . och fractional_digits . Men fractional_digits får bara bestå av en eller flera 0 siffror eller så utlöses en OverflowException . |
| NumberStyles.AllowExponent | Parametern s kan också använda exponentiell notation. |
| NumberStyles.AllowLeadingWhite |
WS-elementet i början av s. |
| NumberStyles.AllowTrailingWhite |
WS-elementet i slutet av s. |
| NumberStyles.AllowLeadingSign | Ett tecken kan visas före siffror. |
| NumberStyles.AllowTrailingSign | Ett tecken kan visas efter siffror. |
| NumberStyles.AllowParentheses | Teckenelementet i form av parenteser som omger det numeriska värdet. |
| NumberStyles.AllowThousands | Elementet , . |
| NumberStyles.AllowCurrencySymbol | Elementet $ . |
NumberStyles.AllowHexSpecifier Om flaggan används s måste vara strängrepresentationen av ett hexadecimalt värde utan prefix. Till exempel parsar "9AF3" korrekt, men "0x9AF3" gör det inte. De enda andra flaggorna som kan finnas i style är NumberStyles.AllowLeadingWhite och NumberStyles.AllowTrailingWhite. (Uppräkningen NumberStyles har ett sammansatt talformat, NumberStyles.HexNumber, som innehåller båda blankstegsflaggorna.)
Parametern s parsas med formateringsinformationen i ett NumberFormatInfo objekt som initieras för den aktuella systemkulturen. Mer information finns i NumberFormatInfo.CurrentInfo. Om du vill parsa s med formateringsinformationen för en viss kultur anropar du Int16.Parse(String, NumberStyles, IFormatProvider) metoden.
Se även
Gäller för
Parse(String, IFormatProvider)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Konverterar strängrepresentationen av ett tal i ett angivet kulturspecifikt format till dess 16-bitars signerade heltalsekvivalent.
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
Parametrar
- s
- String
En sträng som innehåller ett tal som ska konverteras.
- provider
- IFormatProvider
En IFormatProvider som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Ett 16-bitars signerat heltal som motsvarar det nummer som anges i s.
Implementeringar
Undantag
s är null.
s är inte i rätt format.
s representerar ett tal som är mindre än Int16.MinValue eller större än Int16.MaxValue.
Exempel
I följande exempel parsas strängrepresentationer av Int16 värden med Int16.Parse(String, IFormatProvider) -metoden.
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.
Kommentarer
Parametern s innehåller ett antal av formuläret:
[ws][sign]digits[ws]
Element inom hakparenteser ([ och ]) är valfria. I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Ett valfritt tomt utrymme. |
| sign | Ett valfritt tecken. |
| Siffror | En sekvens med siffror mellan 0 och 9. |
Parametern s tolkas med formatet NumberStyles.Integer . Förutom decimaltal tillåts endast inledande och avslutande blanksteg tillsammans med ett inledande tecken i s. Om du vill definiera formatelementen explicit tillsammans med den kulturspecifika formateringsinformation som kan finnas i sanvänder du Int16.Parse(String, NumberStyles, IFormatProvider) metoden .
Parametern provider är en IFormatProvider implementering som hämtar ett NumberFormatInfo objekt.
NumberFormatInfo Tillhandahåller kulturspecifik information om formatet för s. Om provider är nullNumberFormatInfo används för den aktuella kulturen.
Se även
Gäller för
Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Parsar ett intervall med UTF-8 tecken till ett värde.
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
Parametrar
- utf8Text
- ReadOnlySpan<Byte>
Intervallet för UTF-8 tecken att parsa.
- style
- NumberStyles
En bitvis kombination av talformat som kan finnas i utf8Text.
- provider
- IFormatProvider
Ett objekt som tillhandahåller kulturspecifik formateringsinformation om utf8Text.
Returer
Resultatet av parsning utf8Text.
Implementeringar
Gäller för
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Konverterar spanrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 16-bitars signerade heltalsekvivalent.
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
Parametrar
- s
- ReadOnlySpan<Char>
Ett intervall som innehåller de tecken som representerar talet som ska konverteras.
- style
- NumberStyles
En bitvis kombination av uppräkningsvärden som anger de formatelement som kan finnas i s. Ett typiskt värde att ange är Integer.
- provider
- IFormatProvider
En IFormatProvider som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Ett 16-bitars signerat heltal som motsvarar det nummer som anges i s.
Implementeringar
Gäller för
Parse(String, NumberStyles, IFormatProvider)
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
- Källa:
- Int16.cs
Konverterar strängrepresentationen av ett tal i ett angivet format och kulturspecifikt format till dess 16-bitars signerade heltalsekvivalent.
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
Parametrar
- s
- String
En sträng som innehåller ett tal som ska konverteras.
- style
- NumberStyles
En bitvis kombination av uppräkningsvärden som anger de formatelement som kan finnas i s. Ett typiskt värde att ange är Integer.
- provider
- IFormatProvider
En IFormatProvider som tillhandahåller kulturspecifik formateringsinformation om s.
Returer
Ett 16-bitars signerat heltal som motsvarar det nummer som anges i s.
Implementeringar
Undantag
s är null.
style är inte ett NumberStyles värde.
-eller-
style är inte en kombination av AllowHexSpecifier och HexNumber värden.
s är inte i ett format som är kompatibelt med style.
s representerar ett tal som är mindre än Int16.MinValue eller större än Int16.MaxValue.
-eller-
s innehåller bråksiffror som inte är noll.
Exempel
I följande exempel används en mängd style olika parametrar för provider att parsa strängrepresentationerna av Int16 värden.
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.
Kommentarer
Parametern style definierar formatelementen (till exempel tomt utrymme eller det positiva tecknet) som tillåts i parametern s för att parsningsåtgärden ska lyckas. Det måste vara en kombination av bitflaggor från NumberStyles uppräkningen. Beroende på värdet för stylekan parametern s innehålla följande element:
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
Eller, om style inkluderar AllowHexSpecifier:
[ws]hexdigits[ws]
Element inom hakparenteser ([ och ]) är valfria. I följande tabell beskrivs varje element.
| Element | Description |
|---|---|
| Ws | Valfritt blanksteg. Tomt utrymme kan visas i början av s om style innehåller NumberStyles.AllowLeadingWhite flaggan, eller i slutet av s om style innehåller NumberStyles.AllowTrailingWhite flaggan. |
| $ | En kulturspecifik valutasymbol. Dess position i strängen definieras av NumberFormatInfo.CurrencyPositivePattern egenskaperna och NumberFormatInfo.CurrencyNegativePattern för den aktuella kulturen. Den aktuella kulturens valutasymbol kan visas i s om style den NumberStyles.AllowCurrencySymbol innehåller flaggan. |
| signera | Ett valfritt tecken. Tecknet kan visas i början av s om style innehåller NumberStyles.AllowLeadingSign flaggan, och det kan visas i slutet av s om style innehåller NumberStyles.AllowTrailingSign flaggan. Parenteser kan användas i s för att ange ett negativt värde om style den NumberStyles.AllowParentheses innehåller flaggan. |
| Siffror | En sekvens med siffror från 0 till 9. |
| , | En kulturspecifik tusentalsavgränsare. Den aktuella kulturens tusentalsavgränsarsymbol kan visas i s om style den NumberStyles.AllowThousands innehåller flaggan. |
| . | Ett kulturspecifikt decimaltecken. Den aktuella kulturens decimaltecken kan visas i s om style den NumberStyles.AllowDecimalPoint innehåller flaggan. |
| fractional_digits | En sekvens med 0-siffran. Bråksiffror kan visas i s om style den NumberStyles.AllowDecimalPoint innehåller flaggan. Om någon annan siffra än 0 visas i fractional_digits genererar metoden en OverflowException. |
| e | Tecknet "e" eller "E", som anger att s det kan representeras i exponentiell notation. Parametern s kan representera ett tal i exponentiell notation om style den NumberStyles.AllowExponent innehåller flaggan. Parametern s måste dock representera ett tal i datatypens Int16 intervall och får inte ha en bråkkomponent som inte är noll. |
| hexdigits | En sekvens med hexadecimala siffror från 0 till f eller 0 till och med F. |
Note
Alla avslutande NUL-tecken (U+0000) i s ignoreras av parsningsåtgärden, oavsett argumentets style värde.
En sträng med endast siffror (vilket motsvarar NumberStyles.None formatet) parsar alltid korrekt. De flesta av de återstående NumberStyles medlemmarna styr element som kan vara men som inte måste finnas i den här indatasträngen. Följande tabell visar hur enskilda NumberStyles medlemmar påverkar de element som kan finnas i s.
| Icke-sammansatta NumberStyles-värden | Element som tillåts i s utöver siffror |
|---|---|
| NumberStyles.None | Endast decimalsiffror. |
| NumberStyles.AllowDecimalPoint | Elementen . och fractional_digits . Men fractional_digits får bara bestå av en eller flera 0 siffror eller så utlöses en OverflowException . |
| NumberStyles.AllowExponent | Parametern s kan också använda exponentiell notation. |
| NumberStyles.AllowLeadingWhite |
WS-elementet i början av s. |
| NumberStyles.AllowTrailingWhite |
WS-elementet i slutet av s. |
| NumberStyles.AllowLeadingSign | Ett tecken kan visas före siffror. |
| NumberStyles.AllowTrailingSign | Ett tecken kan visas efter siffror. |
| NumberStyles.AllowParentheses | Teckenelementet i form av parenteser som omger det numeriska värdet. |
| NumberStyles.AllowThousands | Elementet , . |
| NumberStyles.AllowCurrencySymbol | Elementet $ . |
NumberStyles.AllowHexSpecifier Om flaggan används s måste vara strängrepresentationen av ett hexadecimalt värde utan prefix. Till exempel parsar "9AF3" korrekt, men "0x9AF3" gör det inte.. De enda andra flaggorna som kan finnas i style är NumberStyles.AllowLeadingWhite och NumberStyles.AllowTrailingWhite. (Uppräkningen NumberStyles har ett sammansatt talformat, NumberStyles.HexNumber, som innehåller båda blankstegsflaggorna.)
Parametern provider är en IFormatProvider implementering vars GetFormat metod hämtar ett NumberFormatInfo objekt. Objektet NumberFormatInfo innehåller kulturspecifik information om formatet sför . Om provider är nullNumberFormatInfo används objektet för den aktuella kulturen.