次の方法で共有


SByte.Parse メソッド (String, NumberStyles, IFormatProvider)

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

SByte 型は CLS との互換性がありません。CLS と互換性のある型は、Int16 です。CLS との互換性に関する詳細については 「共通言語仕様の概要」 を参照してください。

<CLSCompliant(False)>
Overloads Public Shared Function Parse( _   ByVal s As String, _   ByVal style As NumberStyles, _   ByVal provider As IFormatProvider _) As SByte
[C#]
[CLSCompliant(false)]
public static sbyte Parse(strings,NumberStylesstyle,IFormatProviderprovider);
[C++]
[CLSCompliant(false)]
public: static char Parse(String* s,NumberStylesstyle,IFormatProvider* provider);
[JScript]
public
   CLSCompliant(false)
static function Parse(s : String,style : NumberStyles,provider : IFormatProvider) : SByte;

パラメータ

  • s
    変換する数値を表す文字列。
  • style
    s の許容形式を示す、1 つ以上の NumberStyles 定数の組み合わせ。
  • provider
    s に関するカルチャに固有の書式情報を提供する IFormatProvider

戻り値

s で指定した数値と等しい 8 ビット符号付き整数。

例外

例外の種類 条件
ArgumentNullException s が null 参照 (Visual Basic では Nothing) です。
FormatException s の書式が、 style に準拠した書式ではありません。
OverflowException sMinValue 未満の数値か、 MaxValue より大きい数値を表しています。
ArgumentException style が、 NumberStyles 列挙定数の有効な組み合わせではありません。

解説

s パラメータには、次の書式の数値を指定します。

[ws][sign]digits[ws]

角かっこ ([ および ]) で囲まれている項目は省略可能です。その他の項目は次のとおりです。

  • ws
    省略可能な空白。
  • sign
    省略可能な符号。
  • digits
    0 から 9 までの一連の数字。

style パラメータには、ビットごとの OR 演算を使用して、1 つ以上の NumberStyles 列挙定数を組み合わせて指定できます。 Any および AllowDecimalPoint は、このメソッドから返される型として無効であるため除きます。

provider パラメータは、 NumberFormatInfo オブジェクトを取得する IFormatProvider です。 NumberFormatInfo は、 s の書式に関するカルチャ固有の情報を提供します。 provider が null 参照 (Visual Basic では Nothing) の場合は、現在のカルチャの NumberFormatInfo が使用されます。

使用例

[Visual Basic, C#, C++] Parse メソッドの複数のオーバーロードを使用して SByte (符号付きバイト) 値の文字列形式を解析するコード例を次に示します。

 
' Example of the SByte.Parse( ) methods.
Imports System
Imports System.Globalization
Imports Microsoft.VisualBasic

Module SByteParseDemo
    
    Sub SByteParse( styles As NumberStyles, _
        provider As IFormatProvider )

        Dim sbyteFormats As String( ) = { _
            " 99 ",     " +123 ",   " (123) ", _
            " -123 ",   " 1_2_3",   " 7E " }
            
        ' Parse each string in the sbyteFormats array, using 
        ' NumberStyles and IFormatProvider, if specified.
        Dim sbyteString As String
        For Each sbyteString In  sbyteFormats

            Dim sbyteNumber As SByte
                
            ' Display the first part of the output line.
            Console.Write( "  Parse of {0,-12}", _
                String.Format( """{0}""", sbyteString ) )

            ' Use the appropriate SByte.Parse overload, based on 
            ' the parameters that are specified.
            Try
                If provider Is Nothing Then
                    If styles < 0 Then
                        sbyteNumber = SByte.Parse( sbyteString )
                    Else
                        sbyteNumber = _
                            SByte.Parse( sbyteString, styles )
                    End If
                ElseIf styles < 0 Then
                    sbyteNumber = _
                        SByte.Parse( sbyteString, provider )
                Else
                    sbyteNumber = SByte.Parse( _
                        sbyteString, styles, provider )
                End If
                
                ' Display the resulting value if Parse succeeded.
                Console.WriteLine( "succeeded: {0}", _
                    sbyteNumber )

            ' Display the exception message if Parse failed.
            Catch ex As Exception
                Console.WriteLine( "failed: {0}", ex.Message )
            End Try
        Next sbyteString
    End Sub 
        
    Sub RunParseDemo( )
            
        ' Do not use IFormatProvider or NumberStyles.
        Console.WriteLine( vbCrLf & _
            "NumberStyles and IFormatProvider are not used:" )
        SByteParse( CType( -1, NumberStyles ), Nothing )
            
        ' Use NumberStyles.HexNumber; do not use IFormatProvider.
        Console.WriteLine( vbCrLf & "NumberStyles." & _
            "HexNumber is used; IFormatProvider is not used:" )
        SByteParse( NumberStyles.HexNumber, Nothing )
            
        ' Get the NumberFormatInfo object from the invariant 
        ' culture, and enable parentheses to indicate negative.
        Dim culture As New CultureInfo( "" )
        Dim numFormat As NumberFormatInfo = culture.NumberFormat
        numFormat.NumberNegativePattern = 0
            
        ' Change the digit group separator to '_' and the digit
        ' group size to 1.
        numFormat.NumberGroupSeparator = "_"
        numFormat.NumberGroupSizes = New Integer( ) { 1 }
            
        ' Use the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( vbCrLf & _
            "A NumberStyles value is not used, but the " & _
            "IFormatProvider sets the group " & vbCrLf & "" & _
            "separator = '_', group size = 1, and negative " & _
            "pattern = ( ):" )
        SByteParse( CType( -1, NumberStyles ), numFormat )
            
        ' Use NumberStyles.Number and NumberStyles.AllowParentheses.
        Console.WriteLine( vbCrLf & _
            "NumberStyles.Number, NumberStyles.AllowParentheses, " & _
            "and the same " & vbCrLf & "IFormatProvider are used:" )
        SByteParse( NumberStyles.Number + _
            NumberStyles.AllowParentheses, numFormat )
    End Sub 
        
    Sub Main( )
        Console.WriteLine( "This example of" & vbCrLf & _
            "  SByte.Parse( String )," & vbCrLf & _
            "  SByte.Parse( String, NumberStyles )," & vbCrLf & _
            "  SByte.Parse( String, IFormatProvider ), and" & _
            vbCrLf & "  SByte.Parse( String, NumberStyles, " & _
            "IFormatProvider )" & vbCrLf & _
            "generates the following output when parsing " & _
            "string representations" & vbCrLf & "of SByte " & _
            "values with each of these forms of SByte.Parse( )." )
            
        RunParseDemo( )

    End Sub 
End Module 

' This example of
'   SByte.Parse( String ),
'   SByte.Parse( String, NumberStyles ),
'   SByte.Parse( String, IFormatProvider ), and
'   SByte.Parse( String, NumberStyles, IFormatProvider )
' generates the following output when parsing string representations
' of SByte values with each of these forms of SByte.Parse( ).
' 
' NumberStyles and IFormatProvider are not used:
'   Parse of " 99 "      succeeded: 99
'   Parse of " +123 "    succeeded: 123
'   Parse of " (123) "   failed: Input string was not in a correct format.
'   Parse of " -123 "    succeeded: -123
'   Parse of " 1_2_3"    failed: Input string was not in a correct format.
'   Parse of " 7E "      failed: Input string was not in a correct format.
' 
' NumberStyles.HexNumber is used; IFormatProvider is not used:
'   Parse of " 99 "      succeeded: -103
'   Parse of " +123 "    failed: Input string was not in a correct format.
'   Parse of " (123) "   failed: Input string was not in a correct format.
'   Parse of " -123 "    failed: Input string was not in a correct format.
'   Parse of " 1_2_3"    failed: Input string was not in a correct format.
'   Parse of " 7E "      succeeded: 126
' 
' A NumberStyles value is not used, but the IFormatProvider sets the group
' separator = '_', group size = 1, and negative pattern = ( ):
'   Parse of " 99 "      succeeded: 99
'   Parse of " +123 "    succeeded: 123
'   Parse of " (123) "   failed: Input string was not in a correct format.
'   Parse of " -123 "    succeeded: -123
'   Parse of " 1_2_3"    failed: Input string was not in a correct format.
'   Parse of " 7E "      failed: Input string was not in a correct format.
' 
' NumberStyles.Number, NumberStyles.AllowParentheses, and the same
' IFormatProvider are used:
'   Parse of " 99 "      succeeded: 99
'   Parse of " +123 "    succeeded: 123
'   Parse of " (123) "   succeeded: -123
'   Parse of " -123 "    succeeded: -123
'   Parse of " 1_2_3"    succeeded: 123
'   Parse of " 7E "      failed: Input string was not in a correct format.

[C#] 
// Example of the SByte.Parse( ) methods.
using System;
using System.Globalization;

class SByteParseDemo
{
    static void SByteParse( NumberStyles styles, 
        IFormatProvider provider )
    {
        string[ ] sbyteFormats = {
            " 99 ",     " +123 ",   " (123) ", 
            " -123 ",   " 1_2_3",   " 7E " };
            
        // Parse each string in the sbyteFormats array, using 
        // NumberStyles and IFormatProvider, if specified.
        foreach( string sbyteString in sbyteFormats )
        {
            SByte sbyteNumber;
                
            // Display the first part of the output line.
            Console.Write( "  Parse of {0,-12}", 
                String.Format( "\"{0}\"", sbyteString ) );

            try
            {
                // Use the appropriate SByte.Parse overload, based 
                // on the parameters that are specified.
                if( provider == null )
                    if( styles < 0 )
                        sbyteNumber = SByte.Parse( sbyteString );
                    else
                        sbyteNumber = 
                            SByte.Parse( sbyteString, styles );
                else if( styles < 0 )
                    sbyteNumber = 
                        SByte.Parse( sbyteString, provider );
                else
                    sbyteNumber = SByte.Parse(
                        sbyteString, styles, provider );
                
                // Display the resulting value if Parse succeeded.
                Console.WriteLine( "succeeded: {0}", 
                    sbyteNumber );
            }
            catch( Exception ex )
            {
                // Display the exception message if Parse failed.
                Console.WriteLine( "failed: {0}", ex.Message );
            }
        }
    } 
        
    static void RunParseDemo( )
    {
        // Do not use IFormatProvider or NumberStyles.
        Console.WriteLine(
            "\nNumberStyles and IFormatProvider are not used:" );
        SByteParse( (NumberStyles)(-1), null );
            
        // Use NumberStyles.HexNumber; do not use IFormatProvider.
        Console.WriteLine( "\nNumberStyles.HexNumber " +
            "is used; IFormatProvider is not used:" );
        SByteParse( NumberStyles.HexNumber, null );
            
        // Get the NumberFormatInfo object from the invariant 
        // culture, and enable parentheses to indicate negative.
        CultureInfo         culture = new CultureInfo( "" );
        NumberFormatInfo    numFormat = culture.NumberFormat;
        numFormat.NumberNegativePattern = 0;
            
        // Change the digit group separator to '_' and the digit
        // group size to 1.
        numFormat.NumberGroupSeparator = "_";
        numFormat.NumberGroupSizes = new int[ ] { 1 };
            
        // Use the NumberFormatInfo object as the IFormatProvider.
        Console.WriteLine( "\nA NumberStyles value is not used, " +
            "but the IFormatProvider sets the group \nseparator = " +
            "'_', group size = 1, and negative pattern = ( ):" );
        SByteParse( (NumberStyles)(-1), numFormat );
            
        // Use NumberStyles.Number and NumberStyles.AllowParentheses.
        Console.WriteLine( 
            "\nNumberStyles.Number, NumberStyles.AllowParentheses, " +
            "and the same \nIFormatProvider are used:" );
        SByteParse( NumberStyles.Number | 
            NumberStyles.AllowParentheses, numFormat );
    } 
        
    static void Main( )
    {
        Console.WriteLine( "This example of\n" +
            "  SByte.Parse( String ),\n" +
            "  SByte.Parse( String, NumberStyles ),\n" +
            "  SByte.Parse( String, IFormatProvider ), and\n" +
            "  SByte.Parse( String, NumberStyles, IFormatProvider )" +
            "\ngenerates the following output when parsing " +
            "string representations\nof SByte values with each " +
            "of these forms of SByte.Parse( )." );
            
        RunParseDemo( );
    } 
} 

/*
This example of
  SByte.Parse( String ),
  SByte.Parse( String, NumberStyles ),
  SByte.Parse( String, IFormatProvider ), and
  SByte.Parse( String, NumberStyles, IFormatProvider )
generates the following output when parsing string representations
of SByte values with each of these forms of SByte.Parse( ).

NumberStyles and IFormatProvider are not used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles.HexNumber is used; IFormatProvider is not used:
  Parse of " 99 "      succeeded: -103
  Parse of " +123 "    failed: Input string was not in a correct format.
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    failed: Input string was not in a correct format.
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      succeeded: 126

A NumberStyles value is not used, but the IFormatProvider sets the group
separator = '_', group size = 1, and negative pattern = ( ):
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles.Number, NumberStyles.AllowParentheses, and the same
IFormatProvider are used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   succeeded: -123
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    succeeded: 123
  Parse of " 7E "      failed: Input string was not in a correct format.
*/ 

[C++] 
// Example of the SByte::Parse( ) methods.
#using <mscorlib.dll>
using namespace System;
using namespace System::Globalization;
using namespace System::Collections;

void SByteParse( NumberStyles styles, IFormatProvider* provider )
{
    String* sbyteFormats [ ] = __gc new String*[ 6 ];
        sbyteFormats[ 0 ] = S" 99 ";
        sbyteFormats[ 1 ] = S" +123 ";
        sbyteFormats[ 2 ] = S" (123) ";
        sbyteFormats[ 3 ] = S" -123 ";
        sbyteFormats[ 4 ] = S" 1_2_3";
        sbyteFormats[ 5 ] = S" 7E ";
        
    // Parse each string in the sbyteFormats array, using 
    // NumberStyles and IFormatProvider, if specified.
    // Implements foreach( String* sbyteString in sbyteFormats ).
    IEnumerator*    myEnum = sbyteFormats->GetEnumerator( );
    while( myEnum->MoveNext( ) )
    {
        String* sbyteString = __try_cast<String*>( myEnum->Current );
        SByte   sbyteNumber;
            
            // Display the first part of the output line.
        Console::Write( "  Parse of {0,-12}", 
            String::Format( "\"{0}\"", sbyteString ) );

        try
        {
            // Use the appropriate SByte.Parse overload, based 
            // on the parameters that are specified.
            if( provider == (IFormatProvider*)0 )
                if( styles < 0 )
                    sbyteNumber = SByte::Parse( sbyteString );
                else
                    sbyteNumber = SByte::Parse( sbyteString, styles );
            else if( styles < 0 )
                sbyteNumber = SByte::Parse( sbyteString, provider );
            else
                sbyteNumber = 
                    SByte::Parse( sbyteString, styles, provider );
            
            // Display the resulting value if Parse succeeded.
            Console::WriteLine( S"succeeded: {0}", 
                __box( sbyteNumber ) );
        }
        catch( Exception* ex )
        {
            // Display the exception message if Parse failed.
            Console::WriteLine( S"failed: {0}", ex->Message );
        }
    }
} 
    
void RunParseDemo( )
{
    IFormatProvider* null = 0;

    // Do not use IFormatProvider or NumberStyles.
    Console::WriteLine( 
        S"\nNumberStyles and IFormatProvider are not used:" );
    SByteParse( (NumberStyles)(-1), null );
        
    // Use NumberStyles::HexNumber; do not use IFormatProvider.
    Console::WriteLine( S"\nNumberStyles::HexNumber " 
        S"is used; IFormatProvider is not used:" );
    SByteParse( NumberStyles::HexNumber, null );
        
    // Get the NumberFormatInfo object from the invariant 
    // culture, and enable parentheses to indicate negative.
    CultureInfo*        culture = new CultureInfo( S"" );
    NumberFormatInfo*   numFormat = culture->NumberFormat;
    numFormat->NumberNegativePattern = 0;
        
    // Change the digit group separator to '_' and the digit
    // group size to 1.
    numFormat->NumberGroupSeparator = S"_";
    Int32 sizes __gc [] = { 1 };
    numFormat->NumberGroupSizes = sizes;
        
    // Use the NumberFormatInfo object as the IFormatProvider.
    Console::WriteLine( S"\nA NumberStyles value is not used, " 
        S"but the IFormatProvider sets the group \nseparator = " 
        S"'_', group size = 1, and negative pattern = ( ):" );
    SByteParse( (NumberStyles)(-1), numFormat );
        
    // Use NumberStyles::Number and NumberStyles::AllowParentheses.
    Console::WriteLine( 
        S"\nNumberStyles::Number, NumberStyles::AllowParentheses, " 
        S"and the same \nIFormatProvider are used:" );
    SByteParse( (NumberStyles)(NumberStyles::Number | 
        NumberStyles::AllowParentheses), numFormat );
} 
    
void main( )
{
    Console::WriteLine( S"This example of\n" 
        S"  SByte::Parse( String* ),\n" 
        S"  SByte::Parse( String*, NumberStyles ),\n" 
        S"  SByte::Parse( String*, IFormatProvider* ), and\n" 
        S"  SByte::Parse( String*, NumberStyles, IFormatProvider* )" 
        S"\ngenerates the following output when " 
        S"parsing string representations\nof SByte " 
        S"values with each of these forms of SByte::Parse( )." );
        
    RunParseDemo( );
}

/*
This example of
  SByte::Parse( String* ),
  SByte::Parse( String*, NumberStyles ),
  SByte::Parse( String*, IFormatProvider* ), and
  SByte::Parse( String*, NumberStyles, IFormatProvider* )
generates the following output when parsing string representations
of SByte values with each of these forms of SByte::Parse( ).

NumberStyles and IFormatProvider are not used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles::HexNumber is used; IFormatProvider is not used:
  Parse of " 99 "      succeeded: -103
  Parse of " +123 "    failed: Input string was not in a correct format.
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    failed: Input string was not in a correct format.
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      succeeded: 126

A NumberStyles value is not used, but the IFormatProvider sets the group
separator = '_', group size = 1, and negative pattern = ( ):
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   failed: Input string was not in a correct format.
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    failed: Input string was not in a correct format.
  Parse of " 7E "      failed: Input string was not in a correct format.

NumberStyles::Number, NumberStyles::AllowParentheses, and the same
IFormatProvider are used:
  Parse of " 99 "      succeeded: 99
  Parse of " +123 "    succeeded: 123
  Parse of " (123) "   succeeded: -123
  Parse of " -123 "    succeeded: -123
  Parse of " 1_2_3"    succeeded: 123
  Parse of " 7E "      failed: Input string was not in a correct format.
*/

[JScript] JScript のサンプルはありません。Visual Basic、C#、および C++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

SByte 構造体 | SByte メンバ | System 名前空間 | SByte.Parse オーバーロードの一覧 | 書式設定の概要 | ToString