Int16.Parse 方法

定義

將數字的字串表示轉換成它的對等 16 位元帶正負號的整數。

多載

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

將指定樣式和特定文化特性格式之數字的字串表示轉換成它的對等 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 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style 不相容。

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

-或-

s 包含非零的小數數字。

範例

下列範例會使用各種 styleprovider 參數來剖析值的字串表示 Int16

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.

備註

參數 style 會定義樣式專案 (,例如空白字元或正負號) ,在 參數中 s 允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 styles 參數可能包含下列元素:

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

或者,如果 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。 如果包含 旗標,則空白字元可以出現在 的 s 開頭,如果包含 旗標, NumberStyles.AllowTrailingWhite 則會出現 style 在 結尾 sNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 和 NumberFormatInfo.CurrencyNegativePattern 屬性所 NumberFormatInfo.CurrencyPositivePattern 定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可以出現在 s 中。
簽署 選擇性符號。 如果 style 包含 旗標, NumberStyles.AllowLeadingSign 則符號可以出現在 的 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 結尾 s 。 如果 style 包含 NumberStyles.AllowParentheses 旗標,可以使用 s 括弧來表示負值。
數字 從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符號符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的千位分隔符號符號可以出現在 中 s
. 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前文化特性的小數點符號可以出現在 中 s
fractional_digits 0 位數的序列。 如果 style 包含 旗標, NumberStyles.AllowDecimalPoint 則小數位數可以出現在 中 s 。 如果 0 以外的任何數位出現在 fractional_digits,則方法會擲回 OverflowException
e 'e' 或 'E' 字元,表示 s 可以用指數標記法表示。 如果 style 包含 旗標,參數 s 可以表示指數標記法的數位 NumberStyles.AllowExponent 。 不過, s 參數必須代表資料類型範圍 Int16 中的數位,而且不能有非零的小數部分。
hexdigits 從 0 到 f 或 0 到 F 的十六進位數位序列。

注意

不論引數的值 style 為何,剖析作業都會忽略 中 s 任何終止的 NUL (U+0000) 字元。

只有 數位的 字串 (對應至 NumberStyles.None 樣式) 一律會成功剖析。 大部分剩餘 NumberStyles 的成員控制項元素可能存在,但不需要存在於此輸入字串中。 下表指出個別 NumberStyles 成員如何影響 中 s 可能存在的專案。

非複合 NumberStyles 值 除了數位之外,也允許的專案
NumberStyles.None 僅限十進位數。
NumberStyles.AllowDecimalPoint fractional_digits專案。 不過, fractional_digits 必須只包含一或多個 0 位數或 OverflowException 擲回 。
NumberStyles.AllowExponent 參數 s 也可以使用指數標記法。
NumberStyles.AllowLeadingWhite 開頭的 sws元素。
NumberStyles.AllowTrailingWhite 結尾處的 sws元素。
NumberStyles.AllowLeadingSign 符號可以出現在 數位之前。
NumberStyles.AllowTrailingSign 符號可以出現在 數位之後。
NumberStyles.AllowParentheses 以括弧括住數值形式的 sign 元素。
NumberStyles.AllowThousands 元素。
NumberStyles.AllowCurrencySymbol $ 項目。

NumberStyles.AllowHexSpecifier如果使用旗標, s 必須是不含前置詞之十六進位值的字串表示。 例如,「9AF3」 會成功剖析,但 「0x9AF3」 則不會。 唯一可以出現在 中的 style 其他旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhite 。 (列舉 NumberStyles 具有複合編號樣式, NumberStyles.HexNumber 其中包含兩個空白字元旗標。)

參數 providerIFormatProvider 方法取得 NumberFormatInfo 物件的實 GetFormat 作。 物件 NumberFormatInfo 提供有關 格式 s 的文化特性特定資訊。 如果 providernull ,則會 NumberFormatInfo 使用目前文化特性的 物件。

另請參閱

適用於

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

來源:
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

將 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

將指定特定文化特性格式之數字的字串表示轉換成它的對等 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;
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.

備註

參數 s 包含一些格式:

[ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性的空白字元。
簽署 選擇性符號。
數字 介於 0 到 9 的數位序列。

參數 s 會使用 NumberStyles.Integer 樣式來解譯。 除了十進位數之外,在 中只允許開頭和尾端空格與前置符號。 s 若要明確定義樣式專案以及可以存在於 s 中的特定文化特性格式資訊,請使用 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。

參數 provider 是取得 IFormatProvider 物件的實作 NumberFormatInfo 。 提供 NumberFormatInfo 有關 格式 s 的文化特性特定資訊。 如果 providernullNumberFormatInfo 則會使用目前文化特性的 。

另請參閱

適用於

Parse(String)

來源:
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的數位。

範例

下列範例示範如何使用 方法,將字串值轉換成 16 位帶正負號的整數值 Int16.Parse(String) 。 產生的整數值接著會顯示至主控台。

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.

備註

參數 s 包含一些格式:

[ws][sign]digits[ws]

在方括號 ([ 和 ]) 中的項目是選擇性的項目。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。
簽署 選擇性符號。
數字 介於 0 到 9 的數位序列。

參數 s 會使用 NumberStyles.Integer 樣式來解譯。 除了整數值的十進位數之外,只允許前置和尾端空格與前置符號。 若要明確定義可以存在於 中的 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

將字元範圍剖析為值。

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

將 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

將指定樣式之數字的字串表示轉換成它的對等 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 不是 AllowHexSpecifierHexNumber 值的組合。

s 的格式與 style 不相容。

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

-或-

s 包含非零的小數數字。

範例

下列範例會 Int16.Parse(String, NumberStyles) 使用 方法來剖析使用 en-US 文化特性的值字串表示 Int16

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.

備註

參數 style 會定義樣式專案 (,例如空白字元或符號符號) ,參數中 s 允許剖析作業成功。 它必須是列舉中的 NumberStyles 位旗標組合。 根據 的值 styles 參數可能包含下列元素:

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

或者,如果 style 包含 AllowHexSpecifier

[ws]hexdigits[ws]

方括弧中的專案 ([ 和 ]) 是選擇性專案。 下表說明每個元素。

元素 描述
ws 選擇性空白字元。 如果包含 旗標,則空白字元可以出現在 的 s 開頭,如果包含 旗標, NumberStyles.AllowTrailingWhite 則會出現 style 在 結尾 sNumberStyles.AllowLeadingWhitestyle
$ 特定文化特性的貨幣符號。 字串中的位置是由目前文化特性的 和 NumberFormatInfo.CurrencyNegativePattern 屬性所 NumberFormatInfo.CurrencyPositivePattern 定義。 如果 style 包含 NumberStyles.AllowCurrencySymbol 旗標,則目前文化特性的貨幣符號可以出現在 s 中。
簽署 選擇性符號。 如果 style 包含 旗標, NumberStyles.AllowLeadingSign 則符號可以出現在 的 s 開頭,如果 style 包含 NumberStyles.AllowTrailingSign 旗標,則它可能會出現在 結尾 s 。 如果包含 NumberStyles.AllowParentheses 旗標,則可以在 中使用 s 括弧來表示負值 style
數字 從 0 到 9 的數位序列。
, 特定文化特性的千位分隔符號符號。 如果 style 包含 NumberStyles.AllowThousands 旗標,則目前文化特性的千位分隔符號符號可能會出現在 中 s
. 特定文化特性的小數點符號。 如果 style 包含 NumberStyles.AllowDecimalPoint 旗標,則目前的文化特性小數點符號可能會出現在 中 s
fractional_digits 0 位數的序列。 如果 style 包含 旗標, NumberStyles.AllowDecimalPoint 則可以在 中 s 顯示小數位數。 如果 0 以外的任何數位出現在 fractional_digits中,此方法會 OverflowException 擲回 。
e 'e' 或 'E' 字元,表示 s 可以用指數標記法表示。 如果 style 包含 NumberStyles.AllowExponent 旗標,參數 s 可以代表指數標記法的數位。 不過, s 參數必須代表資料類型範圍 Int16 中的數位,而且不能有非零的小數部分。
hexdigits 從 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 開頭的 sws元素。
NumberStyles.AllowTrailingWhite 結尾處的 sws專案。
NumberStyles.AllowLeadingSign 符號可以出現在 數位之前。
NumberStyles.AllowTrailingSign 符號可以出現在 數位之後。
NumberStyles.AllowParentheses 以括弧括住數值形式的 sign 元素。
NumberStyles.AllowThousands 專案。
NumberStyles.AllowCurrencySymbol $ 項目。

NumberStyles.AllowHexSpecifier如果使用 旗標, s 必須是不含前置詞之十六進位值的字串表示。 例如,「9AF3」 會成功剖析,但 「0x9AF3」 則不會。 唯一可以存在的 style 旗標是 NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteNumberStyles (列舉具有複合編號樣式, NumberStyles.HexNumber 其中包含兩個空白字元旗標。)

參數 s 會使用針對目前系統文化特性初始化的 物件中的 NumberFormatInfo 格式資訊進行剖析。 如需詳細資訊,請參閱NumberFormatInfo.CurrentInfo。 若要使用特定文化特性的格式資訊剖析 s ,請呼叫 Int16.Parse(String, NumberStyles, IFormatProvider) 方法。

另請參閱

適用於