System.Int32.Parse Method

Converts the string representation of a number to its 32-bit signed integer equivalent.


public static int Parse (string s)


A string containing a number to convert.


A 32-bit signed integer equivalent to the number contained in s.


ArgumentNullException s is a null reference.
FormatException s is not in the correct style.
OverflowException s represents a number greater than int.MaxValue or less than int.MinValue.


The s parameter contains a number of the form:


Items in square brackets ([ and ]) are optional. The following table describes each element.


Optional white space.


An optional sign.


A sequence of digits ranging from 0 to 9.

The s parameter is interpreted using the System.Globalization.NumberStyles.Integer style. In addition to decimal digits, only leading and trailing spaces together with a leading sign are allowed. To explicitly define the style elements that can be present in s, use either the int.Parse(string, System.Globalization.NumberStyles) or the int.Parse(string, System.Globalization.NumberStyles, IFormatProvider) method.

The s parameter is parsed using the formatting information in a System.Globalization.NumberFormatInfo object initialized for the current system culture. For more information, see System.Globalization.NumberFormatInfo.CurrentInfo. To parse a string using the formatting information of some other culture, use the int.Parse(string, System.Globalization.NumberStyles, IFormatProvider) method.


This example demonstrates parsing a string to a int.

C# Example

using System;
public class Int32ParseClass {
  public static void Main() {
    string str = "  100   ";
    Console.WriteLine("String: \"{0}\" <Int32> {1}",str,Int32.Parse(str));

The output is

String: " 100 " <Int32> 100


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0,,