Int16.Parse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を、それと等価な 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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
実装
例外
s
が null
です。
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
次の要素を含めることができます。
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
または、 が含まれているAllowHexSpecifier場合style
は 、
[ws]hexdigits[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 空白は、 が フラグを含む場合は のs 先頭に、フラグが含NumberStyles.AllowLeadingWhiteまれている場合style は のs 末尾にNumberStyles.AllowTrailingWhite表示style されます。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、現在のカルチャの NumberFormatInfo.CurrencyPositivePattern および NumberFormatInfo.CurrencyNegativePattern プロパティによって定義されます。 に フラグが含まれている場合style は、現在のカルチャの通貨記号を NumberStyles.AllowCurrencySymbol にs 表示できます。 |
sign | 省略可能な記号。 署名は、 フラグを含む場合は のs 先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合style は のs 末尾にNumberStyles.AllowTrailingSign表示style されます。 に フラグが含まれている場合style は、かっこを使用s して負の値をNumberStyles.AllowParentheses示すことができます。 |
数値 | 0 から 9 までの数字のシーケンス。 |
, | カルチャ固有の桁区切り記号。 現在のカルチャの桁区切り記号は、 に フラグが含まれている場合style にs NumberStyles.AllowThousands表示できます。 |
. | カルチャ固有の小数点記号。 に フラグが含まれている場合style は、現在のカルチャの小数点記号を NumberStyles.AllowDecimalPoint にs 表示できます。 |
fractional_digits | 0 桁のシーケンス。 フラグが含まれている場合style 、小数部の数字が にs NumberStyles.AllowDecimalPoint表示されます。
fractional_digitsに 0 以外の数字が表示された場合、 メソッドは をOverflowExceptionスローします。 |
e | 指数表記で表すことができることを示す s 'e' または 'E' 文字。 フラグが含まれている場合style 、パラメーターはs 指数表記で数値を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 | の先頭s にある ws 要素。 |
NumberStyles.AllowTrailingWhite | の末尾s にある ws 要素。 |
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
、 メソッドが IFormatProviderGetFormat オブジェクトを取得する実装です NumberFormatInfo 。 オブジェクトは NumberFormatInfo 、 の形式に関するカルチャ固有の s
情報を提供します。 が null
の場合provider
は、現在の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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
実装
適用対象
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
s
に関するカルチャに固有の書式設定情報を提供する IFormatProvider。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
実装
例外
s
が null
です。
s
が正しい形式ではありません。
s
は、Int16.MinValue 未満または Int16.MaxValue より大きい数値を表します。
例
次の例では、 メソッドを使用して値の 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
、次の形式の数値が含まれています。
[ws][sign]digits[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | 省略可能な空白。 |
sign | 省略可能な記号。 |
数値 | 0 から 9 までの数字のシーケンス。 |
パラメーターは s
、 スタイルを NumberStyles.Integer 使用して解釈されます。 では、10 進数に加えて、先頭と末尾のスペースと先頭の記号のみを使用できます s
。 に存在できるカルチャ固有の書式設定情報と共にスタイル要素を明示的に s
定義するには、 メソッドを Int16.Parse(String, NumberStyles, IFormatProvider) 使用します。
パラメーターは provider
、 オブジェクトを IFormatProvider 取得する実装です NumberFormatInfo 。 は NumberFormatInfo 、 の形式に関するカルチャ固有の s
情報を提供します。 が のnull
場合provider
は、NumberFormatInfo現在のカルチャの が使用されます。
こちらもご覧ください
適用対象
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
変換する数値を含む文字列。
戻り値
s
に格納されている数値と等しい 16 ビット符号付き整数。
例外
s
が null
です。
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 | オプションの空白。 |
sign | 省略可能な記号。 |
数値 | 0 から 9 までの数字のシーケンス。 |
パラメーターは s
、 スタイルを NumberStyles.Integer 使用して解釈されます。 整数値の 10 進数に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 に存在できるスタイル要素を明示的に s
定義するには、 Int16.Parse(String, NumberStyles) メソッドまたは メソッドを Parse 使用します。
パラメーターは s
、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 詳細については、「CurrentInfo」を参照してください。 他のカルチャの書式設定情報を使用して文字列を解析するには、 メソッドまたは メソッドをInt16.Parse(String, NumberStyles, IFormatProvider)使用Int16.Parse(String, 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 です。
戻り値
s
で指定した数値と等しい 16 ビット符号付き整数。
例外
s
が null
です。
s
の形式が style
に準拠していません。
例
次の例では、 メソッドを 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 の組み合わせである必要があります。 の style
値に応じて、 パラメーターに s
次の要素を含めることができます。
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
または、 が含まれているAllowHexSpecifier場合style
は 、
[ws]hexdigits[ws]
角かっこ ([ と ]) の項目は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 空白は、 が フラグを含む場合は のs 先頭に、フラグが含NumberStyles.AllowLeadingWhiteまれている場合style は のs 末尾にNumberStyles.AllowTrailingWhite表示style されます。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、現在のカルチャの NumberFormatInfo.CurrencyPositivePattern および NumberFormatInfo.CurrencyNegativePattern プロパティによって定義されます。 に フラグが含まれている場合style は、現在のカルチャの通貨記号を NumberStyles.AllowCurrencySymbol にs 表示できます。 |
sign | 省略可能な記号。 署名は、 フラグを含む場合は のs 先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合style は のs 末尾にNumberStyles.AllowTrailingSign表示style されます。 に フラグが含まれている場合style 、かっこを使用s して負の値をNumberStyles.AllowParentheses示すことができます。 |
数値 | 0 ~ 9 の数字のシーケンス。 |
, | カルチャ固有の桁区切り記号。 現在のカルチャの桁区切り記号は、 フラグが含まれている場合style にNumberStyles.AllowThousandsにs 表示できます。 |
. | カルチャ固有の小数点記号。 現在のカルチャの小数点記号は、 フラグが含まれている場合style にNumberStyles.AllowDecimalPoints に表示できます。 |
fractional_digits | 0 桁のシーケンス。 フラグが含まれている場合style 、小数部の数字を にs NumberStyles.AllowDecimalPoint表示できます。
fractional_digitsに 0 以外の数字が表示される場合、メソッドは をOverflowExceptionスローします。 |
e | 'e' または 'E' 文字。指数表記で表すことができることを s 示します。 フラグが含まれている場合style 、パラメーターはs 指数表記で数値をNumberStyles.AllowExponent表すことができます。 ただし、 パラメーターは s データ型の範囲内の数値を Int16 表す必要があり、0 以外の小数部を持つことはできません。 |
hexdigits | 0 から f、または 0 から F までの 16 進数のシーケンス。 |
注意
の終端 NUL (U+0000) 文字 s
は、引数の style
値に関係なく、解析操作では無視されます。
数字のみの文字列 (スタイルにNumberStyles.None対応) は常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、この入力文字列に存在する必要がない要素を制御します。 次の表は、 にs
存在する可能性がある要素に個々NumberStylesのメンバーがどのように影響するかを示しています。
非複合 NumberStyles 値 | 数字に加えて、 で許可される要素 |
---|---|
NumberStyles.None | 10 進数のみ。 |
NumberStyles.AllowDecimalPoint | 要素と fractional_digits 要素。 ただし、 fractional_digits は 1 つ以上の 0 桁のみで構成する必要があります。または が OverflowException スローされます。 |
NumberStyles.AllowExponent | パラメーターでは s 指数表記を使用することもできます。 |
NumberStyles.AllowLeadingWhite | の先頭s にある ws 要素。 |
NumberStyles.AllowTrailingWhite | の末尾s にある ws 要素。 |
NumberStyles.AllowLeadingSign | 記号は 数字の前に表示できます。 |
NumberStyles.AllowTrailingSign | 記号は 数字の後に表示できます。 |
NumberStyles.AllowParentheses | 数値を囲むかっこの形式の 符号 要素。 |
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) 。
こちらもご覧ください
適用対象
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示