共用方式為


Int16.Parse 方法

定義

將數位的字串表示轉換為其相等的16位帶正負號的整數。

多載

名稱 Description
Parse(String, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式之數位的字串表示轉換為其16位帶正負號的整數對等。

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

將指定樣式和特定文化特性格式的數位範圍表示轉換為其16位帶正負號的整數對等。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(String, IFormatProvider)

將指定之特定文化特性格式之數位的字串表示轉換為其相等的16位帶正負號整數。

Parse(String)

將數位的字串表示轉換為其相等的16位帶正負號的整數。

Parse(ReadOnlySpan<Char>, IFormatProvider)

將字元範圍剖析為值。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

將UTF-8字元的範圍剖析為值。

Parse(String, NumberStyles)

將指定樣式中數位的字串表示轉換為其相等的16位帶正負號的整數。

Parse(String, NumberStyles, IFormatProvider)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將指定樣式和特定文化特性格式之數位的字串表示轉換為其16位帶正負號的整數對等。

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

參數

s
String

字串,包含要轉換的數位。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

提供IFormatProvider針對特定文化的格式資訊。s

傳回

一個 16 位元有號整數,等價於 中 s指定的數字。

實作

例外狀況

snull

style 不是一個 NumberStyles 數值。

-或-

style不是 和 HexNumber 的組合AllowHexSpecifier

s格式不符合。style

s 代表小於 Int16.MinValue 或大於 Int16.MaxValue 的數字。

-或-

s 包含非零的分數位數。

範例

以下範例使用多種 styleprovider 參數來解析數值的 Int16 字串表示。

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.

備註

參數 style 定義了允許 s 在解析操作中成功使用樣式元素(如空白或正號)。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 根據 的 style值, s 參數可能包含以下元素:

[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]

或,若 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果包含旗幟,則可在 的s開頭出現空白,或在包含旗幟時出現在結尾NumberStyles.AllowTrailingWhitestylesNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo.CurrencyNegativePattern當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。
簽署 選擇性符號。 如果包含NumberStyles.AllowLeadingSign旗幟,標誌可以出現在 的styles開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾sstyle。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
數字 從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符符號。 當前style文化的千分符號若包含NumberStyles.AllowThousands國旗,則可出現s
. 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。
fractional_digits 0 位數的序列。 若style包含NumberStyles.AllowDecimalPoint旗幟,則可出現s小數數字。 若 fractional_digits中出現非0的數字,方法會拋出一個 OverflowException
e 表示 可以用 s 指數符號表示的「e」或「E」字元。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。 然而,參數 s 必須代表資料型態範圍內 Int16 的一個數字,且不能有非零的分數成分。
六角位數 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有 數字 的字串(對應 NumberStyles.None 樣式)總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不強制存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

非複合 NumberStyles 值 除了數位以外,允許的 元素
NumberStyles.None 只有十進位數。
NumberStyles.AllowDecimalPoint 以及 以及 fractional_digits 元素。 然而, fractional_digits 必須只包含一個或多個0,否則會擲出一個 OverflowException
NumberStyles.AllowExponent 參數 s 也可以使用指數符號。
NumberStyles.AllowLeadingWhite 開頭sw 元素。
NumberStyles.AllowTrailingWhite 末尾s 元素。
NumberStyles.AllowLeadingSign 符號可以出現在 數字之前。
NumberStyles.AllowTrailingSign 數字 後面會出現符號。
NumberStyles.AllowParentheses 符號 元素以 括號形式包含數值。
NumberStyles.AllowThousands 這個元素
NumberStyles.AllowCurrencySymbol 元素 $

NumberStyles.AllowHexSpecifier 使用旗標, s 必須是無前綴的十六進位值字串表示。 例如,“9AF3” 剖析成功,但 “0x9AF3” 則不會。 唯一可能存在於 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉採用複合數字風格, NumberStyles.HexNumber包含兩個空白旗。)

參數 provider 是一個 IFormatProvider 實作,其 GetFormat 方法會取得一個 NumberFormatInfo 物件。 該 NumberFormatInfo 物件提供關於 格式 s的文化特定資訊。 若provider為 ,nullNumberFormatInfo則使用 當前文化的物件。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將指定樣式和特定文化特性格式的數位範圍表示轉換為其16位帶正負號的整數對等。

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

