次の方法で共有


SByte.Parse メソッド

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

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

オーバーロードの一覧

数値の文字列形式を、それと等価な 8 ビット符号付き整数に変換します。このメソッドは、CLS と互換性がありません。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function Parse(String) As SByte

[C#] public static sbyte Parse(string);

[C++] public: static char Parse(String*);

[JScript] public static function Parse(String) : SByte;

指定したカルチャに固有の書式による数値の文字列形式を、それと等価な 8 ビット符号付き整数に変換します。このメソッドは、CLS と互換性がありません。

[Visual Basic] Overloads Public Shared Function Parse(String, IFormatProvider) As SByte

[C#] public static sbyte Parse(string, IFormatProvider);

[C++] public: static char Parse(String*, IFormatProvider*);

[JScript] public static function Parse(String, IFormatProvider) : SByte;

指定したスタイルの数値の文字列形式を、それと等価の 8 ビット符号付き整数に変換します。このメソッドは、CLS と互換性がありません。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles) As SByte

[C#] public static sbyte Parse(string, NumberStyles);

[C++] public: static char Parse(String*, NumberStyles);

[JScript] public static function Parse(String, NumberStyles) : SByte;

指定したスタイルおよびカルチャに固有の書式による数値の文字列形式を、それと等価の 8 ビット符号付き整数に変換します。このメソッドは、CLS と互換性がありません。

.NET Compact Framework でもサポート。

[Visual Basic] Overloads Public Shared Function Parse(String, NumberStyles, IFormatProvider) As SByte

[C#] public static sbyte Parse(string, NumberStyles, IFormatProvider);

[C++] public: static char Parse(String*, NumberStyles, IFormatProvider*);

[JScript] public static function Parse(String, NumberStyles, IFormatProvider) : SByte;

使用例

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

[Visual Basic, C#, C++] メモ   ここでは、Parse のオーバーロード形式のうちの 1 つだけについて、使用例を示します。その他の例については、各オーバーロード形式のトピックを参照してください。

 
' 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++ のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

参照

SByte 構造体 | SByte メンバ | System 名前空間