Export (0) Print
Expand All

SByte.Parse Method (String, NumberStyles, IFormatProvider)

Converts the string representation of a number in a specified style and culture-specific format to its 8-bit signed integer equivalent.

This method is not CLS-compliant.  The CLS-compliant alternative is Parse(String).

Namespace: System
Assembly: mscorlib (in mscorlib.dll)

[CLSCompliantAttribute(false)] 
public static sbyte Parse (
	string s,
	NumberStyles style,
	IFormatProvider provider
)
/** @attribute CLSCompliantAttribute(false) */ 
public static SByte Parse (
	String s, 
	NumberStyles style, 
	IFormatProvider provider
)
CLSCompliantAttribute(false) 
public static function Parse (
	s : String, 
	style : NumberStyles, 
	provider : IFormatProvider
) : sbyte

Parameters

s

A string representing a number to convert.

style

A bitwise combination of NumberStyles values that indicates the permitted format of s. A typical value to specify is Integer.

provider

An IFormatProvider that supplies culture-specific formatting information about s.

Return Value

An 8-bit signed integer equivalent to the number specified in s.

Exception typeCondition

ArgumentNullException

s is a null reference (Nothing in Visual Basic).

FormatException

s is not in a format compliant with style.

OverflowException

s represents a number less than MinValue or greater than MaxValue.

ArgumentException

style is not a NumberStyles value.

-or-

style is not a combination of AllowHexSpecifier and HexNumber values.

The s parameter contains a number of the form:

[ws][sign]digits[ws]

Or, if style includes AllowHexSpecifier:

[ws]hexdigits[ws]

Items in square brackets ([ and ]) are optional, and other items are as follows.

ws

Optional white space if permitted by style.

sign

An optional sign.

digits

A sequence of digits from 0 through 9.

hexdigits

A sequence of hexadecimal digits from 0 through f, or 0 through F.

The provider parameter is an IFormatProvider that obtains a NumberFormatInfo object. The NumberFormatInfo provides culture-specific information about the format of s. If provider is a null reference (Nothing in Visual Basic), the NumberFormatInfo for the current culture is used.

The following code example parses string representations of SByte (signed byte) values with several overloads of the Parse method.

// 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.
*/ 

// Example of the SByte.Parse( ) methods.
import System.*;
import 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.
        for (int iCtr = 0; iCtr < sByteFormats.length; iCtr++) {
            String sByteString = sByteFormats[iCtr];
            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.GetHashCode() < 0) {
                        sByteNumber = (System.SByte)SByte.Parse(sByteString);
                    }
                    else {
                        sByteNumber = (System.SByte)SByte.Parse(sByteString, 
                            styles);
                    }
                }
                else {
                    if (styles.GetHashCode() < 0) {
                        sByteNumber = (System.SByte)SByte.Parse(sByteString, 
                            provider);
                    }
                    else {
                        sByteNumber = (System.SByte)SByte.Parse(sByteString, 
                            styles, provider);
                    }
                } 
                // Display the resulting value if Parse succeeded.
                Console.WriteLine("succeeded: {0}", sByteNumber);
            }
            catch (System.Exception  ex) {
                // Display the exception message if Parse failed.
                Console.WriteLine("failed: {0}", ex.get_Message());
            }
        }
    } //SByteParse

    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.get_NumberFormat();
        numFormat.set_NumberNegativePattern (0);
        
        // Change the digit group separator to '_' and the digit
        // group size to 1.
        numFormat.set_NumberGroupSeparator("_");
        numFormat.set_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);
    } //RunParseDemo

    public static void main(String[] args)
    {
        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();
    } //main
} //SByteParseDemo

/*
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.
*/

Windows 98, Windows 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft