Is a character a member of an vector

the subject line says it all. I can laboriously loop through the array. However, I am wondering if there is not a function which will do this more elegantly.

I don’t understand the subject line, just looking at it I’m imagining Vector{Char} but you’re saying you don’t want to loop through an array but you want a function that does? So map, broadcasting, or other such higher order functions? I don’t understand that either because you don’t really save much effort there either, you’d still have to write out what each iteration does.

julia> in('b', 'a':'f')

julia> in('z', 'a':'f')

Any other haystack of Char would work in place of the 'a':'f' I used. That said, unless it can exploit some particular organization (like that the haystack is a range, like I used here), it will just do the looping you mention internally. If your haystack is sorted, you can at least consider the insorted function.


This is not different from mikmoore’s solution, but in the REPL you can use the \in tab complete to write the same function as an infix operator (i.e. you type “\in<tab>” and the REPL transforms that text to “∈”):

In [1]: 'e' ∈ "needle"
Out[1]: true

Frankly, one of my favorite features of the REPL.


For completeness, you can also just use in as an infix operator:

'e' in "needle"

The subject line was possibly (very) badly written, possibly because I had not thought through what I wanted to do. Be that as it may, the question was answered. Thank you to all.