# How to convert a String to an Int?

 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 ``System.Convert.ToInt32(string);``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 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){                if(x.Equals("0")){                    strInt.Add(0);                }                else if (x.Equals("1")) {                    strInt.Add(1);                }                else if (x.Equals("2"))                {                    strInt.Add(2);                }                else if (x.Equals("3"))                {                    strInt.Add(3);                }                else if (x.Equals("4"))                {                    strInt.Add(4);                }                else if (x.Equals("5"))                {                    strInt.Add(5);                }                else if (x.Equals("6"))                {                    strInt.Add(6);                }                else if (x.Equals("7"))                {                    strInt.Add(7);                }                else if (x.Equals("8"))                {                    strInt.Add(8);                }                else if (x.Equals("9"))                {                    strInt.Add(9);                }            }            #endregion        }``

## Replies

 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;                index++;            }                        while( index < userInput.length() ){                number *= 10;                number += userInput[index++] - '0';                                     }                        if(isNegetive){                number = -number;            }                        return number;        } catch (Exception e) {        } return 0;}`` 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? 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. Peer Ligthart · April 30, 2015 You can try using:Int32.Parse(string); zhang yida · May 2, 2015 I also think it's easy with this:   Int32.Parse(string); 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. 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;           }           else               return 0;       }if built in functions are not allowed at all though as Sirajus mentioned this will not be an optiongrab 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#

118,333 followers