次の方法で共有


Int32.Parse メソッド

定義

数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

オーバーロード

名前 説明
Parse(String)

数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

Parse(ReadOnlySpan<Byte>, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, IFormatProvider)

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

Parse(String, NumberStyles)

指定したスタイルの数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

Parse(String, IFormatProvider)

指定したカルチャ固有の形式の数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

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

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。

Parse(String, NumberStyles, IFormatProvider)

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

Parse(String)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

public:
 static int Parse(System::String ^ s);
public static int Parse(string s);
static member Parse : string -> int
Public Shared Function Parse (s As String) As Integer

パラメーター

s
String

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

返品

sに含まれる数値に相当する 32 ビット符号付き整数。

例外

snullです。

s が正しい形式ではありません。

s は、 Int32.MinValue より小さいか、 Int32.MaxValue より大きい数値を表します。

次の例では、 Int32.Parse(String) メソッドを使用して文字列値を 32 ビット符号付き整数値に変換する方法を示します。 結果の整数値がコンソールに表示されます。

using System;

public class Example
{
   public static void Main()
   {
      string[] values = { "+13230", "-0", "1,390,146", "$190,235,421,127",
                          "0xFA1B", "163042", "-10", "007", "2147483647",
                          "2147483648", "16e07", "134985.0", "-12034",
                          "-2147483648", "-2147483649" };
      foreach (string value in values)
      {
         try {
            int number = Int32.Parse(value);
            Console.WriteLine("{0} --> {1}", value, number);
         }
         catch (FormatException) {
            Console.WriteLine("{0}: Bad Format", value);
         }
         catch (OverflowException) {
            Console.WriteLine("{0}: Overflow", value);
         }
      }
   }
}
// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10 --> -10
//       007 --> 7
//       2147483647 --> 2147483647
//       2147483648: Overflow
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034 --> -12034
//       -2147483648 --> -2147483648
//       -2147483649: Overflow
open System

let values =
    [ "+13230"; "-0"; "1,390,146"; "$190,235,421,127"
      "0xFA1B"; "163042"; "-10"; "007"; "2147483647"
      "2147483648"; "16e07"; "134985.0"; "-12034"
      "-2147483648"; "-2147483649" ]

for value in values do
    try
        let number = Int32.Parse value
        printfn $"{value} --> {number}"
    with 
    | :? FormatException ->
        printfn $"{value}: Bad Format"
    | :? OverflowException ->
        printfn $"{value}: Overflow"
    

// The example displays the following output:
//       +13230 --> 13230
//       -0 --> 0
//       1,390,146: Bad Format
//       $190,235,421,127: Bad Format
//       0xFA1B: Bad Format
//       163042 --> 163042
//       -10 --> -10
//       007 --> 7
//       2147483647 --> 2147483647
//       2147483648: Overflow
//       16e07: Bad Format
//       134985.0: Bad Format
//       -12034 --> -12034
//       -2147483648 --> -2147483648
//       -2147483649: Overflow
Module Example
   Public Sub Main()
      Dim values() As String = { "+13230", "-0", "1,390,146", "$190,235,421,127",
                                 "0xFA1B", "163042", "-10", "007", "2147483647", 
                                 "2147483648", "16e07", "134985.0", "-12034",
                                 "-2147483648", "-2147483649"  }
      For Each value As String In values
         Try
            Dim number As Integer = Int32.Parse(value) 
            Console.WriteLine("{0} --> {1}", value, number)
         Catch e As FormatException
            Console.WriteLine("{0}: Bad Format", value)
         Catch e As OverflowException
            Console.WriteLine("{0}: Overflow", value)   
         End Try  
      Next
   End Sub
End Module
' The example displays the following output:
'       +13230 --> 13230
'       -0 --> 0
'       1,390,146: Bad Format
'       $190,235,421,127: Bad Format
'       0xFA1B: Bad Format
'       163042 --> 163042
'       -10 --> -10
'       007 --> 7
'       2147483647 --> 2147483647
'       2147483648: Overflow
'       16e07: Bad Format
'       134985.0: Bad Format
'       -12034 --> -12034
'       -2147483648 --> -2147483648
'       -2147483649: Overflow

