How to find a substring or a char in an array of strings in Julia

Viewed 457

Whats a more efficient way to do the same thing that the next line of code is doing:

X = ["Please", "Thx", "Hello", "World"]
findfirst(k->occursin('H',k)==true,X)

So basically i'm trying to find the first element of the array X that has the uppercase letter H, so in the example the output is 3, but is there a more efficient way of doing this?

1 Answers

Unless I am missing something fundamental here, check out the timings below:


julia> @time findfirst(k->occursin('H',k),X)
  0.015461 seconds (11.20 k allocations: 689.455 KiB, 99.63% compilation time)
3

julia> @time findfirst(contains('H'),X)
  0.000008 seconds
3

Seems like the contains route is significantly more performant than occursin.

Related