Logo

Programming-Idioms

History of Idiom 22 > diff from v40 to v41

Edit summary for version 41 by :
New Scala implementation by user [meshelton]

Version 40

2016-02-16, 21:28:08

Version 41

2016-02-18, 16:57:57

Idiom #22 Convert string to integer

Extract integer value i from its string representation s (in radix 10)

Idiom #22 Convert string to integer

Extract integer value i from its string representation s (in radix 10)

Code
I := Integer'Value (s);
Code
I := Integer'Value (s);
Code
let i = match s.parse::<i32>() {
  Ok(i) => i,
  Err(e) => {
    -1
  }
};
Code
let i = match s.parse::<i32>() {
  Ok(i) => i,
  Err(e) => {
    -1
  }
};
Comments bubble
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.
Comments bubble
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.
Imports
uses SysUtils;
Imports
uses SysUtils;
Code
var
  i: Integer;
 S: String;
begin
  S := '123';
  i := StrToInt(S);
end.
Code
var
  i: Integer;
 S: String;
begin
  S := '123';
  i := StrToInt(S);
end.
Comments bubble
The function StrToInt will raise an exception of type EConvertError if the string is not a proper representation of an integer.
Comments bubble
The function StrToInt will raise an exception of type EConvertError if the string is not a proper representation of an integer.
Code
let i: i32 = s.parse().unwrap_or(0);
Code
let i: i32 = s.parse().unwrap_or(0);
Comments bubble
This explicitly sets the value to 0 if it can't be parsed to an integer.
Comments bubble
This explicitly sets the value to 0 if it can't be parsed to an integer.
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20s%20%3D%20%2242%22%3B%0A%20%20%20%20let%20i%3A%20i32%20%3D%20s.parse().unwrap_or(0)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20i)%3B%0A%7D%0A&version=stable
Demo URL
https://play.rust-lang.org/?code=fn%20main()%20%7B%0A%20%20%20%20let%20s%20%3D%20%2242%22%3B%0A%20%20%20%20let%20i%3A%20i32%20%3D%20s.parse().unwrap_or(0)%3B%0A%20%20%20%20%0A%20%20%20%20println!(%22%7B%7D%22%2C%20i)%3B%0A%7D%0A&version=stable
Imports
import std.conv;
Imports
import std.conv;
Code
auto i = s.to!int;
Code
auto i = s.to!int;
Comments bubble
The useful to function can convert just about anything to something else. It will throw on a non-integral input string.
Comments bubble
The useful to function can convert just about anything to something else. It will throw on a non-integral input string.
Code
let i = s.parse::<i32>().unwrap();
Code
let i = s.parse::<i32>().unwrap();
Comments bubble
This panics if s is not a valid number.
s is parsed to 32-bits signed integer here (change number type if needed).
Comments bubble
This panics if s is not a valid number.
s is parsed to 32-bits signed integer here (change number type if needed).
Code
i = 0+str;
Code
i = 0+str;
Comments bubble
JavaScript automatically converts a string to a number when you use it in a numeric expression.
(This also works with floating point numbers.)
Comments bubble
JavaScript automatically converts a string to a number when you use it in a numeric expression.
(This also works with floating point numbers.)
Code
my $s = "12";
my $i = $s + 13;   # Foo is now 25
Code
my $s = "12";
my $i = $s + 13;   # Foo is now 25
Comments bubble
Perl automatically converts numbers to strings, and strings to numbers, whenever required.
Comments bubble
Perl automatically converts numbers to strings, and strings to numbers, whenever required.
Imports
#include <cstdlib>
Imports
#include <cstdlib>
Code
int i = std::atoi(s);
Code
int i = std::atoi(s);
Comments bubble
s is a char*
Comments bubble
s is a char*
Doc URL
http://en.cppreference.com/w/cpp/string/byte/atoi
Doc URL
http://en.cppreference.com/w/cpp/string/byte/atoi
Code
i = s.to_i
Code
i = s.to_i
Comments bubble
to_i returns 0 if s is not a valid number.
Comments bubble
to_i returns 0 if s is not a valid number.
Doc URL
http://ruby-doc.org/core-2.3.0/String.html#to_i-method
Doc URL
http://ruby-doc.org/core-2.3.0/String.html#to_i-method
Code
int i = new Integer( s ).intValue();
Code
int i = new Integer( s ).intValue();
Comments bubble
This will throw NumberFormatException if s does not contain a parsable integer
Comments bubble
This will throw NumberFormatException if s does not contain a parsable integer
Doc URL
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#Integer%28java.lang.String%29
Doc URL
https://docs.oracle.com/javase/7/docs/api/java/lang/Integer.html#Integer%28java.lang.String%29