Converting a part of a String to an Integer

Hello there, let start with the obligatory code block:

1	var myString:String="15";
2	var secondDigit=myString[1];
3	print("the second digit of my String is "+secondDigit);
4	var secondDigitAsInt=parseInt(myString[1]);
5	print("10="+(5+secondDigitAsInt));

I have a string, take the second digit only. It works fine to concatenate to a string as in like 3, but if I want to use it as an integer via parseInt(), I get a 53 as a result. This was driving me nuts for hours because I didn’t have a clue where that 53 was coming from until it dawned on me: 53 is the ASCII code for “5”.

After a while of rolling on the floor, facepalming i’m back in the ranks of the sane people and would like to ask: is there a way to convert this char or whatever it is back to an integer i can calculate with the right way?

var myString : String = “15” ;
var secondDigit = myString.Substring(1) ;
var secondDigitAsInt = parseInt(secondDigit) ;

function Start(){
   print("10 = " + (5+secondDigitAsInt)) ;
}

Ok, just did some quick playing with this in Unity.
I use C# myself, so Java is not my strong point.

However, that said, try this…

   var myString:String="15";
   var secondDigit:String =myString[1].ToString();
   print("the second digit of my String is "+secondDigit);
   var secondDigitAsInt : int =int.Parse(secondDigit);
   print (secondDigitAsInt);
   print("10="+(5+secondDigitAsInt));

I believe it will give you the results you are looking for.

-Larry

I believe it will give you the results you are looking for.

-Larry