Another solutions is to use int_of_string to see if it raises an exception:
let check_str s =
try int_of_string s |> ignore; true
with Failure _ -> false
If you are going to convert your string to an integer anyway, you can use that.
Beware, it will allow everything that OCaml's parser consider to be an integer
check_str "10";; //gives true
check_str "0b10";; //gives true, 0b11 = 2
check_str "0o10";; //gives true, 0o10 = 8
check_str "0x10";; //gives true, 0x10 = 16
So if you want to allow only decimal representation you can do:
let check_str s =
try (int_of_string s |> string_of_int) = s
with Failure _ -> false
as string_of_int
returns the string representation of an integer, in decimal.