注釈

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

[ws][sign]digits[ws]

角かっこ ([ と ]) の項目は省略可能です。 次の表では、各要素について説明します。

要素 形容
ws 省略可能な空白。
看板 省略可能な記号。
0 から 9 までの数字のシーケンス。

s パラメーターは、NumberStyles.Integer スタイルを使用して解釈されます。 10 進数に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 sに存在できるスタイル要素を明示的に定義するには、Int32.Parse(String, NumberStyles)メソッドまたはInt32.Parse(String, NumberStyles, IFormatProvider) メソッドを使用します。

s パラメーターは、現在のシステム カルチャ用に初期化されたNumberFormatInfo オブジェクトの書式設定情報を使用して解析されます。 詳細については、CurrentInfoを参照してください。 他のカルチャの書式設定情報を使用して文字列を解析するには、 Int32.Parse(String, NumberStyles, IFormatProvider) メソッドを使用します。

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, IFormatProvider)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

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

public:
 static int Parse(ReadOnlySpan<System::Byte> utf8Text, IFormatProvider ^ provider) = IUtf8SpanParsable<int>::Parse;
public static int Parse(ReadOnlySpan<byte> utf8Text, IFormatProvider? provider);
static member Parse : ReadOnlySpan<byte> * IFormatProvider -> int
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), provider As IFormatProvider) As Integer

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

provider
IFormatProvider

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

返品

utf8Text解析の結果。

実装

適用対象

Parse(ReadOnlySpan<Char>, IFormatProvider)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

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

public:
 static int Parse(ReadOnlySpan<char> s, IFormatProvider ^ provider) = ISpanParsable<int>::Parse;
public static int Parse(ReadOnlySpan<char> s, IFormatProvider? provider);
static member Parse : ReadOnlySpan<char> * IFormatProvider -> int
Public Shared Function Parse (s As ReadOnlySpan(Of Char), provider As IFormatProvider) As Integer

パラメーター

s
ReadOnlySpan<Char>

解析する文字のスパン。

provider
IFormatProvider

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

返品

s解析の結果。

実装

適用対象

Parse(String, NumberStyles)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

指定したスタイルの数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

public:
 static int Parse(System::String ^ s, System::Globalization::NumberStyles style);
public static int Parse(string s, System.Globalization.NumberStyles style);
static member Parse : string * System.Globalization.NumberStyles -> int
Public Shared Function Parse (s As String, style As NumberStyles) As Integer

パラメーター

s
String

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

style
NumberStyles

sに存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は Integerです。

返品

sで指定された数値に相当する 32 ビット符号付き整数。

例外

snullです。

styleNumberStyles 値ではありません。

-又は-

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

s は、 styleに準拠した形式ではありません。

s は、 Int32.MinValue より小さいか、 Int32.MaxValue より大きい数値を表します。

-又は-

s には、0 以外の小数部の数字が含まれます。

次の例では、 Int32.Parse(String, NumberStyles) メソッドを使用して、複数の Int32 値の文字列形式を解析します。 この例の現在のカルチャは en-USです。

using System;
using System.Globalization;

public class ParseInt32
{
   public static void Main()
   {
      Convert("104.0", NumberStyles.AllowDecimalPoint);
      Convert("104.9", NumberStyles.AllowDecimalPoint);
      Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol |
                                 NumberStyles.Number);
      Convert("103E06", NumberStyles.AllowExponent);
      Convert("-1,345,791", NumberStyles.AllowThousands);
      Convert("(1,345,791)", NumberStyles.AllowThousands |
                             NumberStyles.AllowParentheses);
   }

   private static void Convert(string value, NumberStyles style)
   {
      try
      {
         int number = Int32.Parse(value, style);
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}'.", value);
      }
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is out of range of the Int32 type.", value);
      }
   }
}
// The example displays the following output to the console:
//       Converted '104.0' to 104.
//       '104.9' is out of range of the Int32 type.
//       ' $17,198,064.42' is out of range of the Int32 type.
//       Converted '103E06' to 103000000.
//       Unable to convert '-1,345,791'.
//       Converted '(1,345,791)' to -1345791.
open System
open System.Globalization

let convert value (style: NumberStyles) =
    try
        let number = Int32.Parse(value, style)
        printfn $"Converted '{value}' to {number}."
    with 
    | :? FormatException ->
        printfn $"Unable to convert '{value}'."
    | :? OverflowException ->
        printfn $"'{value}' is out of range of the Int32 type."

convert "104.0" NumberStyles.AllowDecimalPoint
convert "104.9" NumberStyles.AllowDecimalPoint
convert " $17,198,064.42" (NumberStyles.AllowCurrencySymbol ||| NumberStyles.Number)
convert "103E06" NumberStyles.AllowExponent
convert "-1,345,791" NumberStyles.AllowThousands
convert "(1,345,791)" (NumberStyles.AllowThousands ||| NumberStyles.AllowParentheses)


// The example displays the following output to the console:
//       Converted '104.0' to 104.
//       '104.9' is out of range of the Int32 type.
//       ' $17,198,064.42' is out of range of the Int32 type.
//       Converted '103E06' to 103000000.
//       Unable to convert '-1,345,791'.
//       Converted '(1,345,791)' to -1345791.
Imports System.Globalization

Module ParseInt32
   Public Sub Main()
      Convert("104.0", NumberStyles.AllowDecimalPoint)    
      Convert("104.9", NumberStyles.AllowDecimalPoint)
      Convert(" $17,198,064.42", NumberStyles.AllowCurrencySymbol Or _
                                 NumberStyles.Number)
      Convert("103E06", NumberStyles.AllowExponent)  
      Convert("-1,345,791", NumberStyles.AllowThousands)
      Convert("(1,345,791)", NumberStyles.AllowThousands Or _
                             NumberStyles.AllowParentheses)
   End Sub
   
   Private Sub Convert(value As String, style As NumberStyles)
      Try
         Dim number As Integer = Int32.Parse(value, style)
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", value)
      Catch e As OverflowException
         Console.WriteLine("'{0}' is out of range of the Int32 type.", value)   
      End Try
   End Sub
End Module
' The example displays the following output to the console:
'       Converted '104.0' to 104.
'       '104.9' is out of range of the Int32 type.
'       ' $17,198,064.42' is out of range of the Int32 type.
'       Converted '103E06' to 103000000.
'       Unable to convert '-1,345,791'.
'       Converted '(1,345,791)' to -1345791.

注釈

style パラメーターは、解析操作を成功させるために s パラメーターで許可されるスタイル要素 (空白、正符号または負符号記号、桁区切り記号など) を定義します。 NumberStyles列挙型のビット フラグの組み合わせである必要があります。 styleの値によっては、s パラメーターに次の要素が含まれる場合があります。

[ws][$][sign][digits,]digits[.fractional_digits][e[sign]exponential_digits][ws]

または、AllowHexSpecifierstyle含まれている場合:

[ws]hexdigits[ws]

角かっこ ([ と ]) の項目は省略可能です。 次の表では、各要素について説明します。

要素 形容
ws 省略可能な空白。 styleNumberStyles.AllowLeadingWhite フラグが含まれている場合は、sの先頭に空白が表示され、styleNumberStyles.AllowTrailingWhite フラグが含まれている場合は、sの末尾に空白を表示できます。
$ カルチャ固有の通貨記号。 文字列内での位置は、現在のカルチャの NumberFormatInfo.CurrencyNegativePattern プロパティと NumberFormatInfo.CurrencyPositivePattern プロパティによって定義されます。 現在のカルチャの通貨記号は、s フラグstyle含まれている場合、NumberStyles.AllowCurrencySymbolに表示されます。
サイン 省略可能な記号。 styleNumberStyles.AllowLeadingSign フラグが含まれている場合はsの先頭に表示され、styleNumberStyles.AllowTrailingSign フラグが含まれている場合はsの末尾に表示されます。 sでかっこを使用して、styleNumberStyles.AllowParentheses フラグが含まれている場合は負の値を示すことができます。


