# The correct use of findfirst

**URL:** <https://discourse.julialang.org/t/the-correct-use-of-findfirst/22351>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [March 26, 2019, 12:59pm UTC](https://discourse.julialang.org/t/the-correct-use-of-findfirst/22351 "2019-03-26T12:59:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [March 26, 2019, 12:59pm UTC](https://discourse.julialang.org/t/the-correct-use-of-findfirst/22351/1 "2019-03-26T12:59:44Z")

</div>

DOCS\> “You can search for the index of a particular character  
using the findfirst and findlast functions.”

```
julia> findfirst(isequal('('), "André(m, n)")
7

julia> findfirst(isequal('('), "Andre(m, n)")
6

```

It seems clear that this can lead to mistakes.  
What’s the way out?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 26, 2019, 1:14pm UTC](https://discourse.julialang.org/t/the-correct-use-of-findfirst/22351/2 "2019-03-26T13:14:26Z")

</div>

In this case, `findfirst` is giving you precisely the index you would use to extract that character:

```julia
julia> s = "André(m, n)"
"André(m, n)"

julia> s[7]
'(': ASCII/Unicode U+0028 (category Ps: Punctuation, open)

```

Is your actual goal something other than this? Do you want to count the number of letters that occur before the `(` character?

---

<div class="post-metadata">

**Author:** ![leiteiro](https://avatars.discourse-cdn.com/v4/letter/l/b5e925/32.png) [@leiteiro](https://discourse.julialang.org/u/leiteiro)\
**Post date:** [March 26, 2019, 2:52pm UTC](https://discourse.julialang.org/t/the-correct-use-of-findfirst/22351/3 "2019-03-26T14:52:01Z")

</div>

> Is your actual goal something other than this?

For example consider:

```
function extractName(s::String)::String
    i = findfirst(isequal('('), s)
    return s[1:(i-1)]
end

```

Here I expect

```
extractName("Andre(m, n)") |> println

```

to return “Andre”, which is what it does. Similarly I expect

```
extractName("André(m, n)") |> println

```

to return “André” but I get

```
ERROR: LoadError: StringIndexError("André(m, n)", 6)

```

---

<div class="post-metadata">

**Author:** ![kristoffer.carlsson](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kristoffer.carlsson/32/22_2.png) [@kristoffer.carlsson](https://discourse.julialang.org/u/kristoffer.carlsson)\
**Post date:** [March 26, 2019, 2:54pm UTC](https://discourse.julialang.org/t/the-correct-use-of-findfirst/22351/4 "2019-03-26T14:54:08Z")

</div>

Have you read through [Strings · The Julia Language](https://docs.julialang.org/en/v1/manual/strings/index.html)?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [March 26, 2019, 3:01pm UTC](https://discourse.julialang.org/t/the-correct-use-of-findfirst/22351/5 "2019-03-26T15:01:57Z")

</div>

Not all indices into a string are valid, since some indices might not lie exactly at the start of a character (and some characters span multiple bytes). Instead, you can use `prevind` to extract the previous _valid_ index:

```julia
julia> s = "André(m, n)"
"André(m, n)"

julia> i = findfirst(isequal('('), s)
7

julia> s[1:prevind(s, i)]
"André"

```

Although in this case a regular expression might be more appropriate:

```julia
julia> m = match(r"(.*)\(", s)
RegexMatch("André(", 1="André")

julia> m.captures[1]
"André"

```