參數

s
ReadOnlySpan<Char>

範圍,包含表示要轉換之數位的字元。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值為 Integer

provider
IFormatProvider

提供IFormatProvider針對特定文化的格式資訊。s

傳回

一個 16 位元有號整數,等價於 中 s指定的數字。

實作

適用於

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將UTF-8字元的範圍剖析為值。

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

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

style
NumberStyles

一個可以存在 utf8Text於 的數字樣式的位元組合。

provider
IFormatProvider

一個提供關於 utf8Text的文化特定格式資訊的物件。

傳回

解析 的結果 utf8Text

實作

適用於

Parse(String, IFormatProvider)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將指定之特定文化特性格式之數位的字串表示轉換為其相等的16位帶正負號整數。

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

參數

s
String

字串,包含要轉換的數位。

provider
IFormatProvider

提供IFormatProvider針對特定文化的格式資訊。s

傳回

一個 16 位元有號整數,等價於 中 s指定的數字。

實作

例外狀況

snull

s 格式不正確。

s 代表小於 Int16.MinValue 或大於 Int16.MaxValue 的數字。

範例

以下範例解析使用此Int16.Parse(String, IFormatProvider)方法的Int16字串表示值。

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.

備註

s 參數包含以下形式的數字:

[ws][sign]digits[ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。
標誌 選擇性符號。
數位 範圍從 0 到 9 的數位序列。

s參數是透過 NumberStyles.Integer style 來解釋的。 除了十進位數字外,只有前置和後置空格連同前導符號被允許。s 若要明確定義風格元素及可存在 s的文化特定格式資訊,請使用該 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。

參數 provider 是一個 IFormatProvider 實作,用來取得一個 NumberFormatInfo 物件。 提供 NumberFormatInfo 關於格式 s的文化特定資訊。 若 providernullNumberFormatInfo則 為當前文化。

另請參閱

適用於

Parse(String)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將數位的字串表示轉換為其相等的16位帶正負號的整數。

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

參數

s
String

字串,包含要轉換的數位。

傳回

一個 16 位元有號整數,等價於 所 s包含的數字。

例外狀況

snull

s 格式不正確。

s 代表小於 Int16.MinValue 或大於 Int16.MaxValue 的數字。

範例

以下範例示範如何利用此 Int16.Parse(String) 方法將字串值轉換為16位元有號整數值。 產生的整數值接著會顯示至主控台。

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.

備註

s 參數包含以下形式的數字:

[ws][sign]digits[ws]

方括弧 ([ 和 ]) 中的元素是選擇性的。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。
簽署 選擇性符號。
數字 範圍從 0 到 9 的數位序列。

s參數是透過 NumberStyles.Integer style 來解釋的。 除了整數值的十進位數之外,只允許前置和尾端空格與前置正負號。 要明確定義可以存在 s於 中的樣式元素,請使用 該 Int16.Parse(String, NumberStyles) 方法或 方法 Parse

s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 如需詳細資訊,請參閱CurrentInfo。 若要解析字串,使用其他文化的格式資訊,請使用 Int16.Parse(String, IFormatProvider) 或 方法 Int16.Parse(String, NumberStyles, IFormatProvider)

另請參閱

適用於

Parse(ReadOnlySpan<Char>, IFormatProvider)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將字元範圍剖析為值。

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

參數

s
ReadOnlySpan<Char>

要剖析的字元範圍。

provider
IFormatProvider

一個提供關於 s的文化特定格式資訊的物件。

傳回

解析 的結果 s

實作

適用於

Parse(ReadOnlySpan<Byte>, IFormatProvider)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將UTF-8字元的範圍剖析為值。

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

參數

utf8Text
ReadOnlySpan<Byte>

要剖析的UTF-8字元範圍。

provider
IFormatProvider

一個提供關於 utf8Text的文化特定格式資訊的物件。

傳回

解析 的結果 utf8Text

實作

適用於

Parse(String, NumberStyles)

來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs
來源:
Int16.cs

將指定樣式中數位的字串表示轉換為其相等的16位帶正負號的整數。

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

參數

s
String

字串,包含要轉換的數位。

style
NumberStyles

一個位元組合的列舉值,表示可能存在 s於 中的樣式元素。 典型的指定值為 Integer

傳回

一個 16 位元有號整數,等價於 中 s指定的數字。

例外狀況

snull

style 不是一個 NumberStyles 數值。

-或-

style不是 和 HexNumber 的組合AllowHexSpecifier

s格式不符合。style

s 代表小於 Int16.MinValue 或大於 Int16.MaxValue 的數字。

-或-

s 包含非零的分數位數。

範例

以下範例使用 Int16.Parse(String, NumberStyles) 了利用 en-US 文化解析數值字 Int16 串表示的方法。

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.

備註

參數定義 style 了參數中允許 s 的樣式元素(例如空白或符號符號),以使解析操作成功。 它必須是列舉中多個位元旗 NumberStyles 標的組合。 根據 的 style值, s 參數可能包含以下元素:

[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]

或,若 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

方括弧 ([ 和 ]) 中的項目是選擇性專案。 下表描述每個元素。

元素 描述
ws 選擇性的空格符。 如果包含旗幟,則可在 的s開頭出現空白,或在包含旗幟時出現在結尾NumberStyles.AllowTrailingWhitestylesNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 它在字串中的位置由NumberFormatInfo.CurrencyPositivePatternNumberFormatInfo.CurrencyNegativePattern當前文化的性質所定義。 當前s文化的貨幣符號可以出現style在包含NumberStyles.AllowCurrencySymbol國旗的情況下。
簽署 選擇性符號。 如果包含NumberStyles.AllowLeadingSign旗幟,標誌可以出現在 的styles開頭,如果包含NumberStyles.AllowTrailingSign該旗幟,則可以出現在 的結尾sstyle。 若s包含旗標,style則可使用NumberStyles.AllowParentheses括號表示負值。
數字 從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符符號。 當前style文化的千分符號若包含NumberStyles.AllowThousands國旗,則可出現s
. 特定文化特性的小數點符號。 當前文化的小數點符號可以出現sstyle在包含NumberStyles.AllowDecimalPoint國旗的情況下。
fractional_digits 0 位數的序列。 若style包含NumberStyles.AllowDecimalPoint旗幟,則可出現s小數數字。 若 fractional_digits中出現非0的數字,方法會拋出一個 OverflowException
e 表示 可以用 s 指數符號表示的「e」或「E」字元。 若s包含style旗標,參數NumberStyles.AllowExponent可用指數符號表示數字。 然而,參數 s 必須代表資料型態範圍內 Int16 的一個數字,且不能有非零的分數成分。
六角位數 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

任何終止的 NUL(U+0000) 字元 s 都會被解析操作忽略,無論參數值 style 為何。

只有 數字 的字串(對應 NumberStyles.None 樣式)總是能成功解析。 其餘 NumberStyles 大多數成員控制此輸入字串中可能存在但不強制存在的元素。 下表顯示個別 NumberStyles 成員如何影響可能存在於 s中的元素。

非複合 NumberStyles 值 除了數位以外,允許的 元素
NumberStyles.None 只有十進位數。
NumberStyles.AllowDecimalPoint 以及 以及 fractional_digits 元素。 然而, fractional_digits 必須只包含一個或多個0,否則會擲出一個 OverflowException
NumberStyles.AllowExponent 參數 s 也可以使用指數符號。
NumberStyles.AllowLeadingWhite 開頭sw 元素。
NumberStyles.AllowTrailingWhite 末尾s 元素。
NumberStyles.AllowLeadingSign 符號可以出現在 數字之前。
NumberStyles.AllowTrailingSign 數字 後面會出現符號。
NumberStyles.AllowParentheses 符號 元素以 括號形式包含數值。
NumberStyles.AllowThousands 這個元素
NumberStyles.AllowCurrencySymbol 元素 $

NumberStyles.AllowHexSpecifier 使用旗標, s 必須是無前綴的十六進位值字串表示。 例如,“9AF3” 剖析成功,但 “0x9AF3” 則不會。 唯一可能存在於 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite。 (列 NumberStyles 舉採用複合數字風格, NumberStyles.HexNumber包含兩個空白旗。)

s參數是利用為當前系統文化初始化的物件格式資訊NumberFormatInfo進行解析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 若要使用特定文化的格式資訊進行 s 解析,請呼叫該 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。

另請參閱

適用於