C# SByte Parse(String, NumberStyles)

Description

SByte Parse(String, NumberStyles) converts the string representation of a number in a specified style to its 8-bit signed integer equivalent.

Syntax

SByte.Parse(String, NumberStyles) has the following syntax.


[CLSCompliantAttribute(false)]//from   w w w  . j a  v a  2  s . c  o m
public static sbyte Parse(
  string s,
  NumberStyles style
)

Parameters

SByte.Parse(String, NumberStyles) has the following parameters.

  • s - A string that contains a number to convert. The string is interpreted using the style specified by style.
  • style - A bitwise combination of the enumeration values that indicates the style elements that can be present in s. A typical value to specify is NumberStyles.Integer.

Returns

SByte.Parse(String, NumberStyles) method returns An 8-bit signed integer that is equivalent to the number specified in s.

Example

The following example parses string representations of SByte values with the Parse(String, NumberStyles) method.


/* ww  w.  jav  a 2 s .  co  m*/
using System;
using System.Globalization;

public class Example
{
   public static void Main()
   {
      NumberStyles style;
      sbyte number;

      string[] values1 = { " 121 ", "121", "-121" };
      style = NumberStyles.None;
      Console.WriteLine("Styles: {0}", style.ToString());
      foreach (string value in values1)
      {
         try {
            number = SByte.Parse(value, style);
            Console.WriteLine("   Converted '{0}' to {1}.", value, number);
         }   
         catch (FormatException) {
            Console.WriteLine("   Unable to parse '{0}'.", value);
         }
      }

      style = NumberStyles.Integer | NumberStyles.AllowTrailingSign;
      string[] values2 = { " 123+", " 123 +", "+123", "(123)", "   +123  " };
      foreach (string value in values2)
      {
         try {
            number = SByte.Parse(value, style);
            Console.WriteLine("   Converted '{0}' to {1}.", value, number);
         }   
         catch (FormatException) {
            Console.WriteLine("   Unable to parse '{0}'.", value);
         }      
         catch (OverflowException) {
            Console.WriteLine("   '{0}' is out of range of the SByte type.", value);         
         }
      }      
   }
}

The code above generates the following result.





















Home »
  C# Tutorial »
    System »




Array
BitConverter
Boolean
Byte
Char
Console
ConsoleKeyInfo
Convert
DateTime
DateTimeOffset
Decimal
Double
Enum
Environment
Exception
Guid
Int16
Int32
Int64
Math
OperatingSystem
Random
SByte
Single
String
StringComparer
TimeSpan
TimeZone
TimeZoneInfo
Tuple
Tuple
Tuple
Type
UInt16
UInt32
UInt64
Uri
Version