You normally use a regular expression to check if a string is a number:
julia> re = r"^[+-]?([0-9]+([.][0-9]*)?|[.][0-9]+)$";
julia> occursin(re,"123.")
true
julia> occursin(re,"123.0")
true
julia> occursin(re,"123.012")
true
julia> occursin(re,"123")
true
julia> occursin(re,"ab")
false
julia> occursin(re,"ab123.1")
false
julia> occursin(re,"123.1e")
false
Note: I have used the regular expression found at Regular expression for floating point numbers If you just want to have an integer part or include the exponent, such ready regular expressions are also easy to find.
EDIT: Benchmark test.
Let us consider the following function to check whether a String is a number:
function check_str(a)
try
parse(Float64,a)
true
catch
false
end
end
Here are the benchmark tests. Note that the regular expression is roughly 200x faster (the increase would be smaller if we decided to look also for the exponent part) and does not allocate.
julia> using BenchmarkTools
julia> @btime check_str("60.0a")
15.359 μs (18 allocations: 816 bytes)
false
julia> @btime occursin($re,"60.0a")
67.023 ns (0 allocations: 0 bytes)
false
When the String is successfully parsed the speed gap is much smaller:
julia> @btime check_str("60.0")
298.833 ns (0 allocations: 0 bytes)
true
julia> @btime occursin($re,"60.0")
58.865 ns (0 allocations: 0 bytes)
true