- Home /
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()?
Answer by Eric5h5 · Dec 12, 2010 at 12:38 AM
parseInt (JS only), int.Parse (JS + C#), int.TryParse (JS + C#).
this is not in the reference files provided by Unity. Is this normally common knowledge? It certainly wasn't to me, but I'm new to script.
(For the record) This is part of the $$anonymous$$ono API: http://docs.go-mono.com/?link=T%3aSystem.Int32%2f* int is equivalent to Int32.
This is so confusing. Can anyone explain this to me?
Answer by drudiverse · Aug 26, 2014 at 09:38 AM
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[i];
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[i];
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[i];
r = 10 * r;
r = r + (int)char.GetNumericValue(letter);
}
return r;
}
}
Very good, drudiverse! I added an overload to char as well based on your code:
public static int IntParseFast(char value)
{
int result = 0;
result = 10 * result + (value - 48);
return result;
}
Answer by uhahaha · Dec 12, 2010 at 01:22 AM
Try:
var urInt: int = System.Int32.Parse(urText); print(urInt);
Answer by PointyPigheadGames · Oct 02, 2016 at 08:24 PM
@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).
Answer by thaiwas · May 07, 2018 at 04:22 PM
Remember to not try parsing string "0.1" with double quotes, or you get a silent crash
Your answer
Follow this Question
Related Questions
Error CS0029 fix 1 Answer
public string array controlled by an int in the inspector 1 Answer
PlayerPrefs Int to Float 1 Answer
Can I create a list with an int/float and a string? C# 2 Answers
Convert String to int 1 Answer