fractional_digits

exponential_digits
0 から 9 までの数字のシーケンス。 fractional_digitsの場合は、数字 0 のみが有効です。
, カルチャ固有の桁区切り記号。 styleNumberStyles.AllowThousands フラグが含まれている場合は、現在のカルチャの桁区切り記号をsに表示できます。
. カルチャ固有の小数点記号。 現在のカルチャの小数点記号は、s フラグstyle含まれている場合、NumberStyles.AllowDecimalPointに表示できます。 解析操作が成功するには、数字 0 のみを小数桁として表示できます。 fractional_digits他の 数字が含まれている場合は、 OverflowException がスローされます。
e 値が指数表記で表されることを示す 'e' または 'E' 文字。 s パラメーターは、style フラグが含まれている場合NumberStyles.AllowExponent指数表記で数値を表すことができます。
hexdigits 0 から f、または 0 から F までの 16 進数のシーケンス。

手記

sで終了する NUL (U+0000) 文字は、style引数の値に関係なく、解析操作では無視されます。

数字のみを含む文字列 ( NumberStyles.None スタイルに対応) は、 Int32 型の範囲内にある場合、常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、入力文字列に存在する必要がない要素を制御します。 次の表は、個々の NumberStyles メンバーが、 sに存在する可能性がある要素に与える影響を示しています。

NumberStyles 値 数字に加えて s で許可される要素
None digits 要素のみ。
AllowDecimalPoint 小数点 ( . ) および 小数部の要素
AllowExponent s パラメーターでは指数表記を使用することもできます。
AllowLeadingWhite sの先頭にある ws 要素。
AllowTrailingWhite の末尾にある s 要素。
AllowLeadingSign sの先頭にある sign 要素。
AllowTrailingSign の末尾にある s 要素。
AllowParentheses 数値を囲むかっこの形式の 符号 要素。
AllowThousands 桁区切り記号 ( , ) 要素。
AllowCurrencySymbol $要素。
Currency すべての。 s パラメーターは、指数表記で 16 進数または数値を表すことはできません。
Float sの先頭または末尾の ws 要素、sの先頭の符号、および小数点 ( . ) 記号。 s パラメーターでは指数表記を使用することもできます。
Number wssign、桁区切り記号 ()、および小数点 ( . ) 要素。
Any sを除くすべてのスタイルは、16 進数を表すことはできません。

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

s パラメーターは、現在のシステム カルチャ用に初期化されたNumberFormatInfo オブジェクトの書式設定情報を使用して解析されます。 解析操作で書式設定情報を使用するカルチャを指定するには、 Int32.Parse(String, NumberStyles, IFormatProvider) オーバーロードを呼び出します。

こちらもご覧ください

適用対象

Parse(String, IFormatProvider)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

指定したカルチャ固有の形式の数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

public:
 static int Parse(System::String ^ s, IFormatProvider ^ provider);
public:
 static int Parse(System::String ^ s, IFormatProvider ^ provider) = IParsable<int>::Parse;
public static int Parse(string s, IFormatProvider provider);
public static int Parse(string s, IFormatProvider? provider);
static member Parse : string * IFormatProvider -> int
Public Shared Function Parse (s As String, provider As IFormatProvider) As Integer

パラメーター

s
String

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

provider
IFormatProvider

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

返品

sで指定された数値に相当する 32 ビット符号付き整数。

実装

例外

snullです。

s が正しい形式ではありません。

s は、 Int32.MinValue より小さいか、 Int32.MaxValue より大きい数値を表します。

