Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to Convert string to an int?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 153
    Comment on it

    We can convert the string to an int by using following methods:
    1. Convert class
    2. Parse method
    3. TryParse method
    4. A custom method

     

    1.Convert class

    This method is used to convert one datatype to another datatype.

    Convert class contains following methods:

    1. ToBoolean(Boolean) --------> This method returns a Boolean value.

    2. ToByte(Int16) ---------> This method returns a unsigned integer.

    3. ToChar(Char) ----------> This method returns a character value.

    4. ToInt32(Byte) ---------> This method returns a signed integer.

    5. ToInt32(String) ---------> This method returns signed integer.

    Syntax:-

    Convert.ToInt32(variablename)

    It accepts a parameter "variablename" which is used to specify the name of a variable to convert.

     Example:

    string str="41";
    int n=Convert.ToInt32(str);

    2. Parse method

    This method is used to convert one datatype to another datatype.

    Syntax:-

     Int.Parse(variablename)

    It accept one parameter which is a type of String.Its return type value is System.Int32.

    Exception
    Parse throw following exception when string is not valid:

    1. ArgumentNullException ------> This exception is occurs if string is null.

    2. FormatException -------> This exception is occurs if string is not in the correct format.

    3. OverflowException ------> This exception is occurs if string length is less than or greater than the specified value.

     

    Example:-

    class SampleProgramParse
    {
        static void Main()
        {
        string str = "67";
        int n = int.Parse(str);
        Console.WriteLine(n);
        }
    }

    3. TryParse method

    This method is used to convert one datatype to another datatype.TryParse is faster as compare to other methods. TryParse method takes a "out" modifier to specify the variable valid state.

    Syntax:-
      int.TryParse(str, out num);

    It accept two parameters "str" which is a type of System.String.This string type variable contain a number to convert and "out" stores a result of a conversion.

    Its return type values is System.Boolean.

    This method returns zero when the conversion failed otherwise it will return a converted value.

     

    Example:-
     

    string str="58678";
    if (Int32.TryParse(str, out r))
        Console.WriteLine(r);
    else
        Console.WriteLine("Conversion failed!!!!");

    4. A custom method

     

    In this method we convert each character in numeric string through loops. In this code we writing our own method which convert string to an int.

    public static int IntParseString(string str)
         {
         int r = 0;
         for (int i = 0; i < str.Length; i++)
         {
             char ch = str[i];
             r = 10 * r + (ch - 48);
         }
         return r;
         }
    public static int IntParseChar(char ch)
         {
             int r = 0;
             r = 10 * result + (ch - 48);
             return r;
         }

     

    We prefer TryParse method to convert the string to an int because TryParse method returns false if the specified string is not in a valid numeric format. So, we are usually prefer this method for the conversion.

     

     

    A sample C# program to convert string to an int using Tryparse method

    class SampleProgram
    {
       static void Main()
        {
    	
    	string str1 = "ABC";
    	int n1;
    	bool r = int.TryParse(str1, out n1);
    	if (r == false)
    	{
    	    Console.WriteLine(n1);
    	}
    	string str2 = "5643";
    	int n2;
    	if (int.TryParse(str2, out n2))
    	{
    	    Console.WriteLine(n2);
    	}	
    	
    	
        }
    }

    Output:

    0
    5643

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: