Logo

Programming-Idioms

History of Idiom 22 > diff from v21 to v22

Edit summary for version 22 by :

Version 21

2015-10-25, 19:06:50

Version 22

2015-10-29, 14:05:12

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
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 extremely useful 'to' function can convert just about anything to something else. It will throw on a non-integral input string.
Comments bubble
the extremely useful 'to' function can convert just about anything to something else. It will throw on a non-integral input string.
Code
print "Perl automatically converts numbers, like ", 7, "to strings when needed\n";

my $s = "12";
my $i = $s + 13;   # Foo is now 25
Code
print "Perl automatically converts numbers, like ", 7, "to strings when needed\n";

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
import "strconv"
Imports
import "strconv"
Code
i, err  := strconv.ParseInt(s, 10, 0)
Code
i, err  := strconv.ParseInt(s, 10, 0)
Comments bubble
Radix 10. Third argument 0 means "fit in implementation-specific int". But result type is always int64.
Don't ignore the potential error err !
Comments bubble
Radix 10. Third argument 0 means "fit in implementation-specific int". But result type is always int64.
Don't ignore the potential error err !
Doc URL
http://golang.org/pkg/strconv/#ParseInt
Doc URL
http://golang.org/pkg/strconv/#ParseInt
Imports
#include <stdlib.h>
Imports
#include <stdlib.h>
Code
int i = atoi(s);
Code
int i = atoi(s);
Comments bubble
s is a char*
Comments bubble
s is a char*
Imports
import "strconv"
Imports
import "strconv"
Code
i, err  := strconv.Atoi(s) 
Code
i, err  := strconv.Atoi(s) 
Comments bubble
Atoi(s) is shorthand for ParseInt(s, 10, 0). It yields type int.
Comments bubble
Atoi(s) is shorthand for ParseInt(s, 10, 0). It yields type int.
Doc URL
http://golang.org/pkg/strconv/#Atoi
Doc URL
http://golang.org/pkg/strconv/#Atoi
Demo URL
http://play.golang.org/p/VXjGHZbj6J
Demo URL
http://play.golang.org/p/VXjGHZbj6J
Code
i = s.to_i
Code
i = s.to_i
Comments bubble
"123".to_i returns 123
"abc".to_i returns 0
Comments bubble
"123".to_i returns 123
"abc".to_i returns 0
Code
int i = Integer.parseInt( s );
Code
int i = Integer.parseInt( s );
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