次の例は、Web フォームのボタン クリック イベント ハンドラーです。 HttpRequest.UserLanguages プロパティによって返される配列を使用して、ユーザーのロケールを決定します。 その後、そのロケールに対応する CultureInfo オブジェクトをインスタンス化します。 そのCultureInfo オブジェクトに属するNumberFormatInfo オブジェクトが Parse(String, IFormatProvider) メソッドに渡され、ユーザーの入力がInt32値に変換されます。

protected void OkToInteger_Click(object sender, EventArgs e)
{
    string locale;
    int number;
    CultureInfo culture;

    // Return if string is empty
    if (String.IsNullOrEmpty(this.inputNumber.Text))
        return;

    // Get locale of web request to determine possible format of number
    if (Request.UserLanguages.Length == 0)
        return;
    locale = Request.UserLanguages[0];
    if (String.IsNullOrEmpty(locale))
        return;

    // Instantiate CultureInfo object for the user's locale
    culture = new CultureInfo(locale);

    // Convert user input from a string to a number
    try
    {
        number = Int32.Parse(this.inputNumber.Text, culture.NumberFormat);
    }
    catch (FormatException)
    {
        return;
    }
    catch (Exception)
    {
        return;
    }
    // Output number to label on web form
    this.outputNumber.Text = "Number is " + number.ToString();
}
Protected Sub OkToInteger_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles OkToInteger.Click
    Dim locale As String
    Dim culture As CultureInfo
    Dim number As Integer

    ' Return if string is empty
    If String.IsNullOrEmpty(Me.inputNumber.Text) Then Exit Sub

    ' Get locale of web request to determine possible format of number
    If Request.UserLanguages.Length = 0 Then Exit Sub
    locale = Request.UserLanguages(0)
    If String.IsNullOrEmpty(locale) Then Exit Sub

    ' Instantiate CultureInfo object for the user's locale
    culture = New CultureInfo(locale)

    ' Convert user input from a string to a number
    Try
        number = Int32.Parse(Me.inputNumber.Text, culture.NumberFormat)
    Catch ex As FormatException
        Exit Sub
    Catch ex As Exception
        Exit Sub
    End Try

    ' Output number to label on web form
    Me.outputNumber.Text = "Number is " & number.ToString()
End Sub

注釈

Parse(String, IFormatProvider) メソッドのこのオーバーロードは、通常、さまざまな方法で書式設定できるテキストをInt32値に変換するために使用されます。 たとえば、ユーザーが入力したテキストを HTML テキスト ボックスに数値に変換するために使用できます。

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

[ws][sign]digits[ws]

角かっこ ([ と ]) の項目は省略可能です。 次の表では、各要素について説明します。

要素 形容
ws 省略可能な空白。
サイン 省略可能な記号。
0 から 9 までの数字のシーケンス。

s パラメーターは、NumberStyles.Integer スタイルを使用して解釈されます。 10 進数に加えて、先頭と末尾のスペースと先頭の符号のみを使用できます。 sに存在できるスタイル要素を明示的に定義するには、Int32.Parse(String, NumberStyles, IFormatProvider) メソッドを使用します。

provider パラメーターは、NumberFormatInfoCultureInfo オブジェクトなどのIFormatProvider実装です。 provider パラメーターは、sの形式に関するカルチャ固有の情報を提供します。 providernull場合は、現在のカルチャのNumberFormatInfo オブジェクトが使用されます。

こちらもご覧ください

適用対象

Parse(ReadOnlySpan<Byte>, NumberStyles, IFormatProvider)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

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

