Check if an array with strings and floating contains a string

Hello Everybody,

Let’s say I have a 2-element array containing one floating and a string, something like

2-element Array{Any,1}:
  "0 0"
 1

I want to check whether the array contains the string “0 0”. I tried isassigned(), but it does not work with strings. Do you have any suggestions?

Thank you for your time

Best

The following should work:

"0 0" in array
3 Likes

Or, in the case that you don’t know the exact string and want to know if there just is a String:

julia> String in typeof.(array)
true
2 Likes

Or any(isa.(array, String)) (which potentially does fewer type checks)

2 Likes

Actually, benchmarking it, my method has a slightly worse best case and a much worse worst case. But for a majority of cases it’s faster. So I guess I don’t know.

1 Like

Perhaps this is the fastest:

any( x -> isa(x,String), array )
4 Likes

It is indeed. By two orders of magnitude. I keep forgetting about that version of the any family of functions.

(also, looking forward to this, if it ever happens:

)

1 Like