# Is a character a member of an vector

**URL:** https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634
**Category:** New to Julia
**Created:** [October 5, 2023, 9:44pm UTC](https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634 "2023-10-05T21:44:41Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![neophytedave](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neophytedave/32/202273_2.png) [@neophytedave](https://discourse.julialang.org/u/neophytedave)
#### Post date: [October 5, 2023, 9:44pm UTC](https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634/1 "2023-10-05T21:44:41Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)
#### Post date: [October 5, 2023, 9:53pm UTC](https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634/2 "2023-10-05T21:53:22Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![mikmoore](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mikmoore/32/31109_2.png) [@mikmoore](https://discourse.julialang.org/u/mikmoore)
#### Post date: [October 5, 2023, 10:06pm UTC](https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634/3 "2023-10-05T22:06:26Z")

</div>

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

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

```

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.

---

<div class="post-metadata">

### Author: ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)
#### Post date: [October 6, 2023, 1:06pm UTC](https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634/4 "2023-10-06T13:06:01Z")

</div>

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 “∈”):

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

```

Frankly, one of my favorite features of the REPL.

---

<div class="post-metadata">

### Author: ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)
#### Post date: [October 6, 2023, 2:14pm UTC](https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634/5 "2023-10-06T14:14:45Z")

</div>

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

```julia
'e' in "needle"

```

---

<div class="post-metadata">

### Author: ![neophytedave](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/neophytedave/32/202273_2.png) [@neophytedave](https://discourse.julialang.org/u/neophytedave)
#### Post date: [October 25, 2023, 8:14pm UTC](https://discourse.julialang.org/t/is-a-character-a-member-of-an-vector/104634/6 "2023-10-25T20:14:38Z")

</div>

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.