public static int Parse(ReadOnlySpan<byte> utf8Text, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
static member Parse : ReadOnlySpan<byte> * System.Globalization.NumberStyles * IFormatProvider -> int
Public Shared Function Parse (utf8Text As ReadOnlySpan(Of Byte), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Integer

パラメーター

utf8Text
ReadOnlySpan<Byte>

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

style
NumberStyles

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

provider
IFormatProvider

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

返品

utf8Text解析の結果。

実装

適用対象

Parse(ReadOnlySpan<Char>, NumberStyles, IFormatProvider)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

指定したスタイルおよびカルチャ固有の形式の数値のスパン表現を、等価の 32 ビット符号付き整数に変換します。

public static int Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider? provider = default);
public static int Parse(ReadOnlySpan<char> s, System.Globalization.NumberStyles style = System.Globalization.NumberStyles.Integer, IFormatProvider provider = default);
static member Parse : ReadOnlySpan<char> * System.Globalization.NumberStyles * IFormatProvider -> int
Public Shared Function Parse (s As ReadOnlySpan(Of Char), Optional style As NumberStyles = System.Globalization.NumberStyles.Integer, Optional provider As IFormatProvider = Nothing) As Integer

パラメーター

s
ReadOnlySpan<Char>

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

style
NumberStyles

sに存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は Integerです。

provider
IFormatProvider

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

返品

sで指定された数値に相当する 32 ビット符号付き整数。

実装

適用対象

Parse(String, NumberStyles, IFormatProvider)

ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs
ソース:
Int32.cs

指定したスタイルおよびカルチャ固有の形式の数値の文字列形式を、等価の 32 ビット符号付き整数に変換します。

public:
 static int Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider);
public:
 static int Parse(System::String ^ s, System::Globalization::NumberStyles style, IFormatProvider ^ provider) = System::Numerics::INumberBase<int>::Parse;
public static int Parse(string s, System.Globalization.NumberStyles style, IFormatProvider provider);
public static int Parse(string s, System.Globalization.NumberStyles style, IFormatProvider? provider);
static member Parse : string * System.Globalization.NumberStyles * IFormatProvider -> int
Public Shared Function Parse (s As String, style As NumberStyles, provider As IFormatProvider) As Integer

パラメーター

s
String

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

style
NumberStyles

sに存在できるスタイル要素を示す列挙値のビットごとの組み合わせ。 指定する一般的な値は Integerです。

provider
IFormatProvider

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

返品

sで指定された数値に相当する 32 ビット符号付き整数。

実装

例外

snullです。

styleNumberStyles 値ではありません。

-又は-

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

s は、 styleに準拠した形式ではありません。

s は、 Int32.MinValue より小さいか、 Int32.MaxValue より大きい数値を表します。

-又は-

s には、0 以外の小数部の数字が含まれます。

次の例では、さまざまな style パラメーターと provider パラメーターを使用して、 Int32 値の文字列表現を解析します。 また、書式設定情報が解析操作に使用されるカルチャに応じて、同じ文字列を解釈できるさまざまな方法についても説明します。

using System;
using System.Globalization;

public class ParseInt32
{
   public static void Main()
   {
      Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
              new CultureInfo("en-GB"));
      Convert("12,000", NumberStyles.Float | NumberStyles.AllowThousands,
              new CultureInfo("fr-FR"));
      Convert("12,000", NumberStyles.Float, new CultureInfo("en-US"));

      Convert("12 425,00", NumberStyles.Float | NumberStyles.AllowThousands,
              new CultureInfo("sv-SE"));
      Convert("12,425.00", NumberStyles.Float | NumberStyles.AllowThousands,
              NumberFormatInfo.InvariantInfo);
      Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
              new CultureInfo("fr-FR"));
      Convert("631,900", NumberStyles.Integer | NumberStyles.AllowDecimalPoint,
              new CultureInfo("en-US"));
      Convert("631,900", NumberStyles.Integer | NumberStyles.AllowThousands,
              new CultureInfo("en-US"));
   }

   private static void Convert(string value, NumberStyles style,
                               IFormatProvider provider)
   {
      try
      {
         int number = Int32.Parse(value, style, provider);
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}'.", value);
      }
      catch (OverflowException)
      {
         Console.WriteLine("'{0}' is out of range of the Int32 type.", value);
      }
   }
}
// This example displays the following output to the console:
//       Converted '12,000' to 12000.
//       Converted '12,000' to 12.
//       Unable to convert '12,000'.
//       Converted '12 425,00' to 12425.
//       Converted '12,425.00' to 12425.
//       '631,900' is out of range of the Int32 type.
//       Unable to convert '631,900'.
//       Converted '631,900' to 631900.
open System
open System.Globalization

