Thursday, June 25, 2009

How to convert string to int

        //Int32.TryParse Method (String, Int32%)
        // This method Converts the string representation of a number to its 32-bit signed integer equivalent,it return whether conversion succeeded or not
        int num;
        string[] value = { "123", "test", "-123" };
        for (int i = 0; i < value.Length - 1; i++)
        {
            bool result = Int32.TryParse(value[i].ToString(), out num);
            if (result)
            {
                Response.Write("String " + value[i].ToString() + " Converted Successfully");
            }
            else
            {
                if (value[i] == null) value[i] = "";
                Response.Write("Attempted For " + value[i] + " failed.");
            }
        }

No comments: