如何检查字符串是否为空?
我目前正在使用==运营商:
julia> x = "";
julia> x == "";
true
Run Code Online (Sandbox Code Playgroud)
Dav*_*ela 10
使用isempty. 它更明确,更有可能针对其用例进行优化。
例如,在最新的 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
Run Code Online (Sandbox Code Playgroud)