let convert (value: string) (style: NumberStyles) (provider: IFormatProvider) =
    try
        let number = Int32.Parse(value, style, provider)
        printfn $"Converted '{value}' to {number}."
    with 
    | :? FormatException ->
        printfn $"Unable to convert '{value}'."
    | :? OverflowException ->
        printfn $"'{value}' is out of range of the Int32 type."

convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "en-GB")
convert "12,000" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "fr-FR")
convert "12,000" NumberStyles.Float (CultureInfo "en-US")
convert "12 425,00" (NumberStyles.Float ||| NumberStyles.AllowThousands) (CultureInfo "sv-SE")
convert "12,425.00" (NumberStyles.Float ||| NumberStyles.AllowThousands) NumberFormatInfo.InvariantInfo
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "fr-FR")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowDecimalPoint) (CultureInfo "en-US")
convert "631,900" (NumberStyles.Integer ||| NumberStyles.AllowThousands) (CultureInfo "en-US")

// This example displays the following output to the console:
//       Converted '12,000' to 12000.
//       Converted '12,000' to 12.
//       Unable to convert '12,000'.
//       Converted '12 425,00' to 12425.
//       Converted '12,425.00' to 12425.
//       '631,900' is out of range of the Int32 type.
//       Unable to convert '631,900'.
//       Converted '631,900' to 631900.
Imports System.Globalization

Module ParseInt32
   Public Sub Main()
      Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
              New CultureInfo("en-GB"))      
      Convert("12,000", NumberStyles.Float Or NumberStyles.AllowThousands, _
              New CultureInfo("fr-FR"))
      Convert("12,000", NumberStyles.Float, New CultureInfo("en-US"))
      
      Convert("12 425,00", NumberStyles.Float Or NumberStyles.AllowThousands, _
              New CultureInfo("sv-SE")) 
      Convert("12,425.00", NumberStyles.Float Or NumberStyles.AllowThousands, _
              NumberFormatInfo.InvariantInfo) 
      Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _ 
              New CultureInfo("fr-FR"))
      Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowDecimalPoint, _
              New CultureInfo("en-US"))
      Convert("631,900", NumberStyles.Integer Or NumberStyles.AllowThousands, _
              New CultureInfo("en-US"))
   End Sub

   Private Sub Convert(value As String, style As NumberStyles, _
                       provider As IFormatProvider)
      Try
         Dim number As Integer = Int32.Parse(value, style, provider)
         Console.WriteLine("Converted '{0}' to {1}.", value, number)
      Catch e As FormatException
         Console.WriteLine("Unable to convert '{0}'.", value)
      Catch e As OverflowException
         Console.WriteLine("'{0}' is out of range of the Int32 type.", value)   
      End Try
   End Sub                       
End Module
' This example displays the following output to the console:
'       Converted '12,000' to 12000.
'       Converted '12,000' to 12.
'       Unable to convert '12,000'.
'       Converted '12 425,00' to 12425.
'       Converted '12,425.00' to 12425.
'       '631,900' is out of range of the Int32 type.
'       Unable to convert '631,900'.
'       Converted '631,900' to 631900.

注釈

style パラメーターは、解析操作を成功させるために s パラメーターで許可されるスタイル要素 (空白や正符号など) を定義します。 NumberStyles列挙型のビット フラグの組み合わせである必要があります。 styleの値によっては、s パラメーターに次の要素が含まれる場合があります。

[ws][$][sign][digits,]digits[.fractional_digist][e[sign]exponential_digits][ws]

または、AllowHexSpecifierstyle含まれている場合:

