Следующий код представляет собой способ преобразования String в целое число с помощью рекурсии. Я понял процесс, но не могу понять, что происходит, когда мы это делаем:
str.charAt(0) - '0'
Мы можем легко преобразовать его в int или double с помощью parseInt или parseDouble. Тогда почему мы вычитаем символ '0' из String (который является числом)? Как это преобразует наш символ в int или double?
// Java implementation of the approach to convert a String to an Integer using Recursion
public class GFG {
// Recursive function to convert string to integer
static int stringToInt(String str)
{
// If the number represented as a string
// contains only a single digit
// then returns its value
if (str.length() == 1)
return (str.charAt(0) - '0');
// Recursive call for the sub-string
// starting at the second character
double y = stringToInt(str.substring(1));
// First digit of the number
double x = str.charAt(0) - '0';
// First digit multiplied by the
// appropriate power of 10 and then
// add the recursive result
// For example, xy = ((x * 10) + y)
x = x * Math.pow(10, str.length() - 1) + y;
return (int)(x);
}
// Driver code
public static void main(String[] args)
{
String str = "1235";
System.out.print(stringToInt(str));
}
}