Int16.Parse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を、それと等価な 16 ビット符号付き整数に変換します。
オーバーロード
Parse(String, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 16 ビット符号付き整数に変換します。 |
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider) |
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 16 ビット符号付き整数に変換します。 |
Parse(String, NumberStyles) |
指定したスタイルの数値の文字列形式を、それと等価の 16 ビット符号付き整数に変換します。 |
Parse(String) |
数値の文字列形式を、それと等価な 16 ビット符号付き整数に変換します。 |
Parse(String, IFormatProvider) |
指定したカルチャに固有の書式による数値の文字列形式を、それと等価な 16 ビット符号付き整数に変換します。 |
Parse(String, NumberStyles, IFormatProvider)
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 16 ビット符号付き整数に変換します。
public:
static short Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
例外
s
が null
です。
style
が NumberStyles 値ではありません。
- または -
style
は AllowHexSpecifier 値と HexNumber 値の組み合わせではありません。
s
の形式が style
に準拠していません。
例
次の例では、さまざまなパラメーターを使用して、 style
provider
値の文字列形式を解析し 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 ます。 の値に応じて style
、パラメーターには s
次の要素を含めることができます。
jax-ws[$]シャープ[数字,] 数字 [.fractional_digits] [e [sign] 数字] [ws]
または、に style
が含まれている場合 AllowHexSpecifier :
[ws] hexdigits [ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | 省略可能な空白。 にフラグが含まれている場合は、の先頭に空白が表示され s style NumberStyles.AllowLeadingWhite ます。または、の末尾にフラグが含まれている場合も s style NumberStyles.AllowTrailingWhite あります。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、 NumberFormatInfo.CurrencyPositivePattern 現在のカルチャのおよびプロパティによって定義され NumberFormatInfo.CurrencyNegativePattern ます。 に s フラグが含まれている場合は、現在のカルチャの通貨記号がに表示され style NumberStyles.AllowCurrencySymbol ます。 |
sign | 省略可能な符号。 にフラグが含まれている場合は、の先頭に符号が表示され s style ます。また、にフラグが含まれている場合は、 NumberStyles.AllowLeadingSign の末尾にも表示され s style NumberStyles.AllowTrailingSign ます。 に s フラグが含まれている場合は、かっこを使用して負の値を示すことができ style NumberStyles.AllowParentheses ます。 |
数値 | 0から9までの一連の数字。 |
, | カルチャ固有の桁区切り記号。 に s フラグが含まれている場合は、現在のカルチャの桁区切り記号がに表示され style NumberStyles.AllowThousands ます。 |
. | カルチャ固有の小数点の記号。 に s フラグが含まれている場合、現在のカルチャの小数点の記号がに表示され style NumberStyles.AllowDecimalPoint ます。 |
fractional_digits | 0の数字のシーケンス。 に s フラグが含まれている場合、には小数点以下の桁数が表示され style NumberStyles.AllowDecimalPoint ます。 Fractional_digits に0以外の数字が含まれている場合、メソッドはをスロー OverflowException します。 |
e | "E" または "E" 文字 s 。指数表記で表すことができることを示します。 に s フラグが含まれている場合、パラメーターは指数表記で数値を表すことができ style NumberStyles.AllowExponent ます。 ただし、パラメーターは、 s データ型の範囲の数値を表す必要があり、 Int16 0 以外の小数部分を持つことはできません。 |
hexdigits | 0から f、または0から F までの16進数のシーケンス。 |
注意
の終端の NUL (U + 0000) 文字 s
は、引数の値に関係なく、解析操作によって無視され style
ます。
数字 のみを含む文字列 (スタイルに対応) は、 NumberStyles.None 常に正常に解析されます。 残りのメンバーのほとんどは、 NumberStyles この入力文字列に存在する必要があるが必須ではない要素を制御します。 次の表は、の各メンバーが、に存在する可能性のある要素にどのように影響するかを示して NumberStyles s
います。
非複合 NumberStyles 値 | 数字に加えてで許可される要素 |
---|---|
NumberStyles.None | 10進数のみ。 |
NumberStyles.AllowDecimalPoint | . 要素と fractional_digits 要素。 ただし、 fractional_digits は1桁以上の0桁で構成する必要があります。それ以外の場合は、 OverflowException がスローされます。 |
NumberStyles.AllowExponent | パラメーターでは、 s 指数表記を使用することもできます。 |
NumberStyles.AllowLeadingWhite | の先頭にある ws 要素 s 。 |
NumberStyles.AllowTrailingWhite | の末尾の ws 要素 s 。 |
NumberStyles.AllowLeadingSign | 数字 の前に記号を付けることができます。 |
NumberStyles.AllowTrailingSign | 数字 の後に符号が表示されます。 |
NumberStyles.AllowParentheses | 数値を囲むかっこ形式の sign 要素。 |
NumberStyles.AllowThousands | 要素 、 要素。 |
NumberStyles.AllowCurrencySymbol | $ 要素。 |
NumberStyles.AllowHexSpecifierフラグが使用されている場合、は、 s
プレフィックスのない16進値の文字列形式である必要があります。 たとえば、"9AF3" は正常に解析されますが、"0x9AF3" は正常に解析されません。 内に存在できるフラグは、とだけ style
です NumberStyles.AllowLeadingWhite NumberStyles.AllowTrailingWhite 。 ( NumberStyles 列挙体には、両方の空白フラグを含む複合数値スタイルがあり NumberStyles.HexNumber ます)。
provider
パラメーターは、 IFormatProvider メソッドが GetFormat オブジェクトを取得する実装です NumberFormatInfo 。 オブジェクトは、 NumberFormatInfo の書式に関するカルチャ固有の情報を提供し s
ます。 がの場合は provider
null
、 NumberFormatInfo 現在のカルチャのオブジェクトが使用されます。
こちらもご覧ください
適用対象
Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
適用対象
Parse(String, NumberStyles)
指定したスタイルの数値の文字列形式を、それと等価の 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 です。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
例外
s
が null
です。
style
が NumberStyles 値ではありません。
- または -
style
は AllowHexSpecifier 値と HexNumber 値の組み合わせではありません。
s
の形式が style
に準拠していません。
例
次の例では、メソッドを使用し Int16.Parse(String, NumberStyles) て、 Int16 en-us カルチャを使用して値の文字列形式を解析します。
using namespace System;
using namespace System::Globalization;
ref class ParseSample
{
public:
static void Main()
{
String^ value;
NumberStyles style;
// Parse a number with a thousands separator (throws an exception).
value = "14,644";
style = NumberStyles::None;
ParseSample::ParseToInt16(value, style);
style = NumberStyles::AllowThousands;
ParseToInt16(value, style);
// Parse a number with a thousands separator and decimal point.
value = "14,644.00";
style = NumberStyles::AllowThousands | NumberStyles::Integer |
NumberStyles::AllowDecimalPoint;
ParseToInt16(value, style);
// Parse a number with a fractional component (throws an exception).
value = "14,644.001";
ParseToInt16(value, style);
// Parse a number in exponential notation.
value = "145E02";
style = style | NumberStyles::AllowExponent;
ParseToInt16(value, style);
// Parse a number in exponential notation with a positive sign.
value = "145E+02";
ParseToInt16(value, style);
// Parse a number in exponential notation with a negative sign
// (throws an exception).
value = "145E-02";
ParseToInt16(value, style);
}
private:
static void ParseToInt16(String^ value, NumberStyles style)
{
try
{
Int16 number = Int16::Parse(value, style);
Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException ^e)
{
Console::WriteLine("Unable to parse '{0}' with style {1}.", value,
style);
}
catch (OverflowException ^e)
{
Console::WriteLine("'{0}' is out of range of the Int16 type.", value);
}
}
};
int main()
{
ParseSample::Main();
Console::ReadLine();
return 0;
}
// The example displays the following output:
// Unable to parse '14,644' with style None.
// Converted '14,644' to 14644.
// Converted '14,644.00' to 14644.
// '14,644.001' is out of range of the Int16 type.
// Converted '145E02' to 14500.
// Converted '145E+02' to 14500.
// '145E-02' is out of range of the Int16 type.
using System;
using System.Globalization;
public class ParseSample
{
public static void Main()
{
string value;
NumberStyles style;
// Parse a number with a thousands separator (throws an exception).
value = "14,644";
style = NumberStyles.None;
ParseToInt16(value, style);
style = NumberStyles.AllowThousands;
ParseToInt16(value, style);
// Parse a number with a thousands separator and decimal point.
value = "14,644.00";
style = NumberStyles.AllowThousands | NumberStyles.Integer |
NumberStyles.AllowDecimalPoint;
ParseToInt16(value, style);
// Parse a number with a fractional component (throws an exception).
value = "14,644.001";
ParseToInt16(value, style);
// Parse a number in exponential notation.
value = "145E02";
style = style | NumberStyles.AllowExponent;
ParseToInt16(value, style);
// Parse a number in exponential notation with a positive sign.
value = "145E+02";
ParseToInt16(value, style);
// Parse a number in exponential notation with a negative sign
// (throws an exception).
value = "145E-02";
ParseToInt16(value, style);
}
private static void ParseToInt16(string value, NumberStyles style)
{
try
{
short number = Int16.Parse(value, style);
Console.WriteLine("Converted '{0}' to {1}.", value, number);
}
catch (FormatException)
{
Console.WriteLine("Unable to parse '{0}' with style {1}.", value,
style.ToString());
}
catch (OverflowException)
{
Console.WriteLine("'{0}' is out of range of the Int16 type.", value);
}
}
}
// The example displays the following output to the console:
// Unable to parse '14,644' with style None.
// Converted '14,644' to 14644.
// Converted '14,644.00' to 14644.
// '14,644.001' is out of range of the Int16 type.
// Converted '145E02' to 14500.
// Converted '145E+02' to 14500.
// '145E-02' is out of range of the Int16 type.
open System
open System.Globalization
let parseToInt16 (value: string) (style: NumberStyles) =
try
let number = Int16.Parse(value, style)
printfn $"Converted '{value}' to {number}."
with
| :? FormatException ->
printfn $"Unable to parse '{value}' with style {style}."
| :? OverflowException ->
printfn $"'{value}' is out of range of the Int16 type."
[<EntryPoint>]
let main _ =
// Parse a number with a thousands separator (throws an exception).
let value = "14,644"
let style = NumberStyles.None
parseToInt16 value style
let style = NumberStyles.AllowThousands
parseToInt16 value style
// Parse a number with a thousands separator and decimal point.
let value = "14,644.00"
let style = NumberStyles.AllowThousands ||| NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
parseToInt16 value style
// Parse a number with a fractional component (throws an exception).
let value = "14,644.001"
parseToInt16 value style
// Parse a number in exponential notation.
let value = "145E02"
let style = style ||| NumberStyles.AllowExponent
parseToInt16 value style
// Parse a number in exponential notation with a positive sign.
let value = "145E+02"
parseToInt16 value style
// Parse a number in exponential notation with a negative sign
// (throws an exception).
let value = "145E-02"
parseToInt16 value style
0
// The example displays the following output to the console:
// Unable to parse '14,644' with style None.
// Converted '14,644' to 14644.
// Converted '14,644.00' to 14644.
// '14,644.001' is out of range of the Int16 type.
// Converted '145E02' to 14500.
// Converted '145E+02' to 14500.
// '145E-02' is out of range of the Int16 type.
Imports System.Globalization
Module ParseSample
Public Sub Main()
Dim value As String
Dim style As NumberStyles
' Parse a number with a thousands separator (throws an exception).
value = "14,644"
style = NumberStyles.None
ParseToInt16(value, style)
style = NumberStyles.AllowThousands
ParseToInt16(value, style)
' Parse a number with a thousands separator and decimal point.
value = "14,644.00"
style = NumberStyles.AllowThousands Or NumberStyles.Integer Or _
NumberStyles.AllowDecimalPoint
ParseToInt16(value, style)
' Parse a number with a fractional component (throws an exception).
value = "14,644.001"
ParseToInt16(value, style)
' Parse a number in exponential notation.
value = "145E02"
style = style Or NumberStyles.AllowExponent
ParseToInt16(value, style)
' Parse a number in exponential notation with a positive sign.
value = "145E+02"
ParseToInt16(value, style)
' Parse a number in exponential notation with a negative sign
' (throws an exception).
value = "145E-02"
ParseToInt16(value, style)
End Sub
Private Sub ParseToInt16(value As String, style As NumberStyles)
Try
Dim number As Short = Int16.Parse(value, style)
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Catch e As FormatException
Console.WriteLine("Unable to parse '{0}' with style {1}.", value, _
style.ToString())
Catch e As OverflowException
Console.WriteLine("'{0}' is out of range of the Int16 type.", value)
End Try
End Sub
End Module
' The example displays the following output to the console:
' Unable to parse '14,644' with style None.
' Converted '14,644' to 14644.
' Converted '14,644.00' to 14644.
' '14,644.001' is out of range of the Int16 type.
' Converted '145E02' to 14500.
' Converted '145E+02' to 14500.
' '145E-02' is out of range of the Int16 type.
注釈
パラメーターは、 style
解析操作を成功させるためにパラメーターで使用できるスタイル要素 (空白や記号など) を定義し s
ます。 列挙体のビットフラグの組み合わせである必要があり NumberStyles ます。 の値に応じて style
、パラメーターには s
次の要素を含めることができます。
jax-ws[$]シャープ[数字,] 数字 [.fractional_digits] [e [sign] 数字] [ws]
または、に style
が含まれている場合 AllowHexSpecifier :
[ws] hexdigits [ws]
角かっこ ([および]) 内の項目は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | 省略可能な空白。 にフラグが含まれている場合は、の先頭に空白が表示され s style NumberStyles.AllowLeadingWhite ます。または、の末尾にフラグが含まれている場合も s style NumberStyles.AllowTrailingWhite あります。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、 NumberFormatInfo.CurrencyPositivePattern 現在のカルチャのおよびプロパティによって定義され NumberFormatInfo.CurrencyNegativePattern ます。 に s フラグが含まれている場合は、現在のカルチャの通貨記号がに表示され style NumberStyles.AllowCurrencySymbol ます。 |
sign | 省略可能な符号。 にフラグが含まれている場合は、の先頭に符号が表示され s style ます。また、にフラグが含まれている場合は、 NumberStyles.AllowLeadingSign の末尾にも表示され s style NumberStyles.AllowTrailingSign ます。 に s フラグが含まれている場合は、かっこを使用して負の値を示すことができ style NumberStyles.AllowParentheses ます。 |
数値 | 0から9までの一連の数字。 |
, | カルチャ固有の桁区切り記号。 に s フラグが含まれている場合は、現在のカルチャの桁区切り記号がに表示され style NumberStyles.AllowThousands ます。 |
. | カルチャ固有の小数点の記号。 に s フラグが含まれている場合、現在のカルチャの小数点の記号がに表示され style NumberStyles.AllowDecimalPoint ます。 |
fractional_digits | 0の数字のシーケンス。 に s フラグが含まれている場合、には小数点以下の桁数が表示され style NumberStyles.AllowDecimalPoint ます。 Fractional_digits に0以外の数字が含まれている場合、メソッドはをスロー OverflowException します。 |
e | "E" または "E" 文字 s 。指数表記で表すことができることを示します。 に s フラグが含まれている場合、パラメーターは指数表記で数値を表すことができ style NumberStyles.AllowExponent ます。 ただし、パラメーターは、 s データ型の範囲の数値を表す必要があり、 Int16 0 以外の小数部分を持つことはできません。 |
hexdigits | 0から f、または0から F までの16進数のシーケンス。 |
注意
の終端の NUL (U + 0000) 文字 s
は、引数の値に関係なく、解析操作によって無視され style
ます。
数字 のみを含む文字列 (スタイルに対応) は、 NumberStyles.None 常に正常に解析されます。 残りのメンバーのほとんどは、 NumberStyles この入力文字列に存在する必要があるが必須ではない要素を制御します。 次の表は、の各メンバーが、に存在する可能性のある要素にどのように影響するかを示して NumberStyles s
います。
非複合 NumberStyles 値 | 数字に加えてで許可される要素 |
---|---|
NumberStyles.None | 10進数のみ。 |
NumberStyles.AllowDecimalPoint | . 要素と fractional_digits 要素。 ただし、 fractional_digits は1桁以上の0桁で構成する必要があります。それ以外の場合は、 OverflowException がスローされます。 |
NumberStyles.AllowExponent | パラメーターでは、 s 指数表記を使用することもできます。 |
NumberStyles.AllowLeadingWhite | の先頭にある ws 要素 s 。 |
NumberStyles.AllowTrailingWhite | の末尾の ws 要素 s 。 |
NumberStyles.AllowLeadingSign | 数字 の前に記号を付けることができます。 |
NumberStyles.AllowTrailingSign | 数字 の後に符号が表示されます。 |
NumberStyles.AllowParentheses | 数値を囲むかっこ形式の sign 要素。 |
NumberStyles.AllowThousands | 要素 、 要素。 |
NumberStyles.AllowCurrencySymbol | $ 要素。 |
NumberStyles.AllowHexSpecifierフラグが使用されている場合、は、 s
プレフィックスのない16進値の文字列形式である必要があります。 たとえば、"9AF3" は正常に解析されますが、"0x9AF3" は正常に解析されません。 内に存在できるフラグは、とだけ style
です NumberStyles.AllowLeadingWhite NumberStyles.AllowTrailingWhite 。 ( NumberStyles 列挙体には、両方の空白フラグを含む複合数値スタイルがあり NumberStyles.HexNumber ます)。
パラメーターは、 s
NumberFormatInfo 現在のシステムカルチャに対して初期化されたオブジェクトの書式情報を使用して解析されます。 詳細については、「NumberFormatInfo.CurrentInfo」を参照してください。 s
特定のカルチャの書式情報を使用して解析するには、メソッドを呼び出し Int16.Parse(String, NumberStyles, IFormatProvider) ます。
こちらもご覧ください
適用対象
Parse(String)
数値の文字列形式を、それと等価な 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
変換する数値を含む文字列。
戻り値
s
に格納されている数値と等しい 16 ビット符号付き整数。
例外
s
が null
です。
s
が正しい形式ではありません。
例
次の例は、メソッドを使用して文字列値を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 | 省略可能な空白。 |
sign | 省略可能な記号。 |
数値 | 0 ~ 9 の範囲の数字のシーケンス。 |
パラメーター s
は、 スタイルを使用して解釈 NumberStyles.Integer されます。 整数値の 10 進数に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 に存在できるスタイル要素を明示的に定義するには、 s
メソッドまたは Int16.Parse(String, NumberStyles) メソッドを使用 Parse します。
パラメーターは、現在のシステム カルチャ用に初期化された オブジェクトの書式設定情報 s
NumberFormatInfo を使用して解析されます。 詳細については、「CurrentInfo」を参照してください。 他のカルチャの書式設定情報を使用して文字列を解析するには、メソッドまたはメソッドを使用し Int16.Parse(String, IFormatProvider) Int16.Parse(String, NumberStyles, IFormatProvider) ます。
こちらもご覧ください
適用対象
Parse(String, IFormatProvider)
指定したカルチャに固有の書式による数値の文字列形式を、それと等価な 16 ビット符号付き整数に変換します。
public:
static short Parse(System::String ^ s, IFormatProvider ^ provider);
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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
例外
s
が null
です。
s
が正しい形式ではありません。
例
次の例では Int16 、メソッドを使用して、値の文字列形式を解析し Int16.Parse(String, IFormatProvider) ます。
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
次のような形式の数値が含まれています。
jax-ws[sign] 数字 [ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | 省略可能な空白。 |
sign | 省略可能な符号。 |
数値 | 0から9までの一連の数字。 |
s
パラメーターは、スタイルを使用して解釈され NumberStyles.Integer ます。 10進数に加えて、では先頭と末尾のスペースだけでなく先頭の記号も使用でき s
ます。 で使用できるカルチャ固有の書式情報と共にスタイル要素を明示的に定義するには、 s
メソッドを使用し Int16.Parse(String, NumberStyles, IFormatProvider) ます。
provider
パラメーターは、 IFormatProvider オブジェクトを取得する実装です NumberFormatInfo 。 は、 NumberFormatInfo の書式に関するカルチャ固有の情報を提供し s
ます。 がの場合は、 provider
null
現在の NumberFormatInfo カルチャのが使用されます。