UInt64.TryParse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
数値の文字列形式を、それと等価の 64 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。
オーバーロード
TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt64) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Char>, UInt64) |
数値のスパン表現の、等価の 64 ビット符号なし整数への変換を試みます。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(String, UInt64) |
数値の文字列形式を、それと等価の 64 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt64) |
文字のスパンを値に解析しようとします。 |
TryParse(String, IFormatProvider, UInt64) |
文字列を値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt64) |
UTF-8 文字のスパンを値に解析しようとします。 |
TryParse(ReadOnlySpan<Byte>, UInt64) |
数値の文字列表現を含む UTF-8 文字スパンを、等価の 64 ビット符号なし整数に変換しようとします。 |
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, UInt64) |
指定したスタイルおよびカルチャ固有の書式による数値のスパン表現を、それと等価な 64 ビット符号なし整数に変換するよう試みます。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(String, NumberStyles, IFormatProvider, UInt64) |
指定したスタイルおよびカルチャ固有の書式による数値の文字列形式を、それと等価な 64 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。 |
TryParse(ReadOnlySpan<Byte>, IFormatProvider, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = IUtf8SpanParsable<System::UInt64>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out ulong result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As ULong) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- provider
- IFormatProvider
utf8Text
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- UInt64
戻り値には、正常に解析 utf8Text
された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 utf8Text
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Char>, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
重要
この API は CLS 準拠ではありません。
数値のスパン表現の、等価の 64 ビット符号なし整数への変換を試みます。 戻り値は変換が成功したか失敗したかを示します。
public:
static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] System::UInt64 % result);
public static bool TryParse (ReadOnlySpan<char> s, out ulong result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, out ulong result);
static member TryParse : ReadOnlySpan<char> * uint64 -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * uint64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As ULong) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を格納しているスパン。
- result
- UInt64
このメソッドから制御が返されたとき、変換に成功した場合は、s
に格納された数値と等価な 64 ビット符号なし整数値が格納されます。変換に失敗した場合は 0 が格納されます。 パラメーターが null
または である場合、または Emptyが正しい形式ではない場合、または UInt64.MinValue より小さい数値または UInt64.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
適用対象
TryParse(String, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
数値の文字列形式を、それと等価の 64 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。
public:
static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] System::UInt64 % result);
[System.CLSCompliant(false)]
public static bool TryParse (string s, out ulong result);
public static bool TryParse (string? s, out ulong result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, out ulong result);
[<System.CLSCompliant(false)>]
static member TryParse : string * uint64 -> bool
static member TryParse : string * uint64 -> bool
Public Shared Function TryParse (s As String, ByRef result As ULong) As Boolean
パラメーター
- s
- String
変換する数値を表す文字列。
- result
- UInt64
このメソッドから制御が返されたとき、変換に成功した場合は、s
に格納された数値と等価な 64 ビット符号なし整数値が格納されます。変換に失敗した場合は 0 が格納されます。 パラメーターが null
または である場合、または Emptyが正しい形式ではない場合、または UInt64.MinValue より小さい数値または UInt64.MaxValue より大きい数値を表す場合s
、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
例
次の例では、文字列配列内の TryParse(String, UInt64) 各要素に対して メソッドを 1 回呼び出します。
string[] numericStrings = { "1293.8", "+1671.7", "28347.",
" 33113684 ", "(0)", "-0", "+1293617",
"18-", "119870", "31,024", " 3127094 ",
"00700000" };
ulong number;
foreach (string numericString in numericStrings)
{
if (UInt64.TryParse(numericString, out number))
Console.WriteLine("Converted '{0}' to {1}.", numericString, number);
else
Console.WriteLine("Cannot convert '{0}' to a UInt64.", numericString);
}
// The example displays the following output:
// Cannot convert '1293.8' to a UInt64.
// Cannot convert '+1671.7' to a UInt64.
// Cannot convert '28347.' to a UInt64.
// Converted ' 33113684 ' to 33113684.
// Cannot convert '(0)' to a UInt64.
// Converted '-0' to 0.
// Converted '+1293617' to 1293617.
// Cannot convert '18-' to a UInt64.
// Converted '119870' to 119870.
// Cannot convert '31,024' to a UInt64.
// Converted ' 3127094 ' to 3127094.
// Converted '0070000' to 70000.
open System
let numericStrings =
[| "1293.8"; "+1671.7"; "28347."
" 33113684 "; "(0)"; "-0"; "+1293617"
"18-"; "119870"; "31,024"; " 3127094 "
"00700000" |]
for numericString in numericStrings do
match UInt64.TryParse numericString with
| true, number ->
printfn $"Converted '{numericString}' to {number}."
| _ ->
printfn $"Cannot convert '{numericString}' to a UInt64."
// The example displays the following output:
// Cannot convert '1293.8' to a UInt64.
// Cannot convert '+1671.7' to a UInt64.
// Cannot convert '28347.' to a UInt64.
// Converted ' 33113684 ' to 33113684.
// Cannot convert '(0)' to a UInt64.
// Converted '-0' to 0.
// Converted '+1293617' to 1293617.
// Cannot convert '18-' to a UInt64.
// Converted '119870' to 119870.
// Cannot convert '31,024' to a UInt64.
// Converted ' 3127094 ' to 3127094.
// Converted '0070000' to 70000.
Dim numericStrings() As String = {"1293.8", "+1671.7", "28347.", _
" 33113684 ", "(0)", "-0", "+1293617", _
"18-", "119870", "31,024", " 3127094 ", _
"0070000" }
Dim number As ULong
For Each numericString As String In numericStrings
If UInt64.TryParse(numericString, number) Then
Console.WriteLine("Converted '{0}' to {1}.", numericString, number)
Else
Console.WriteLine("Cannot convert '{0}' to a UInt64.", numericString)
End If
Next
' The example displays the following output:
' Cannot convert '1293.8' to a UInt64.
' Cannot convert '+1671.7' to a UInt64.
' Cannot convert '28347.' to a UInt64.
' Converted ' 33113684 ' to 33113684.
' Cannot convert '(0)' to a UInt64.
' Converted '-0' to 0.
' Converted '+1293617' to 1293617.
' Cannot convert '18-' to a UInt64.
' Converted '119870' to 119870.
' Cannot convert '31,024' to a UInt64.
' Converted ' 3127094 ' to 3127094.
' Converted '0070000' to 70000.
注釈
メソッドは TryParse(String, UInt64) メソッドに Parse(String) 似ていますが、変換が失敗しても例外はスローされません。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをs
テストFormatExceptionするために例外処理を使用する必要がなくなります。
パラメーターは s
、次の形式の 10 進数の文字列表現である必要があります。
[ws][sign]digits[ws]
角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 |
sign | 省略可能な記号。 有効な符号文字は、現在のカルチャの NumberFormatInfo.NegativeSign プロパティと NumberFormatInfo.PositiveSign プロパティによって決まります。 |
数値 | 0 から 9 までの 10 進数のシーケンス。 |
パラメーターは s
、 スタイルを使用 NumberStyles.Integer して解釈されます。 10 進数に加えて、先頭と末尾に先頭の記号が付いたスペースのみを使用できます。 に存在できるカルチャ固有の書式設定情報を使用してスタイル要素を明示的に s
定義するには、 メソッドを TryParse(String, NumberStyles, IFormatProvider, UInt64) 呼び出します。
注意
パラメーターで s
指定された文字列には、グループ区切り記号または小数点記号を含めることはできません。また、小数部を含めることはできません。
パラメーターは s
、現在のシステム カルチャ用に初期化されたオブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 詳細については、「NumberFormatInfo.CurrentInfo」を参照してください。
このオーバーロードは、パラメーター内のすべての数字を s
10 進数として解釈します。 16 進数の文字列表現を解析するには、代わりに オーバーロードを TryParse(String, NumberStyles, IFormatProvider, UInt64) 呼び出します。
こちらもご覧ください
- Parse
- ToString
- .NET での数値文字列の解析
- サンプル: .NET Core WinForms 書式設定ユーティリティ (C#)
- サンプル: .NET Core WinForms 書式設定ユーティリティ (Visual Basic)
適用対象
TryParse(ReadOnlySpan<Char>, IFormatProvider, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = ISpanParsable<System::UInt64>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out ulong result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As ULong) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
解析する文字のスパン。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- UInt64
このメソッドが返されると、 には、正常に解析 s
された結果、または失敗した場合に未定義の値が格納されます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 false
は 。
適用対象
TryParse(String, IFormatProvider, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
文字列を値に解析しようとします。
public:
static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = IParsable<System::UInt64>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out ulong result);
static member TryParse : string * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As ULong) As Boolean
パラメーター
- s
- String
解析する文字列。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- UInt64
このメソッドが戻ったとき、 には、正常に解析 s
された結果または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 s
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
UTF-8 文字のスパンを値に解析しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = System::Numerics::INumberBase<System::UInt64>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out ulong result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As ULong) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
解析する UTF-8 文字のスパン。
- style
- NumberStyles
に utf8Text
存在できる数値スタイルのビットごとの組み合わせ。
- provider
- IFormatProvider
utf8Text
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- UInt64
戻り値には、正常に解析 utf8Text
された結果、または失敗した場合に未定義の値が含まれます。
戻り値
true
が正常に解析された場合 utf8Text
は 。それ以外の場合 false
は 。
適用対象
TryParse(ReadOnlySpan<Byte>, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
数値の文字列表現を含む UTF-8 文字スパンを、同等の 64 ビット符号なし整数に変換しようとします。
public:
static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] System::UInt64 % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out ulong result);
static member TryParse : ReadOnlySpan<byte> * uint64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As ULong) As Boolean
パラメーター
- utf8Text
- ReadOnlySpan<Byte>
変換する数値を表す UTF-8 文字を含むスパン。
- result
- UInt64
このメソッドが戻るとき、 には、変換が成功した場合は に utf8Text
含まれる数値と等価の 64 ビット符号なし整数値が格納され、変換に失敗した場合は 0 が格納されます。 このパラメーターは、初期化されていない状態で渡されます。result にもともと入っていた値は上書きされます。
戻り値
utf8Text
が正常に変換された場合は true
。それ以外の場合は false
。
適用対象
TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
重要
この API は CLS 準拠ではありません。
指定したスタイルおよびカルチャ固有の書式による数値のスパン表現を、それと等価な 64 ビット符号なし整数に変換するよう試みます。 戻り値は変換が成功したか失敗したかを示します。
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result);
public:
static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = System::Numerics::INumberBase<System::UInt64>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ulong result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out ulong result);
[System.CLSCompliant(false)]
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ulong result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * uint64 -> bool
[<System.CLSCompliant(false)>]
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As ULong) As Boolean
パラメーター
- s
- ReadOnlySpan<Char>
変換する数値を表す文字を格納しているスパン。 スパンは、style
パラメーターで指定されたスタイルを使用して解釈されます。
- style
- NumberStyles
s
で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- UInt64
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等しい 64 ビット符号なし整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null
または の形式style
でない場合、または UInt64.MinValue より小さい数値または UInt64.MaxValue より大きい数値を表す場合s
、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
適用対象
TryParse(String, NumberStyles, IFormatProvider, UInt64)
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
- ソース:
- UInt64.cs
指定したスタイルおよびカルチャ固有の書式による数値の文字列形式を、それと等価な 64 ビット符号なし整数に変換します。 戻り値は変換が成功したか失敗したかを示します。
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result);
public:
static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] System::UInt64 % result) = System::Numerics::INumberBase<System::UInt64>::TryParse;
[System.CLSCompliant(false)]
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out ulong result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ulong result);
[System.CLSCompliant(false)]
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out ulong result);
[<System.CLSCompliant(false)>]
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * uint64 -> bool
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * uint64 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As ULong) As Boolean
パラメーター
- s
- String
変換する数値を表す文字列。 文字列は、style
パラメーターで指定されたスタイルを使用して解釈されます。
- style
- NumberStyles
s
で使用可能な書式を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。
- provider
- IFormatProvider
s
に関するカルチャ固有の書式情報を提供するオブジェクト。
- result
- UInt64
変換が成功した場合、このメソッドが返されるときに、s
に格納された数値と等しい 64 ビット符号なし整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null
または の形式style
でない場合、または UInt64.MinValue より小さい数値または UInt64.MaxValue より大きい数値を表す場合s
、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result
で最初に指定された任意の値が上書きされます。
戻り値
s
が正常に変換された場合は true
。それ以外の場合は false
。
- 属性
例外
例
次の例では、 TryParse(String, NumberStyles, IFormatProvider, UInt64) さまざまな文字列と値を使用して メソッドを NumberStyles 呼び出します。
using System;
using System.Globalization;
public class Example
{
public static void Main()
{
string numericString;
NumberStyles styles;
numericString = "2106034";
styles = NumberStyles.Integer;
CallTryParse(numericString, styles);
numericString = "-10603";
styles = NumberStyles.None;
CallTryParse(numericString, styles);
numericString = "29103674.00";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "10345.72";
styles = NumberStyles.Integer | NumberStyles.AllowDecimalPoint;
CallTryParse(numericString, styles);
numericString = "41792210E-01";
styles = NumberStyles.Integer | NumberStyles.AllowExponent;
CallTryParse(numericString, styles);
numericString = "9112E-01";
CallTryParse(numericString, styles);
numericString = "312E01";
CallTryParse(numericString, styles);
numericString = "FFC86DA1";
CallTryParse(numericString, NumberStyles.HexNumber);
numericString = "0x8F8C";
CallTryParse(numericString, NumberStyles.HexNumber);
}
private static void CallTryParse(string stringToConvert, NumberStyles styles)
{
ulong number;
bool result = UInt64.TryParse(stringToConvert, styles,
CultureInfo.InvariantCulture, out number);
if (result)
Console.WriteLine($"Converted '{stringToConvert}' to {number}.");
else
Console.WriteLine($"Attempted conversion of '{stringToConvert}' failed.");
}
}
// The example displays the following output:
// Converted '2106034' to 2106034.
// Attempted conversion of '-10603' failed.
// Converted '29103674.00' to 29103674.
// Attempted conversion of '10345.72' failed.
// Converted '41792210E-01' to 4179221.
// Attempted conversion of '9112E-01' failed.
// Converted '312E01' to 3120.
// Converted 'FFC86DA1' to 4291325345.
// Attempted conversion of '0x8F8C' failed.
open System
open System.Globalization
let callTryParse (stringToConvert: string) (styles: NumberStyles) =
match UInt64.TryParse(stringToConvert, styles, CultureInfo.InvariantCulture) with
| true, number ->
printfn $"Converted '{stringToConvert}' to {number}."
| _ ->
printfn $"Attempted conversion of '{stringToConvert}' failed."
do
let numericString = "2106034"
let styles = NumberStyles.Integer
callTryParse numericString styles
let numericString = "-10603"
let styles = NumberStyles.None
callTryParse numericString styles
let numericString = "29103674.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 = "41792210E-01"
let styles = NumberStyles.Integer ||| NumberStyles.AllowExponent
callTryParse numericString styles
let numericString = "9112E-01"
callTryParse numericString styles
let numericString = "312E01"
callTryParse numericString styles
let numericString = "FFC86DA1"
callTryParse numericString NumberStyles.HexNumber
let numericString = "0x8F8C"
callTryParse numericString NumberStyles.HexNumber
// The example displays the following output:
// Converted '2106034' to 2106034.
// Attempted conversion of '-10603' failed.
// Converted '29103674.00' to 29103674.
// Attempted conversion of '10345.72' failed.
// Converted '41792210E-01' to 4179221.
// Attempted conversion of '9112E-01' failed.
// Converted '312E01' to 3120.
// Converted 'FFC86DA1' to 4291325345.
// Attempted conversion of '0x8F8C' failed.
Imports System.Globalization
Module Example
Public Sub Main()
Dim numericString As String
Dim styles As NumberStyles
numericString = "2106034"
styles = NumberStyles.Integer
CallTryParse(numericString, styles)
numericString = "-10603"
styles = NumberStyles.None
CallTryParse(numericString, styles)
numericString = "29103674.00"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "10345.72"
styles = NumberStyles.Integer Or NumberStyles.AllowDecimalPoint
CallTryParse(numericString, styles)
numericString = "41792210E-01"
styles = NumberStyles.Integer Or NumberStyles.AllowExponent
CallTryParse(numericString, styles)
numericString = "9112E-01"
CallTryParse(numericString, styles)
numericString = "312E01"
CallTryParse(numericString, styles)
numericString = "FFC86DA1"
CallTryParse(numericString, NumberStyles.HexNumber)
numericString = "0x8F8C"
CallTryParse(numericString, NumberStyles.HexNumber)
End Sub
Private Sub CallTryParse(stringToConvert As String, styles AS NumberStyles)
Dim number As ULong
Dim result As Boolean = UInt64.TryParse(stringToConvert, styles, _
CultureInfo.InvariantCulture, 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 '2106034' to 2106034.
' Attempted conversion of '-10603' failed.
' Converted '29103674.00' to 29103674.
' Attempted conversion of '10345.72' failed.
' Converted '41792210E-01' to 4179221.
' Attempted conversion of '9112E-01' failed.
' Converted '312E01' to 3120.
' Converted 'FFC86DA1' to 4291325345.
' Attempted conversion of '0x8F8C' failed.
注釈
メソッドは TryParse(String, NumberStyles, IFormatProvider, UInt64) メソッドに Parse(String, NumberStyles, IFormatProvider) 似ていますが、変換が失敗しても例外がスローされないことを除きます。 このメソッドを使用すると、 が無効であり、正常に解析できないかどうかをs
テストFormatExceptionするために例外処理を使用する必要がなくなります。
パラメーターは style
、解析操作を成功させるために パラメーターで s
許可されるスタイル要素 (空白や正または負の符号など) を定義します。 列挙体のビット フラグ NumberStyles の組み合わせである必要があります。 の style
値に応じて、 パラメーターに s
次の要素を含めることができます。
[ws][$][sign][digits,]digits[.fractional_digits][E[sign]exponential_digits][ws]
角かっこ ([ と ]) の項目は省略可能です。 または、 パラメーターに が style
含まれている NumberStyles.AllowHexSpecifier場合、 パラメーターには s
次の要素を含めることができます。
[ws]hexdigits[ws]
次の表は、それぞれの要素の説明です。
要素 | 説明 |
---|---|
ws | オプションの空白。 空白は、 が フラグを含むNumberStyles.AllowLeadingWhite場合style は のs 先頭に、フラグが含まれている場合style は のs 末尾にNumberStyles.AllowTrailingWhite表示されます。 |
$ | カルチャ固有の通貨記号。 文字列内での位置は、 パラメーターの CurrencyPositivePatternNumberFormatInfo メソッドによって返されるオブジェクトの provider プロパティによってGetFormat定義されます。 に フラグが含まれている場合style は、通貨記号を にs NumberStyles.AllowCurrencySymbol表示できます。 |
sign | 省略可能な記号。 署名は、 フラグが含まれている場合は のs 先頭に表示され、フラグが含NumberStyles.AllowLeadingSignまれている場合style は のs 末尾にNumberStyles.AllowTrailingSign表示style されます。 に フラグが含まれている場合style は、かっこを使用s して負の値をNumberStyles.AllowParentheses示すことができます。 ただし、負の符号が存在する場合、 s は解析操作が成功するための値 0 のみを表すことができます。 |
数値 | 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表すことができます。 |
exponential_digits | 0 から 9 までの数字のシーケンス。 フラグが含まれている場合style 、パラメーターはs 指数表記で数値をNumberStyles.AllowExponent表すことができます。 |
hexdigits | 0 から f、または 0 から F までの 16 進数のシーケンス。 |
注意
の終端の NUL (U+0000) 文字 s
は、引数の style
値に関係なく、解析操作では無視されます。
10 進数のみの文字列 (フラグに NumberStyles.None 対応) は常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、この入力文字列に存在する可能性がありますが、存在する必要がない要素を制御します。 次の表は、 に存在する可能性がある要素に対する個々 NumberStyles のメンバーの s
影響を示しています。
非複合 NumberStyles 値 |
数字に加えて値で許可される要素 |
---|---|
None | 10 進数のみ。 |
AllowDecimalPoint | 小数点 (.) 要素と fractional_digits 要素。 ただし、 fractional_digits は 1 つ以上の 0 桁のみで構成する必要があります。または、 メソッドは を返します false 。 |
AllowExponent | 指数表記と exponential_digitsを示す "e" または "E" 文字。 指数表記で数値を表す場合 s は、0 以外の小数部を含めることはできません。 |
AllowLeadingWhite | の先頭s にある ws 要素。 |
AllowTrailingWhite | の末尾s にある ws 要素。 |
AllowLeadingSign | 数字の前の sign 要素。 |
AllowTrailingSign | 数字の後の sign 要素。 |
AllowParentheses | 0 の数値を囲むかっこの形式の sign 要素。 |
AllowThousands | グループ区切り記号 (,) 要素。 |
AllowCurrencySymbol | currency ($) 要素。 |
Currency | すべての要素。 ただし、 s 16 進数または数値を指数表記で表すことはできません。 |
Float | の先頭または末尾の s ws 要素、の先頭s の符号、および小数点 (.) 記号。 パラメーターでは s 、指数表記を使用することもできます。 |
Number | ws、sign、group separator (,)、および decimal point (.) 要素。 |
Any | すべての要素。 ただし、 s 16 進数を表すことはできません。 |
フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 s
16 進値にする必要があります。 有効な 16 進文字は、0 から 9、A から F、および a から f です。 "0x" などのプレフィックスはサポートされていないため、解析操作が失敗します。 に存在できるその他の style
フラグは NumberStyles.AllowLeadingWhite と NumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStyles 、 HexNumber両方の空白フラグを含む複合スタイル があります)。
注意
が 16 進数の文字列表現である場合s
、その前に 16 進数として区別する装飾 (や &h
など0x
) を付けることはできません。 これにより、変換が失敗します。
パラメーターは provider
実装です IFormatProvider 。 そのメソッドは GetFormat 、 の形式に NumberFormatInfo 関するカルチャ固有の s
情報を提供する オブジェクトを返します。 パラメーターには provider
、次のいずれかを指定できます。
CultureInfo書式設定情報を提供するカルチャを表す オブジェクト。 そのメソッドは GetFormat 、そのカルチャの NumberFormatInfo 数値書式情報を提供する オブジェクトを返します。
NumberFormatInfo数値書式情報を提供する オブジェクト。 (の実装 GetFormat はそれ自体を返すだけです)。
を実装 IFormatProviderするカスタム オブジェクト。 そのメソッドは GetFormat 、書式設定情報を提供する オブジェクトを NumberFormatInfo インスタンス化して返します。
が のnull
場合provider
、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。
こちらもご覧ください
適用対象
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示