How to Convert a String to int in C#?
The string to int conversion may be required for different reasons. Commonly, this is required if you are taking the user input for numbers and this may be taken as “text” or string.
So, you have to make sure before performing any actions based on that data to convert that string into int, float, or other numeric data types.
In C#, there are different ways of converting a string to int, float, long, etc.
- Parse or TryParse methods
- Using the Convert class methods e.g. ToDecimal, ToInt16, ToInt32, ToInt64, etc.
The section below shows you how to use these ways to convert the string to numbers.
The example of using Parse/TryParse for string to int conversion
The example below shows converting a string to int by using the Parse method. In the code, positive and negative “string” numbers are converted by int32.Parse method.
C# Program:
using System; using System.Text; class string_to_int { static void Main() { int posStr = Int32.Parse("55"); int negStr = Int32.Parse("-155"); Console.WriteLine("Positive number = " + posStr); Console.WriteLine("Negative number = " + negStr); Console.ReadLine(); } }
The output of the above code:
This is how the Parse and TryParse methods work.
Feature | Parse | TryParse |
---|---|---|
Leading/Trailing Spaces | Ignored | Ignored |
Spaces Between Characters | Error | False |
Character Formation | Must be an appropriate numeric type
For example, “101”, “110.45” |
Must be appropriate numeric type:
e.g. “101”, “110.45” |
Error on Space | Yes e.g. “1 01”, “110 .45” | No |
Output | Number | Boolean (true on success, false on failure) |
The example of converting a decimal string
Similarly, by using the float keyword with the Parse method, you may convert a decimal string to a float number in C#.
See the code below for learning how to convert a string with the decimal number to float by the Parse method:
using System; class string_to_float { static void Main() { float Pos_flt_Str = float.Parse("39.55"); float neg_flt_Str = float.Parse("-101.25"); Console.WriteLine("Positive float number = " + Pos_flt_Str); Console.WriteLine("Negative float number = " + neg_flt_Str); Console.ReadLine(); } }
The output:
The example of using TryParse
For example, you are taking the user input from a text box and there are chances that the entered number may contain spaces or characters other than valid numbers. In that case, if TryParse method is used, it will return false.
The simple usage of TryParse method is:
See a simple example below to display the converted number by using the TryParse method:
using System; class string_to_int_TryParse { static void Main() { string str_conv = "101"; int int_conv; bool flg_con = Int32.TryParse(str_conv, out int_conv); Console.WriteLine("Converted number by TryParse = " + int_conv); Console.ReadLine(); } })
As the string contains a valid number, it is converted and its value is assigned to the int_conv variable.
What if a string is not a valid number?
Now, have a look at the code and output below if the string to be converted into a number is a non-valid number:
using System; class TryParse_false { static void Main() { string str_num = "101A"; int int_a; bool ret_val = Int32.TryParse(str_num, out int_a); if (ret_val) { Console.WriteLine("Converted number by TryParse = " + int_a); } else { Console.WriteLine("An invalid string!"); } Console.ReadLine(); } }
You can see that I set the value of the string variable as 101A, which is an invalid number.
Unlike the Parse method, this did not generate an error. Instead, it returned the false value that we dealt in the if..else block.
The output of the above code should be:
An example of string to int by Convert class
By using Convert class methods, you may convert a string to numbers.
- For a string to int conversion, use the Convert.ToInt32 method.
- For converting a string “number” to decimal, use the ToDecimal, ToDouble, etc.
In the following example, a string is converted by Convert.ToInt32(String) to an integer number. Have a look:
using System; class Convert_Class_example { static void Main() { string str_convert = "1010"; int int_converted; int_converted = Convert.ToInt32(str_convert); Console.WriteLine("Result after Convert method = " + int_converted); Console.ReadLine(); } }
The output:
The following numeric conversion methods are also available in the Convert class:
- For short type : ToInt16(str)
- For Integer : ToInt32(str)
- For long type: ToInt64(str)
The example of string to float by Convert class
Similarly, you may use the ToSingle() method of the Convert class for converting a “decimal string” to float.
See the following example:
using System; class Convert_Class_example { static void Main() { string str_convert_flt = "555.5555"; float flt_converted; flt_converted = Convert.ToSingle(str_convert_flt); Console.WriteLine("Float Result = " + flt_converted); Console.ReadLine(); } }
The result: