Int64.TryParse メソッド

定義

数値の文字列形式を、それと等価な 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

オーバーロード

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int64)

UTF-8 文字のスパンを値に解析しようとします。

TryParse(ReadOnlySpan<Char>, Int64)

数値のスパン表現を、等価の 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

TryParse(String, Int64)

数値の文字列形式を、それと等価な 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int64)

文字のスパンを値に解析しようとします。

TryParse(String, IFormatProvider, Int64)

文字列を値に解析しようとします。

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int64)

UTF-8 文字のスパンを値に解析しようとします。

TryParse(ReadOnlySpan<Byte>, Int64)

数値の文字列表現を含む UTF-8 文字スパンを、等価の 64 ビット符号付き整数に変換しようとします。

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int64)

指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

TryParse(String, NumberStyles, IFormatProvider, Int64)

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

TryParse(ReadOnlySpan<Byte>, IFormatProvider, Int64)

Source:
Int64.cs
Source:
Int64.cs

UTF-8 文字のスパンを値に解析しようとします。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result) = IUtf8SpanParsable<long>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, IFormatProvider? provider, out long result);
static member TryParse : ReadOnlySpan<byte> * IFormatProvider * int64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider, ByRef result As Long) As Boolean

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

provider
IFormatProvider

utf8Text に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Int64

戻り値には、正常に解析 utf8Text された結果または失敗した場合の未定義の値が含まれます。

戻り値

true 正常に解析された場合 utf8Text は 。それ以外の場合 falseは 。

適用対象

TryParse(ReadOnlySpan<Char>, Int64)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

数値のスパン表現を、等価の 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

public:
 static bool TryParse(ReadOnlySpan<char> s, [Runtime::InteropServices::Out] long % result);
public static bool TryParse (ReadOnlySpan<char> s, out long result);
static member TryParse : ReadOnlySpan<char> * int64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), ByRef result As Long) As Boolean

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を格納しているスパン。

result
Int64

変換が成功した場合、このメソッドが返されるときに、s に格納された数値と等価の 64 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null または Emptyの形式ではない場合、または Int64.MinValue より小さい数値または Int64.MaxValue より大きい数値を表す場合s、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

s が正常に変換された場合は true。それ以外の場合は false

適用対象

TryParse(String, Int64)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

数値の文字列形式を、それと等価な 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

public:
 static bool TryParse(System::String ^ s, [Runtime::InteropServices::Out] long % result);
public static bool TryParse (string s, out long result);
public static bool TryParse (string? s, out long result);
static member TryParse : string * int64 -> bool
Public Shared Function TryParse (s As String, ByRef result As Long) As Boolean

パラメーター

s
String

変換する数値を含む文字列。

result
Int64

変換が成功した場合、このメソッドが返されるときに、s に格納された数値と等価の 64 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null または Emptyの形式ではない場合、または Int64.MinValue より小さい数値または Int64.MaxValue より大きい数値を表す場合s、変換は失敗します。 このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

s が正常に変換された場合は true。それ以外の場合は false

次の例では、 Int64.TryParse(String, Int64) さまざまな文字列値を使用して メソッドを呼び出します。

using System;

public class StringParsing
{
   public static void Main()
   {
      TryToParse(null);
      TryToParse("160519");
      TryToParse("9432.0");
      TryToParse("16,667");
      TryToParse("   -322   ");
      TryToParse("+4302");
      TryToParse("(100);");
      TryToParse("01FA");
   }

   private static void TryToParse(string value)
   {
      bool success = Int64.TryParse(value, out long number);
      if (success)
      {
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      else
      {
         if (value == null) value = "";
         Console.WriteLine("Attempted conversion of '{0}' failed.", value);
      }
   }
}
// The example displays the following output to the console:
//       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.
open System

let tryToParse (value: string) =
    match Int64.TryParse value with
    | true, number ->
        printfn $"Converted '{value}' to {number}."
    | _ ->
        let value =
            if isNull value then 
                ""
            else
                value
        printfn $"Attempted conversion of '{value}' failed."

tryToParse null
tryToParse "160519"
tryToParse "9432.0"
tryToParse "16,667"
tryToParse "   -322   "
tryToParse "+4302"
tryToParse "(100);"
tryToParse "01FA"


// The example displays the following output to the console:
//       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.
Module StringParsing
   Public Sub Main()
      TryToParse(Nothing)
      TryToParse("160519")
      TryToParse("9432.0")
      TryToParse("16,667")
      TryToParse("   -322   ")
      TryToParse("+4302")
      TryToParse("(100)")
      TryToParse("01FA")
   End Sub
   
