How to convert a String to an Int?

+2 Timmy Cafe · April 27, 2015
I am trying to create a function that will convert a string to an integer. I can't just use the


function, because it is not available to me, so i have to make my own. So i wrote a function that takes a string, makes a List<int> and compares each character in the string to a number and adds it to the List, but i do not know how to combine the list into one big int. I may be over looking something very obvious, but can someone help me out?

here is my function so far:

public static int convertToInt(string str){
            List strInt = new List();
            #region ChkNum
            foreach (char x in str){
                else if (x.Equals("1")) {
                else if (x.Equals("2"))
                else if (x.Equals("3"))
                else if (x.Equals("4"))
                else if (x.Equals("5"))
                else if (x.Equals("6"))
                else if (x.Equals("7"))
                else if (x.Equals("8"))
                else if (x.Equals("9"))

Post a Reply


Oldest  Newest  Rating
+2 Sirajus Salekin · April 27, 2015
Hope this one helps :) , btw, you have to make sure the user inputs a valid string as input, not containing non number chars.

public static int convertToInt(string userInput)
        try {
            int index = 0, number = 0;
            boolean isNegetive = false;
            if(userInput[0] == '-'){
                isNegetive = true;
            while( index < userInput.length() ){
                number *= 10;
                number += userInput[index++] - '0';                         
                number = -number;
            return number;
        } catch (Exception e) {

return 0;

+1 Timmy Cafe · April 27, 2015
@Sirajus Salekin  Thank you! It worked perfectly!

I have another problem, I wanting to set my cursor position but i do not have access to the

Console.SetCursorPosition(Column, Row);

I am not for sure how i could do this, i was thinking each time something is written to the console to save the position of each line, but this is just a theory that i do not know how to implement it and it would take a lot of memory if there is a lot of lines. Is there a way of doing this?
+1 Sirajus Salekin · April 28, 2015
Glad the solution have worked for you. 

Also, can you please move your 2nd question to another thread? It will get you more answers that way. Thank you. Once you do that, please send me a pm of the link so I can reply there, thank you.
+2 Peer Ligthart · April 30, 2015
You can try using:

+1 zhang yida · May 2, 2015
I also think it's easy with this:   Int32.Parse(string);
0 Sirajus Salekin · May 3, 2015
Yup, that's another way to do it.

But I think what the OP was trying to tell is that he isn't allowed to use any built in functions to do that task.
+1 Shawn Menning · May 11, 2015
This is a bit shorter and has worked for me in the past.

public static int ToInteger(this string s)
           int intergerValue= 0;

           if(int.TryParse(s, out intergerValue))
           return intergerValue;
               return 0;

if built in functions are not allowed at all though as Sirajus mentioned this will not be an option

grab the users input and pass it in as something like  userinput.ToInteger();  if it can't be converted to and int, it will return a 0
  • 1



C# is an object-oriented language designed for improving the development of web applications.