[ws]hexdigits[ws]

角かっこ ([ と ]) の項目は省略可能です。 次の表では、各要素について説明します。

要素 形容
ws 省略可能な空白。 styleNumberStyles.AllowLeadingWhite フラグが含まれている場合は、sの先頭に空白が表示され、styleNumberStyles.AllowTrailingWhite フラグが含まれている場合は、sの末尾に空白を表示できます。
$ カルチャ固有の通貨記号。 文字列内での位置は、NumberFormatInfo.CurrencyPositivePattern パラメーターのNumberFormatInfo メソッドによって返されるGetFormat オブジェクトのprovider プロパティによって定義されます。 sstyleフラグが含まれている場合、通貨記号はNumberStyles.AllowCurrencySymbolに表示されます。
サイン 省略可能な記号。 styleNumberStyles.AllowLeadingSign フラグが含まれている場合はsの先頭に、NumberStyles.AllowTrailingSign フラグstyle含まれている場合はsの最後に表示されます。 sでかっこを使用して、styleNumberStyles.AllowParentheses フラグが含まれている場合は負の値を示すことができます。


fractional_digits

exponential_digits
0 から 9 までの数字のシーケンス。 fractional_digitsの場合は、数字 0 のみが有効です。
, カルチャ固有の桁区切り記号。 styleNumberStyles.AllowThousands フラグが含まれている場合、providerで指定されたカルチャの桁区切り記号をsに表示できます。
. カルチャ固有の小数点記号。 providers フラグが含まれている場合、styleで指定されたカルチャの小数点記号をNumberStyles.AllowDecimalPointに表示できます。

解析操作が成功するには、数字 0 のみを小数桁として表示できます。 fractional_digits他の 数字が含まれている場合は、 OverflowException がスローされます。
e 値が指数表記で表されることを示す 'e' または 'E' 文字。 s パラメーターは、style フラグが含まれている場合NumberStyles.AllowExponent指数表記で数値を表すことができます。
hexdigits 0 から f、または 0 から F までの 16 進数のシーケンス。

手記

sで終了する NUL (U+0000) 文字は、style引数の値に関係なく、解析操作では無視されます。

10 進数のみを含む文字列 ( NumberStyles.None スタイルに対応) は、 Int32 型の範囲内にある場合、常に正常に解析されます。 残りの NumberStyles メンバーのほとんどは、この入力文字列に存在する必要がない要素を制御します。 次の表は、個々の NumberStyles メンバーが、 sに存在する可能性がある要素に与える影響を示しています。

非複合 NumberStyles 値 数字に加えて s で許可される要素
NumberStyles.None 10 進数のみ。
NumberStyles.AllowDecimalPoint 小数点 ( . ) および 小数部の要素 。 ただし、 小数部の数字 は 1 つ以上の 0 桁のみで構成する必要があります。または、 OverflowException がスローされます。
NumberStyles.AllowExponent s パラメーターでは指数表記を使用することもできます。 s指数表記で数値を表す場合は、0 以外の小数部を含まないInt32データ型の範囲内の整数を表す必要があります。
NumberStyles.AllowLeadingWhite sの先頭にある ws 要素。
NumberStyles.AllowTrailingWhite の末尾にある s 要素。
NumberStyles.AllowLeadingSign 正の符号は 数字の前に表示できます。
NumberStyles.AllowTrailingSign 正の符号は 数字の後に表示されます。
NumberStyles.AllowParentheses 数値を囲むかっこの形式の 符号 要素。
NumberStyles.AllowThousands 桁区切り記号 ( , ) 要素。
NumberStyles.AllowCurrencySymbol $要素。

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

provider パラメーターは、NumberFormatInfoCultureInfo オブジェクトなどのIFormatProvider実装です。 provider パラメーターは、解析に使用されるカルチャ固有の情報を提供します。 providernull場合は、現在のカルチャのNumberFormatInfo オブジェクトが使用されます。

こちらもご覧ください

適用対象