   Private Sub TryToParse(value As String)
      Dim number As Long
      Dim result As Boolean = Int64.TryParse(value, number)
      If result Then
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Else
         If value Is Nothing Then value = "" 
         Console.WriteLine("Attempted conversion of '{0}' failed.", value)
      End If     
   End Sub
End Module
' The example displays the following output to the console:
'       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.

この例では、メソッドが TryParse(String, Int64) 変換できない文字列の一部を次に示します。

  • "9432.0". 文字列に小数点区切り記号を含めることができないため、変換は失敗します。整数の数字のみを含む必要があります。

  • "16,667". 文字列にグループ区切り記号を含めることができないため、変換は失敗します。整数の数字のみを含む必要があります。

  • "(100)". 文字列に、現在のカルチャの および NumberFormatInfo.NumberNegativePattern プロパティで定義されている負の符号以外の符号を含めることができないため、変換はNumberFormatInfo.NegativeSign失敗します。

  • "01FA" 文字列に 16 進数を含めることができないため、変換は失敗します。10 進数のみを含む必要があります。

注釈

メソッドは TryParse メソッドに Parse 似ていますが、 TryParse メソッドは変換に失敗した場合に例外をスローしません。 無効であり、正常に解析できないイベントsで 例外処理を使用して をテストFormatExceptionする必要がなくなります。

パラメーターには s 、次の形式の数値が含まれています。

[ws][sign]digits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws 省略可能な空白。
sign 省略可能な記号。
数値 0 から 9 までの数字のシーケンス。

パラメーターは s 、 スタイルを NumberStyles.Integer 使用して解釈されます。 10 進数に加えて、先頭と末尾のスペースと先頭の記号のみを使用できます。 に存在できるカルチャ固有の書式設定情報と共にスタイル要素を明示的に s定義するには、 メソッドを TryParse(String, NumberStyles, IFormatProvider, Int64) 使用します。

パラメーターは s 、現在のシステム カルチャ用に初期化された オブジェクトの NumberFormatInfo 書式設定情報を使用して解析されます。 詳細については、「CurrentInfo」を参照してください。

メソッドのこのオーバーロードは、 TryParse パラメーター内のすべての数字を s 10 進数として解釈します。 16 進数の文字列表現を解析するには、 オーバーロードを TryParse(String, NumberStyles, IFormatProvider, Int64) 呼び出します。

こちらもご覧ください

適用対象

TryParse(ReadOnlySpan<Char>, IFormatProvider, Int64)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

文字のスパンを値に解析しようとします。

public:
 static bool TryParse(ReadOnlySpan<char> s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result) = ISpanParsable<long>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, IFormatProvider? provider, out long result);
static member TryParse : ReadOnlySpan<char> * IFormatProvider * int64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), provider As IFormatProvider, ByRef result As Long) As Boolean

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Int64

このメソッドから制御が戻るときに、 には、正常に解析 sされた結果、または失敗した場合は未定義の値が格納されます。

戻り値

true 正常に解析された場合 s は 。それ以外の場合 falseは 。

適用対象

TryParse(String, IFormatProvider, Int64)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

文字列を値に解析しようとします。

public:
 static bool TryParse(System::String ^ s, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result) = IParsable<long>::TryParse;
public static bool TryParse (string? s, IFormatProvider? provider, out long result);
static member TryParse : string * IFormatProvider * int64 -> bool
Public Shared Function TryParse (s As String, provider As IFormatProvider, ByRef result As Long) As Boolean

パラメーター

s
String

解析する文字列。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Int64

このメソッドから制御が戻るときに、正常に解析 s された結果または失敗した場合は未定義の値が格納されます。

戻り値

true 正常に解析された場合 s は 。それ以外の場合 falseは 。

適用対象

TryParse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider, Int64)

Source:
Int64.cs
Source:
Int64.cs

UTF-8 文字のスパンを値に解析しようとします。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result) = System::Numerics::INumberBase<long>::TryParse;
public static bool TryParse (ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style, IFormatProvider? provider, out long result);
static member TryParse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider * int64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), style As NumberStyles, provider As IFormatProvider, ByRef result As Long) As Boolean

パラメーター

utf8Text
ReadOnlySpan<Byte>

解析する UTF-8 文字のスパン。

style
NumberStyles

utf8Text存在できる数値スタイルのビットごとの組み合わせ。

provider
IFormatProvider

utf8Text に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Int64

戻り値には、正常に解析 utf8Text された結果または失敗した場合の未定義の値が含まれます。

戻り値

true 正常に解析された場合 utf8Text は 。それ以外の場合 falseは 。

適用対象

TryParse(ReadOnlySpan<Byte>, Int64)

Source:
Int64.cs
Source:
Int64.cs

数値の文字列表現を含む UTF-8 文字スパンを、等価の 64 ビット符号付き整数に変換しようとします。

public:
 static bool TryParse(ReadOnlySpan<System::Byte> utf8Text, [Runtime::InteropServices::Out] long % result);
public static bool TryParse (ReadOnlySpan<byte> utf8Text, out long result);
static member TryParse : ReadOnlySpan<byte> * int64 -> bool
Public Shared Function TryParse (utf8Text As ReadOnlySpan(Of Byte), ByRef result As Long) As Boolean

パラメーター

utf8Text
ReadOnlySpan<Byte>

変換する数値を表す UTF-8 文字を含むスパン。

result
Int64

このメソッドが戻るときに、 には、変換が成功した場合は に utf8Text 含まれる数値と等価の 64 ビット符号付き整数値が格納され、変換に失敗した場合は 0 が格納されます。 このパラメーターは、初期化されていない状態で渡されます。result にもともと入っていた値は上書きされます。

戻り値

utf8Text が正常に変換された場合は true。それ以外の場合は false

適用対象

TryParse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider, Int64)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

指定したスタイルおよびカルチャに固有の書式による数値のスパン表現を、等価の 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result);
public:
 static bool TryParse(ReadOnlySpan<char> s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result) = System::Numerics::INumberBase<long>::TryParse;
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider? provider, out long result);
public static bool TryParse (ReadOnlySpan<char> s, System.Globalization.NumberStyles style, IFormatProvider provider, out long result);
static member TryParse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider * int64 -> bool
Public Shared Function TryParse (s As ReadOnlySpan(Of Char), style As NumberStyles, provider As IFormatProvider, ByRef result As Long) As Boolean

パラメーター

s
ReadOnlySpan<Char>

変換する数値を表す文字を格納しているスパン。 このスパンは、style で指定されたスタイルを使用して解釈されます。

style
NumberStyles

s で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Int64

変換が成功した場合、このメソッドが返されるときに、s に格納された数値と等価の 64 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null または の形式styleでない場合、または Int64.MinValue より小さい数値または Int64.MaxValue より大きい数値を表す場合s、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

s が正常に変換された場合は true。それ以外の場合は false

適用対象

TryParse(String, NumberStyles, IFormatProvider, Int64)

Source:
Int64.cs
Source:
Int64.cs
Source:
Int64.cs

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 64 ビット符号付き整数に変換します。 戻り値は変換が成功したか失敗したかを示します。

public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result);
public:
 static bool TryParse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider, [Runtime::InteropServices::Out] long % result) = System::Numerics::INumberBase<long>::TryParse;
public static bool TryParse (string s, System.Globalization.NumberStyles style, IFormatProvider provider, out long result);
public static bool TryParse (string? s, System.Globalization.NumberStyles style, IFormatProvider? provider, out long result);
static member TryParse : string * System.Globalization.NumberStyles * IFormatProvider * int64 -> bool
Public Shared Function TryParse (s As String, style As NumberStyles, provider As IFormatProvider, ByRef result As Long) As Boolean

パラメーター

s
String

変換する数値を含む文字列。 文字列は、style で指定されたスタイルを使用して解釈されます。

style
NumberStyles

s で存在する可能性を持つスタイル要素を示す、列挙値のビットごとの組み合わせ。 通常指定する値は、Integer です。

provider
IFormatProvider

s に関するカルチャ固有の書式情報を提供するオブジェクト。

result
Int64

変換が成功した場合、このメソッドが返されるときに、s に格納された数値と等価の 64 ビット符号付き整数値を格納します。変換に失敗した場合は 0 を格納します。 パラメーターが null または の形式styleでない場合、または Int64.MinValue より小さい数値または Int64.MaxValue より大きい数値を表す場合s、変換は失敗します。Empty このパラメーターは初期化されていない状態で渡されています。result で最初に指定された任意の値が上書きされます。

戻り値

s が正常に変換された場合は true。それ以外の場合は false

例外

styleNumberStyles 値ではありません。

- または -

styleAllowHexSpecifier 値と HexNumber 値の組み合わせではありません。

次の例では、 TryParse(String, NumberStyles, IFormatProvider, Int64) さまざまな文字列と値を使用して メソッドを NumberStyles 呼び出します。

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 = Int64.TryParse(stringToConvert, styles,
                                   provider, out long 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 Int64.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 Long
      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 = Int64.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]exponential_digits][ws]

または、 パラメーターに が style 含まれている場合は NumberStyles.AllowHexSpecifier

[ws]hexdigits[ws]

角かっこ ([ および ]) 内の要素は省略可能です。 次の表は、それぞれの要素の説明です。

要素 説明
ws オプションの空白。 空白は、 が フラグを含む場合は のs先頭に、フラグが含NumberStyles.AllowLeadingWhiteまれている場合styleは のs末尾にNumberStyles.AllowTrailingWhite表示styleされます。
$ カルチャ固有の通貨記号。 文字列内での位置は、 パラメーターの CurrencyPositivePatternNumberFormatInfo メソッドによって返されるオブジェクトの provider プロパティによってGetFormat定義されます。 に フラグが含まれている場合styleは、通貨記号を にsNumberStyles.AllowCurrencySymbol表示できます。
sign 省略可能な記号。 に または NumberStyles.AllowTrailingSign フラグが含まれている場合styleは、s記号記号をNumberStyles.AllowLeadingSign表示できます。
数値

fractional_digits

exponential_digits
0 から 9 までの数字のシーケンス。 fractional_digitsの場合、数字 0 のみが有効です。
, カルチャ固有の桁区切り記号。 でprovider指定されたカルチャの桁区切り記号は、 に フラグが含まれている場合stylesNumberStyles.AllowThousands表示できます。
. カルチャ固有の小数点記号。 でprovider指定されたカルチャの小数点記号は、 に フラグが含まれている場合stylesNumberStyles.AllowDecimalPoint表示できます。
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 以外の小数部を持たないデータ型の Int64 範囲内の整数を表す必要があります。
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 の先頭または末尾の sws 要素、の先頭s符号、および小数点 ( . ) 記号。 パラメーターでは s 、指数表記を使用することもできます。
NumberStyles.Number wssign、thousands separator (,)、および decimal point (.) 要素。
NumberStyles.Any を除く s すべてのスタイルは、16 進数を表すことはできません。

フラグを使用する NumberStyles.AllowHexSpecifier 場合は、 s プレフィックスのない 16 進値を指定する必要があります。 たとえば、"C9AF3" は正常に解析されますが、"0xC9AF3" では解析されません。 に存在できるその他の style フラグは NumberStyles.AllowLeadingWhiteNumberStyles.AllowTrailingWhiteのみです。 (列挙型には NumberStylesNumberStyles.HexNumber両方の空白フラグを含む複合スタイル があります)。

パラメーターはproviderIFormatProvider、オブジェクトや オブジェクトなどのCultureInfo実装でありNumberFormatInfo、そのメソッドは GetFormat オブジェクトをNumberFormatInfo返します。 オブジェクトは NumberFormatInfo 、 の形式に関するカルチャ固有の s情報を提供します。 が nullの場合providerは、現在のNumberFormatInfoカルチャの オブジェクトが使用されます。

こちらもご覧ください

適用対象