Reputation: 6956
How can I check if a string is empty?
I am currently using the ==
operator:
julia> x = "";
julia> x == "";
true
Upvotes: 7
Views: 2668
Reputation: 6956
Use isempty
. It is more explicit and more likely to be optimized for its use case.
For example, on the latest Julia:
julia> using BenchmarkTools
julia> myisempty(x::String) = x == ""
foo (generic function with 1 method)
julia> @btime myisempty("")
2.732 ns (0 allocations: 0 bytes)
true
julia> @btime myisempty("bar")
3.001 ns (0 allocations: 0 bytes)
false
julia> @btime isempty("")
1.694 ns (0 allocations: 0 bytes)
true
julia> @btime isempty("bar")
1.594 ns (0 allocations: 0 bytes)
false
Upvotes: 14