Skip to content

Commit 707860c

Browse files
committed
Merge pull request #39 from huffman/string_to_int
Add to_i to String
2 parents cd04fdf + 97d1716 commit 707860c

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

lib/string.ex

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,11 @@ module String
285285
Erlang.binary_to_atom(self, 'utf8)
286286
end
287287

288+
% Returns the integer representation of this string if it is a valid integer.
289+
def to_i
290+
Erlang.list_to_integer(to_char_list)
291+
end
292+
288293
% Returns a copy of the original string with all lowercase letters replaced with their uppercase counterparts.
289294
def upcase
290295
Erlang.string.to_upper(to_char_list).to_bin

test/elixir/string_test.exs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ three
9797
"Hello" = "Hello".to_str
9898
end
9999

100+
def to_i_test
101+
0 = "0".to_i
102+
1 = "1".to_i
103+
93402 = "093402".to_i
104+
end
105+
100106
def inspect_test
101107
[34,102,92,110,111,34] = "f\no".inspect.to_char_list
102108

0 commit comments

Comments
 (0)