Click here to Skip to main content
15,885,216 members
Please Sign up or sign in to vote.
2.67/5 (2 votes)
See more:
how to convert string formate to int formate
Posted

As Aarti said you can use Convert.ToInt32() method. But a better solution is to use Int.TryParse() method. The main reason why it is because less error-prone. If an error occurs during parsing, you don’t get an exception so your program won’t crash.

An example usage is;
C#
string text = "123";
int number;
if(int.TryParse(text, out number))
{
    Console.WriteLine(number);
}
else
{
    Console.WriteLine("Could not parse string!");
}


Good luck,
OI
 
Share this answer
 
v2
Comments
Ankur\m/ 20-Mar-13 5:11am    
I took me way too long to type that answer. I think I will delete mine as it's almost what you already said.
Btw 5 to you!
Orcun Iyigun 20-Mar-13 6:06am    
Thank you Ankur :)
vinodkumarnie 20-Mar-13 5:15am    
Nicely said..
Orcun Iyigun 20-Mar-13 6:06am    
Thanks.
Very interesting thing in conversion is Input string, here some examples to understand convertion..

1.
C#
int numVal = Int32.Parse("-105");
Console.WriteLine(numVal);
// Output: -105

2.
C#
int j;
bool result = Int32.TryParse("-105", out j);
if (true == result)
    Console.WriteLine(j);
else
    Console.WriteLine("String could not be parsed.");
// Output: -105

3.
C#
try
{
    int m = Int32.Parse("abc");
}
catch (FormatException e)
{
    Console.WriteLine(e.Message);
}
// Output: Input string was not in a correct format.

4.
C#
string inputString = "abc";
int numValue;
bool parsed = Int32.TryParse(inputString, out numValue);
 
if (!parsed)
    Console.WriteLine("Int32.TryParse could not parse '{0}' to an int.\n", inputString);
// Output: Int32.TryParse could not parse 'abc' to an int.


From above example you come to know that input string should be in correct format..
 
Share this answer
 
Comments
Orcun Iyigun 20-Mar-13 5:06am    
Well explained. My 5.
vinodkumarnie 20-Mar-13 5:08am    
Thank you..
see this example
C#
int abc = Convert.ToInt32("12");

Happy Coding!
:)
 
Share this answer
 
Comments
Orcun Iyigun 20-Mar-13 5:05am    
Convert.ToInt32() is an acceptable answer but TryParse() method is more convenient. Because of that I rated 4. Please check my answer.
Int32.TryParse Method (String, Int32)[^] is a better way of doing it as if the conversion fails due to invalid value, it won't raise an exception.
C#
string someIntNumber = "14";
string invalidIntnumber = "14abc"
int result;
Convert.ToInt32(someIntNumber); //works fine
Convert.ToInt32(invalidIntnumber); //raises an exception

bool success1 = int.TryParse(someIntNumber, out result); //returns true. result contains 14
bool success2 = int.TryParse(invalidIntnumber, out result); //returns false. result contains 0


Hope the example above helps you understand better!
 
Share this answer
 
you will visit this following link


[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900