Logo

Programming-Idioms

  • JS
  • Kotlin
  • Rust
  • Java
  • Python
  • Groovy
Integer i = s.toInteger()

While all the Java examples work in Groovy, too, you can also use the toInteger() method that Groovy adds to all CharSequences. It throws a NumberFormatException if the string is not a number.
const i = +s
let i = parseInt(s, 10)

parseInt(string, radix);
The radix is an integer between 2 and 36.
const i = Number(s);
val i = s.toIntOrNull()

i is null if s can not be converted to an integer
val i = s.toInt()
let i = s.parse::<i32>().unwrap();

This panics if s is not a valid number.
s is parsed to 32-bits signed integer here (change number type if needed).
let i: i32 = s.parse().unwrap_or(0);

This explicitly sets the value to 0 if it can't be parsed to an integer.
let i = match s.parse::<i32>() {
  Ok(i) => i,
  Err(_e) => -1,
};

s is parsed to 32-bits signed integer here (change number type if needed).
-1 is used here as a fallback value, but any error handling instructions can be used.
int i = Integer.parseInt(s);

Throws NumberFormatException if s does not contain a parsable integer
int i = new Integer(s).intValue();

Throws NumberFormatException if s does not contain a parsable integer
int i = s.chars()
     .map(x -> x - '0')
     .reduce((a, b) -> (a * 10) + b)
     .getAsInt();
Integer i = Integer.valueOf(s, 10);

The second argument in the valueOf method denotes the radix (or base)
i = int(s)
I : Integer := Integer'Value (s);

New implementation...