Logo

Programming-Idioms

  • C++
  • Java
int i = new Integer(s).intValue();

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

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)
#include <cstdlib>
int i = std::atoi(s);

s is a char*
#include <string>
int i = std::stoi(s);
std::string s("123");
int i;
std::from_chars(s.data(), s.data() + s.size(), i, 10);
#include <string>
#include <sstream>
std::stringstream ss(str);
int i;
ss >> i;
I : Integer := Integer'Value (s);

New implementation...