C# Int32 Parse(String)

Description

Int32 Parse(String) converts the string representation of a number to its 32-bit signed integer equivalent.

Syntax

Int32.Parse(String) has the following syntax.


public static int Parse(
  string s
)

Parameters

Int32.Parse(String) has the following parameters.

  • s - A string containing a number to convert.

Returns

Int32.Parse(String) method returns A 32-bit signed integer equivalent to the number contained in s.

Example

The following example demonstrates how to convert a string value into a 32-bit signed integer value using the Int32.Parse(String) method.


/*from  w w  w.j av  a2s. c  o m*/
using System;

public class ParseInt32
{
   public static void Main()
   {
      Convert("  1  ");
      Convert(" -2 ");
      Convert(" +8 ");
      Convert("  1.3");
   }

   private static void Convert(string value)
   {
      try
      {
         int number = Int32.Parse(value);
         Console.WriteLine("Converted '{0}' to {1}.", value, number);
      }
      catch (FormatException)
      {
         Console.WriteLine("Unable to convert '{0}'.", 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