How to test if a string is composed only of digits ?
One way is to convert it to an integer and if that fails then you'll know it's not an integer.
is_integer(S) ->
try
_ = list_to_integer(S),
true
catch error:badarg ->
false
end.
Or you can just check all of the digits, but you do have to check the edge case of an empty list:
is_integer("") ->
false;
is_integer(S) ->
lists:all(fun (D) -> D >= $0 andalso D =< $9 end, S).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With