There are three easiest ways which allow you to convert string data type to int:
- Parse method
- TryParse method
- Convert class
In the following article I’ll explain each one and show the example of its usage.
Parse method
Basic form of Parse method receives string as an input parameter and returns converted value as an integer data type. In case the format of input value is not able to be converted, an exception is being thrown.
Parse method is overloaded and contains two more parameters which can be optionally passed:
- number style which defines allowed input string format (e.g. allows currency symbol, decimal point etc.).
- format provider which defines culture-specific format
1 2 3 4 5 6 7 8 9 |
int.Parse("123"); // returns 123 int.Parse("abc"); // throws FormatException int.Parse("$123"); // throws FormatException int.Parse("$123", NumberStyles.AllowCurrencySymbol, new CultureInfo("en-GB")); // throws FormatException as en-GB currency symbol is not $ int.Parse("$123", NumberStyles.AllowCurrencySymbol, new CultureInfo("en-US")); // returns 123 |
Above example shows converting to int type which is equivalent to Int32. You can also use following forms depending on your needs.
1 2 3 |
Int16.Parse("123"); Int32.Parse("123"); Int64.Parse("123"); |
TryParse method
TryParse method is very similar to Parse with the difference that it returns true or false (depending on the success or failure during conversion) and a result of conversion is available through output parameter.
The simplest form of TryParse has two parameters: string to be converted and output parameter which is the conversion result. Similar as Parse, there are overloads which allow to define number style and format provider.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
int result; bool succeeded; succeeded = int.TryParse("123", out result); // succeeded = true, result = 123 succeeded = int.TryParse("abc", out result); // succeeded = false, result = 0 succeeded = int.TryParse("$123", out result); // succeeded = false, result = 0 succeeded = int.TryParse("$123", NumberStyles.AllowCurrencySymbol, new CultureInfo("en-GB"), out result); // succeeded = false, result = 0 succeeded = int.TryParse("$123", NumberStyles.AllowCurrencySymbol, new CultureInfo("en-US"), out result); // succeeded = true, result = 123 |
It is worth noticing that TryParse returns false instead of throwing exception in the situation when conversion fails.
Convert class
Convert is a static class where one of the exposed methods is ToInt32. It is a function which allows to convert various types (including string) to integer. Internally it uses Parse method which was described above, as a result of what it behaves in a similar way: returns converted value and throws exception in case of failure.
1 2 3 |
Convert.ToInt32("123"); // returns 123 Convert.ToInt32("abc"); // throws FormatException Convert.ToInt32("$123"); // throws FormatException |
If you would like to receive Int16 or Int64 you can use accordingly ToInt16 or ToInt64 methods.