首页 > 解决方案 > 如何检查字符串是否为空?

问题描述

如何检查字符串是否为空?

我目前正在使用==运营商:

julia> x = "";

julia> x == "";
true

标签: julia

解决方案


使用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

推荐阅读