# Find all indexes of string in string array

**URL:** <https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302>\
**Category:** General Usage\
**Created:** [March 21, 2020, 3:29pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302 "2020-03-21T15:29:32Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 21, 2020, 3:29pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/1 "2020-03-21T15:29:33Z")

</div>

Hello,

Can someone help me with this? It seems like it should be easy but I can’t quite figure it out.

I’m trying to find all of the array indicies in this String array that matches the search string

Something like the following

```julia
data = ["abc","bcd","def","GHF"]
findall( x -> x == "b", data)

```

I was hoping to get that this string is found in the array at indexes 1,2 but it returns nothing. Is there some syntax that I’m not getting right here?

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 21, 2020, 4:13pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/2 "2020-03-21T16:13:52Z")

</div>

If you want to find all array items which contains some substring, then you should either use `occursin` or as a more general solution regexps.

```julia
data = ["abc","bcd","def","GHF"]
findall( x -> occursin("b", x), data)

# 2-element Array{Int64,1}:
# 1
# 2

```

If you want to find where this char can be found, you can add broadcast

```julia
findall.( x -> x .== 'b', data)

# 4-element Array{Array{Int64,1},1}:
# [2]
# [1]
# [] 
# [] 

```

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 21, 2020, 4:18pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/3 "2020-03-21T16:18:37Z")

</div>

Thanks, this works!

I tried to look through the online documentation, but this solution wasn’t clear from there.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 21, 2020, 4:24pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/4 "2020-03-21T16:24:20Z")

</div>

You are welcome.  
Just in case, if you want to find all substring indices in each element of the String array, it can be done even easier

```julia
findall.("b", data)

# 4-element Array{Array{UnitRange{Int64},1},1}:
# [2:2]
# [1:1]
# []   
# []   

```

This will give you all possible ranges, where substring is located.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 21, 2020, 5:14pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/5 "2020-03-21T17:14:02Z")

</div>

I was trying different variants and I was expecting this to return only index 2, but it doesn’t. Is this the right usage?

```julia
julia> findall(x -> startswith("b",x), data)
0-element Array{Int64,1}

```

---

<div class="post-metadata">

**Author:** ![AndiMD](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andimd/32/6531_2.png) [@AndiMD](https://discourse.julialang.org/u/AndiMD)\
**Post date:** [March 21, 2020, 6:14pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/6 "2020-03-21T18:14:16Z")

</div>

The prefix is the second argument:

```julia
findall(x -> startswith(x,"b"), data)

```

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 21, 2020, 6:31pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/7 "2020-03-21T18:31:39Z")

</div>

Thanks.

I’m sure there is some logical reason, but anyone know why with occursin() the search term is the first argument and with startswith(), the search term is the second argument? Doesn’t that make it hard to remember?

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 21, 2020, 6:35pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/8 "2020-03-21T18:35:51Z")

</div>

Yes, I agree that it is not ideal. There is a github issue that addresses this exact point: [https://github.com/JuliaLang/julia/issues/35031](https://github.com/JuliaLang/julia/issues/35031)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 21, 2020, 6:42pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/9 "2020-03-21T18:42:14Z")

</div>

> [@jonjilla](#):
>
> I tried to look through the online documentation, but this solution wasn’t clear from there.

Well, the documentation cannot address every possible use case. But you can reason about your code, and think about what it’s doing, because the behavior you saw _does_ make sense.

> [@jonjilla](#):
>
> ```julia
> data = ["abc","bcd","def","GHF"]
> findall( x -> x == "b", data)
> 
> ```

For every element in `data` it checks, is “abc” equal to “b”, is “bcd” equal to “b”, etc. And of course the answer each time is ‘no’.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 21, 2020, 6:50pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/10 "2020-03-21T18:50:42Z")

</div>

Yes, I agree that in retrospect, that makes sense and I should have been able to figure it out, but just using logic or intuition hasn’t been that useful to figure out the syntax in Julia because if I were to do that, I think the following would make way more sense

```julia
findall( "b", occursin(data))

or more simply

occursin("b", data)

```

rather than

```julia
findall( x -> occursion("b", x), data)

```

But, either way, it’s still better than Python so I’ll keep trying 🙂

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [March 21, 2020, 7:19pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/11 "2020-03-21T19:19:19Z")

</div>

You can use vectorized version of `occursin`, and avoid creation of intermediate function

```julia
occursin.("b", data)

```

which gives you Boolean vector. By adding `findall` you convert boolean values to indices

```julia
findall(occursin.("b", data))

```

The same applies to `startswith` and all other functions. You can read more about dot syntax and broadcasting in [Functions · The Julia Language](https://docs.julialang.org/en/v1/manual/functions/#man-vectorized-1)

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [March 21, 2020, 7:25pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/12 "2020-03-21T19:25:28Z")

</div>

> [@jonjilla](#):
>
> findall( “b”, occursin(data))  
> occursin(“b”, data)

Well, I respectfully disagree 😉 I don’t think these make sense at all. The first one is unclear, but the second obviously checks if `"b"` is one of the elements of `data`, which it isn’t. I mean, what else _could_ it mean?

`occursin` surely has to to work on _each element of_ `data`, not on the `data` as a whole.

---

<div class="post-metadata">

**Author:** ![jonjilla](https://avatars.discourse-cdn.com/v4/letter/j/7ba0ec/32.png) [@jonjilla](https://discourse.julialang.org/u/jonjilla)\
**Post date:** [March 21, 2020, 11:05pm UTC](https://discourse.julialang.org/t/find-all-indexes-of-string-in-string-array/36302/13 "2020-03-21T23:05:01Z")

</div>

OK, now this syntax does make sense to me. It’s clear and simple…

```julia
findall(occursin.("b", data))

```

Thanks for the insight. I’ll have to study those vectorizations in more detail.
