Int32.TryParse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を、それと等価な 32 ビット符号付き整数に変換します。 戻り値は、演算が成功したかどうかを示します。
オーバーロード
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int32) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Char>, Int32) |
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
TryParse(String, Int32) |
数値の文字列形式を、それと等価な 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, Int32) |
文字のスパンを値に解析しようとします。 |
TryParse(String, IFormatProvider, Int32) |
文字列を値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int32) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, Int32) |
数値の文字列表現を含む UTF-8 文字範囲を、等価の 32 ビット符号付き整数に変換しようとします。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int32) |
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
TryParse(String, NumberStyles, IFormatProvider, Int32) |
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int32)
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result) = IUtf8SpanParsable<int>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out int result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * int -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Integer) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- provider
- IFormatProvider
utf8Text
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- Int32
戻り値には、正常に解析 utf8Text
された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 utf8Text
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Char>, Int32)
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] int % result);
public static bool TryParse (ReadOnlySpan<char> s, out int result);
static member TryParse : ReadOnlySpan<char> * int -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Integer) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を格納しているスパン。
- result
- Int32
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等価の 32 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null
または である場合、または Emptyが に準拠style
している形式ではない場合、または Int32.MinValue より小さい数値または Int32.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
適用対象
TryParse(String, Int32)
数値の文字列形式を、それと等価な 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] int % result);
public static bool TryParse (string s, out int result);
public static bool TryParse (string? s, out int result);
static member TryParse : string * int -> bool
Public Shared Function TryParse (s As String, ByRef result As Integer) As Boolean
パラメーター
- s
- String
変換する数値を含む文字列。
- result
- Int32
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等価の 32 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null
または である場合、または Emptyが正しい形式ではない場合、または Int32.MinValue より小さい数値または Int32.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
例
次の例では、 メソッドを Int32.TryParse(String, Int32) さまざまな文字列値で呼び出します。
using namespace System;
void TryToParse(String^ value)
{
Int32 number;
bool result = Int32::TryParse(value, number);
if (result) {
Console::WriteLine("Converted '{0}' to {1}.", value, number);
}
else {
if (value == nullptr) value = "";
Console::WriteLine("Attempted conversion of '{0}' failed.", value);
}
}
void main()
{
TryToParse(nullptr);
TryToParse("160519");
TryToParse("9432.0");
TryToParse("16,667");
TryToParse(" -322 ");
TryToParse("+4302");
TryToParse("(100);");
TryToParse("01FA");
}
// The example displays the following output:
// Attempted conversion of '' failed.
// Converted '160519' to 160519.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100);' failed.
// Attempted conversion of '01FA' failed.
using System;
public class Example
{
public static void Main()
{
string[] values = { null, "160519", "9432.0", "16,667",
" -322 ", "+4302", "(100);", "01FA" };
foreach (var value in values)
{
int number;
bool success = int.TryParse(value, out number);
if (success)
{
Console.WriteLine($"Converted '{value}' to {number}.");
}
else
{
Console.WriteLine($"Attempted conversion of '{value ?? "<null>"}' failed.");
}
}
}
}
// The example displays the following output:
// Attempted conversion of '<null>' failed.
// Converted '160519' to 160519.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100);' failed.
// Attempted conversion of '01FA' failed.
open System
let values =
[ null; "160519"; "9432.0"; "16,667"
" -322 "; "+4302"; "(100);"; "01FA" ]
for value in values do
match Int32.TryParse value with
| true, number ->
printfn $"Converted '{value}' to {number}."
| _ ->
printfn $"""Attempted conversion of '{if isNull value then "<null>" else value}' failed."""
// The example displays the following output:
// Attempted conversion of '<null>' failed.
// Converted '160519' to 160519.
// Attempted conversion of '9432.0' failed.
// Attempted conversion of '16,667' failed.
// Converted ' -322 ' to -322.
// Converted '+4302' to 4302.
// Attempted conversion of '(100);' failed.
// Attempted conversion of '01FA' failed.
Module Example
Public Sub Main()
Dim values() As String = { Nothing, "160519", "9432.0", "16,667",
" -322 ", "+4302", "(100);",
"01FA" }
For Each value In values
Dim number As Integer
Dim success As Boolean = Int32.TryParse(value, number)
If success Then
Console.WriteLine("Converted '{0}' to {1}.", value, number)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.",
If(value ,"<null>"))
End If
Next
End Sub
End Module
' The example displays the following output to the console:
' Attempted conversion of '<null>' failed.
' Converted '160519' to 160519.
' Attempted conversion of '9432.0' failed.
' Attempted conversion of '16,667' failed.
' Converted ' -322 ' to -322.
' Converted '+4302' to 4302.
' Attempted conversion of '(100)' failed.
' Attempted conversion of '01FA' failed.
この例ではメソッドが TryParse(String, Int32) 変換できない文字列の一部を次に示します。
"9432.0". 文字列に小数点を含めることができないため、変換は失敗します。整数の数字のみを含む必要があります。
"16,667". 文字列にグループ区切り記号を含めることができないため、変換は失敗します。整数の数字のみを含む必要があります。
"(100)". 現在のカルチャ NumberFormatInfo.NegativeSign の プロパティと NumberFormatInfo.NumberNegativePattern プロパティで定義されている負の符号以外の符号を文字列に含めることはできません。
"01FA" 文字列に 16 進数を含めることができないため、変換は失敗します。10 進数のみを含む必要があります。
注釈
メソッドは TryParse メソッドに Parse 似ていますが、変換が失敗した場合、 TryParse メソッドは例外をスローしません。 例外処理を使用して、 が無効で正常に解析できないイベントs
で をテストFormatExceptionする必要がなくなります。
パラメーターには s
、次の形式の数値が含まれています。
[ws][sign]digits[ws]
角かっこ ([ と ]) の項目は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 |
sign | 省略可能な記号。 |
数値 | 0 から 9 までの数字のシーケンス。 |
パラメーターは s
、 スタイルを使用して NumberStyles.Integer 解釈されます。 10 進数に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 に存在できるカルチャ固有の書式設定情報と共にスタイル要素を明示的に s
定義するには、 メソッドを使用します Int32.TryParse(String, NumberStyles, IFormatProvider, Int32) 。
パラメーターは s
、現在のシステム カルチャ用に初期化されたオブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 詳細については、「CurrentInfo」を参照してください。
メソッドのこのオーバーロードは、 TryParse パラメーター内のすべての数字を s
10 進数として解釈します。 16 進数の文字列表現を解析するには、 オーバーロードを Int32.TryParse(String, NumberStyles, IFormatProvider, Int32) 呼び出します。
こちらもご覧ください
- Parse(String)
- ToString()
- .NET での数値文字列の解析
- サンプル: .NET Core WinForms 書式設定ユーティリティ (C#)
- サンプル: .NET Core WinForms 書式設定ユーティリティ (Visual Basic)
適用対象
TryParse(ReadOnlySpan<Char>, IFormatProvider, Int32)
文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result) = ISpanParsable<int>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out int result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * int -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Integer) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
解析する文字のスパン。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- Int32
このメソッドが返されると、 には、正常に解析 s
された結果、または失敗した場合に未定義の値が格納されます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 false
は 。
適用対象
TryParse(String, IFormatProvider, Int32)
文字列を値に解析しようとします。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result) = IParsable<int>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out int result);
static member TryParse : string * IFormatProvider * int -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Integer) As Boolean
パラメーター
- s
- String
解析する文字列。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- Int32
このメソッドが戻ったとき、 には、正常に解析 s
された結果または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int32)
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result) = System::Numerics::INumberBase<int>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out int result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * int -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Integer) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- style
- NumberStyles
に utf8Text
存在できる数値スタイルのビットごとの組み合わせ。
- provider
- IFormatProvider
utf8Text
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- Int32
戻り値には、正常に解析 utf8Text
された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 utf8Text
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Byte>, Int32)
数値の文字列表現を含む UTF-8 文字スパンを、等価の 32 ビット符号付き整数に変換しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] int % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out int result);
static member TryParse : ReadOnlySpan<byte> * int -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Integer) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
変換する数値を表す UTF-8 文字を含むスパン。
- result
- Int32
このメソッドが戻るときに、 には、変換が成功した場合は に utf8Text
含まれる数値と等価の 32 ビット符号付き整数値が格納され、変換に失敗した場合は 0 が格納されます。 このパラメーターは、初期化されていない状態で渡されます。result にもともと入っていた値は上書きされます。
戻り値
utf8Text
が正常に変換された場合は true
。それ以外の場合は false
。
適用対象
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int32)
指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result) = System::Numerics::INumberBase<int>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out int result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out int result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * int -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Integer) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を格納しているスパン。 このスパンは、style
で指定されたスタイルを使用して解釈されます。
- style
- NumberStyles
s
で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- Int32
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等価の 32 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null
または の形式style
でない場合、または Int32.MinValue より小さい数値または Int32.MaxValue より大きい数値を表す場合s
、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
適用対象
TryParse(String, NumberStyles, IFormatProvider, Int32)
指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 32 ビット符号付き整数に変換します。 戻り値は、変換が成功したかどうかを示します。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] int % result) = System::Numerics::INumberBase<int>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out int result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out int result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * int -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Integer) As Boolean
パラメーター
- s
- String
変換する数値を含む文字列。 文字列は、style
で指定されたスタイルを使用して解釈されます。
- style
- NumberStyles
s
で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- Int32
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等価の 32 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null
または の形式style
でない場合、または Int32.MinValue より小さい数値または Int32.MaxValue より大きい数値を表す場合s
、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
例外
例
次の例では、 Int32.TryParse(String, NumberStyles, IFormatProvider, Int32) さまざまな文字列と値を使用して メソッドを NumberStyles 呼び出します。
using namespace System;
using namespace System::Globalization;
void CallTryParse(String^ stringToConvert, NumberStyles styles)
{
Int32 number;
CultureInfo^ provider;
// If currency symbol is allowed, use en-US culture.
if (((Int32) (styles & NumberStyles::AllowCurrencySymbol)) > 0)
provider = gcnew CultureInfo("en-US");
else
provider = CultureInfo::InvariantCulture;
bool result = Int32::TryParse(stringToConvert, styles,
provider, number);
if (result)
Console::WriteLine("Converted '{0}' to {1}.", stringToConvert, number);
else
Console::WriteLine("Attempted conversion of '{0}' failed.",
Convert::ToString(stringToConvert));
}
void main()
{
String^ numericString;
NumberStyles styles;
numericString = "106779";
styles = NumberStyles::Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles::None;
CallTryParse(numericString, styles);
styles = NumberStyles::AllowLeadingSign;
CallTryParse(numericString, styles);
numericString = "301677-";
CallTryParse(numericString, styles);
styles = styles | NumberStyles::AllowTrailingSign;
CallTryParse(numericString, styles);
numericString = "$10634";
styles = NumberStyles::Integer;
CallTryParse(numericString, styles);
styles = NumberStyles::Integer | NumberStyles::AllowCurrencySymbol;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles::Integer | NumberStyles::AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles::Integer | NumberStyles::AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
styles = NumberStyles::Integer | NumberStyles::AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles::Integer | NumberStyles::AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles::HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles::HexNumber);
Console::ReadLine();
}
// The example displays the following output:
// Converted '106779' to 106779.
// Attempted conversion of '-30677' failed.
// Converted '-30677' to -30677.
// Attempted conversion of '301677-' failed.
// Converted '301677-' to -301677.
// Attempted conversion of '$10634' failed.
// Converted '$10634' to 10634.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to 32961.
// Attempted conversion of '0x80C1' failed.
using System;
using System.Globalization;
public class StringParsing
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "106779";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-30677";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
styles = NumberStyles.AllowLeadingSign;
CallTryParse(numericString, styles);
numericString = "301677-";
CallTryParse(numericString, styles);
styles = styles | NumberStyles.AllowTrailingSign;
CallTryParse(numericString, styles);
numericString = "$10634";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
styles = NumberStyles.Integer | NumberStyles.AllowCurrencySymbol;
CallTryParse(numericString, styles);
numericString = "10345.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "22,593";
styles = NumberStyles.Integer | NumberStyles.AllowThousands;
CallTryParse(numericString, styles);
numericString = "12E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "12E03";
CallTryParse(numericString, styles);
numericString = "80c1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x80C1";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
CultureInfo provider;
// If currency symbol is allowed, use en-US culture.
if ((styles & NumberStyles.AllowCurrencySymbol) > 0)
provider = new CultureInfo("en-US");
else
provider = CultureInfo.InvariantCulture;
bool success = int.TryParse(stringToConvert, styles,
provider, out int number);
if (success)
Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
else
Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
}
}
// The example displays the following output to the console:
// Converted '106779' to 106779.
// Attempted conversion of '-30677' failed.
// Converted '-30677' to -30677.
// Attempted conversion of '301677-' failed.
// Converted '301677-' to -301677.
// Attempted conversion of '$10634' failed.
// Converted '$10634' to 10634.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to 32961.
// Attempted conversion of '0x80C1' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) styles =
let provider =
// If currency symbol is allowed, use en-US culture.
if int (styles &&& NumberStyles.AllowCurrencySymbol) > 0 then
CultureInfo "en-US"
else
CultureInfo.InvariantCulture
match Int32.TryParse(stringToConvert, styles, provider) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
[<EntryPoint>]
let main _ =
let numericString = "106779"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-30677"
let styles = NumberStyles.None
callTryParse numericString styles
let styles = NumberStyles.AllowLeadingSign
callTryParse numericString styles
let numericString = "301677-"
callTryParse numericString styles
let styles = styles ||| NumberStyles.AllowTrailingSign
callTryParse numericString styles
let numericString = "$10634"
let styles = NumberStyles.Integer
callTryParse numericString styles
let styles = NumberStyles.Integer ||| NumberStyles.AllowCurrencySymbol
callTryParse numericString styles
let numericString = "10345.00"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "10345.72"
let styles = NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint
callTryParse numericString styles
let numericString = "22,593"
let styles = NumberStyles.Integer ||| NumberStyles.AllowThousands
callTryParse numericString styles
let numericString = "12E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "12E03"
callTryParse numericString styles
let numericString = "80c1"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x80C1"
callTryParse numericString NumberStyles.HexNumber
0
// The example displays the following output to the console:
// Converted '106779' to 106779.
// Attempted conversion of '-30677' failed.
// Converted '-30677' to -30677.
// Attempted conversion of '301677-' failed.
// Converted '301677-' to -301677.
// Attempted conversion of '$10634' failed.
// Converted '$10634' to 10634.
// Converted '10345.00' to 10345.
// Attempted conversion of '10345.72' failed.
// Converted '22,593' to 22593.
// Attempted conversion of '12E-01' failed.
// Converted '12E03' to 12000.
// Converted '80c1' to 32961.
// Attempted conversion of '0x80C1' failed.
Imports System.Globalization
Module StringParsing
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "106779"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-30677"
styles = NumberStyles.None
CallTryParse(numericString, styles)
styles = NumberStyles.AllowLeadingSign
CallTryParse(numericString, styles)
numericString = "301677-"
CallTryParse(numericString, styles)
styles = styles Or NumberStyles.AllowTrailingSign
CallTryParse(numericString, styles)
numericString = "$10634"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
styles = NumberStyles.Integer Or NumberStyles.AllowCurrencySymbol
CallTryParse(numericString, styles)
numericString = "10345.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "22,593"
styles = NumberStyles.Integer Or NumberStyles.AllowThousands
CallTryParse(numericString, styles)
numericString = "12E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "12E03"
CallTryParse(numericString, styles)
numericString = "80c1"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x80C1"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As Integer
Dim provider As CultureInfo
' If currency symbol is allowed, use en-US culture.
If CBool(styles And NumberStyles.AllowCurrencySymbol) Then
provider = CultureInfo.CurrentCulture
Else
provider = New CultureInfo("en-US")
End If
Dim result As Boolean = Int32.TryParse(stringToConvert, styles, _
provider, number)
If result Then
Console.WriteLine("Converted '{0}' to {1}.", stringToConvert, number)
Else
Console.WriteLine("Attempted conversion of '{0}' failed.", _
Convert.ToString(stringToConvert))
End If
End Sub
End Module
' The example displays the following output to the console:
' Converted '106779' to 106779.
' Attempted conversion of '-30677' failed.
' Converted '-30677' to -30677.
' Attempted conversion of '301677-' failed.
' Converted '301677-' to -301677.
' Attempted conversion of '$10634' failed.
' Converted '$10634' to 10634.
' Converted '10345.00' to 10345.
' Attempted conversion of '10345.72' failed.
' Converted '22,593' to 22593.
' Attempted conversion of '12E-01' failed.
' Converted '12E03' to 12000.
' Converted '80c1' to 32961.
' Attempted conversion of '0x80C1' failed.
注釈
メソッドは TryParse メソッドに Parse 似ていますが、 TryParse メソッドは変換に失敗した場合に例外をスローしません。 無効であり、正常に解析できないイベントs
で 例外処理を使用して をテストFormatExceptionする必要がなくなります。
パラメーターは style
、解析操作を成功させるために パラメーターで s
許可されるスタイル要素 (空白や正または負の符号など) を定義します。 列挙体のビット フラグ NumberStyles の組み合わせである必要があります。 の style
値に応じて、 パラメーターに s
次の要素を含めることができます。
[ws][$][sign][digits,]digits[.fractional_digits][e[sign]digits][ws]
または、 パラメーターに が style
含まれている場合は AllowHexSpecifier。
[ws]hexdigits[ws]
角かっこ ([ と ]) の項目は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 空白は、 が フラグを含む場合は のs 先頭に、フラグが含NumberStyles.AllowLeadingWhiteまれている場合style は のs 末尾にNumberStyles.AllowTrailingWhite表示style されます。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、 パラメーターの CurrencyPositivePatternNumberFormatInfo メソッドによって返されるオブジェクトの provider プロパティによってGetFormat定義されます。 に フラグが含まれている場合style は、通貨記号を にs NumberStyles.AllowCurrencySymbol表示できます。 |
sign | 省略可能な記号。 に または NumberStyles.AllowTrailingSign フラグが含まれている場合style は、s 記号記号をNumberStyles.AllowLeadingSign表示できます。 |
数値 | 0 から 9 までの数字のシーケンス。 |
, | カルチャ固有の桁区切り記号。 でprovider 指定されたカルチャの桁区切り記号は、 に フラグが含まれている場合style にs NumberStyles.AllowThousands表示できます。 |
. | カルチャ固有の小数点記号。 でprovider 指定されたカルチャの小数点記号は、 に フラグが含まれている場合style にs NumberStyles.AllowDecimalPoint表示できます。 |
fractional_digits | 数字 0 が 1 回以上出現します。 小数部の数字は、 に s フラグがNumberStyles.AllowDecimalPoint含まれている場合style にのみ表示されます。 |
e | 値が指数表記で表されることを示す 'e' または 'E' 文字。 フラグが含まれている場合style 、パラメーターはs 指数表記で数値をNumberStyles.AllowExponent表すことができます。 |
hexdigits | 0 から f、または 0 から F までの 16 進数のシーケンス。 |
注意
の終端の NUL (U+0000) 文字 s
は、引数の style
値に関係なく、解析操作では無視されます。
10 進数のみの文字列 (フラグに NumberStyles.None 対応) は常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、この入力文字列に存在する必要がない要素を制御します。 次の表は、 に存在する可能性がある要素に対する個々 NumberStyles のメンバーの s
影響を示しています。
非複合 NumberStyles 値 | 数字に加えて、 で許可される要素 |
---|---|
NumberStyles.None | 10 進数のみ。 |
NumberStyles.AllowDecimalPoint | 小数点 (.) 要素と fractional_digits 要素。 ただし、 fractional_digits は 1 つ以上の 0 桁のみで構成する必要があります。または、 メソッドは を返します false 。 |
NumberStyles.AllowExponent | パラメーターでは s 、指数表記を使用することもできます。 が指数表記で数値を表す場合 s は、0 以外の小数部を含まないデータ型の範囲内の Int32 整数を表す必要があります。 |
NumberStyles.AllowLeadingWhite | の先頭s にある ws 要素。 |
NumberStyles.AllowTrailingWhite | の末尾s にある ws 要素。 |
NumberStyles.AllowLeadingSign | 記号は 数字の前に表示できます。 |
NumberStyles.AllowTrailingSign | 数字 の後に記号が表示される場合があります。 |
NumberStyles.AllowParentheses | 数値を囲むかっこの形式の sign 要素。 |
NumberStyles.AllowThousands | 桁区切り記号 (,) 要素。 |
NumberStyles.AllowCurrencySymbol | $ 要素。 |
NumberStyles.Currency | すべての要素。 パラメーターは s 、指数表記で 16 進数または数値を表すことはできません。 |
NumberStyles.Float | の先頭または末尾の s ws 要素、の先頭のs 符号、および小数点 (.) 記号。 パラメーターでは s 、指数表記を使用することもできます。 |
NumberStyles.Number | ws、sign、thousands separator (,)、および decimal point (.) 要素。 |
NumberStyles.Any | を除く s すべてのスタイルは、16 進数を表すことはできません。 |
フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 s
プレフィックスのない 16 進値を指定する必要があります。 たとえば、"C9AF3" は正常に解析されますが、"0xC9AF3" では解析されません。 に存在できるその他の style
フラグは NumberStyles.AllowLeadingWhite と NumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStyles 、 NumberStyles.HexNumber両方の空白フラグを含む複合スタイル があります)。
パラメーターはprovider
IFormatProvider、オブジェクトや オブジェクトなどのCultureInfo実装でありNumberFormatInfo、そのメソッドは GetFormat オブジェクトをNumberFormatInfo返します。 オブジェクトは NumberFormatInfo 、 の形式に関するカルチャ固有の s
情報を提供します。 が null
の場合provider
は、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。