How to turn a String to an Int?

I need to change a string into an integer. I know there is ToString() but is there something like ToInt()?

parseInt (JS only), int.Parse (JS + C#), int.TryParse (JS + C#).

if you want to go very fast and don’t have hyphens and spaces and returns in string space:

int.Parse("400")     123.07 ns
IntParseFast("400")    2.87 ns

in .js it’s simply this function to parse 50x faster:
//js:

function  IntParseFast( value : String) : int
{
	var result : int = 0;
	for (var i = 0; i < value.Length; i++)
	{
	    var letter : char = value*;*

_ result = 10 * result + char.GetNumericValue(letter);_

  • }*
  • return result;*
    }
    this is the original CS code:
    public static int IntParseFast(string value)
    {
  • int result = 0;*
  • for (int i = 0; i < value.Length; i++)*
  • {*
    _ char letter = value*;_
    _ result = 10 * result + (letter - 48);_
    _
    }_
    _
    return result;_
    _
    }*_

probably have to use an extension:
using UnityEngine;
public static class Extns
* {*
* public static int ParseFast( this string s )*
* {*
* int r = 0;*
* for (var i = 0; i < s.Length; i++)*
* {*
_ char letter = s*;
r = 10 * r;
r = r + (int)char.GetNumericValue(letter);
}
return r;
}
}*_

Try:

var urInt: int = System.Int32.Parse(urText); print(urInt);

@betobeast1246 So basicly what this code does is convert a string (alphanumeric) to an integer (numeric). parseInt only works with Javascript but int.Parse works with C# and Javascript. Here is some example code:

function Submit () {
	answerInt = int.Parse(answer);
}

So basically answerInt is your int variable and answer is your string. An example of this would be if you are making a calculator app and you wanted to subtract the contents of one input field from another. By default, the input field is stored as a string and using this code would convert it to an int, which can be added/subtracted. If you want decimals in your answer, use float instead. (float is like an int, but more precise).

Remember to not try parsing string “0.1” with double quotes, or you get a silent crash