# Find the position of a single non-matching character between two strings

**URL:** https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560
**Category:** General Usage
**Tags:** strings, unicode
**Created:** [August 30, 2022, 5:07pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560 "2022-08-30T17:07:38Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 30, 2022, 7:24pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/21 "2022-08-30T19:24:46Z")

</div>

> [@stevengj](#):
>
> `const UTF8String = Union{String,SubString{String}}`

Would you mind a footnote on the above? 😓

---

<div class="post-metadata">

### Author: ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)
#### Post date: [August 30, 2022, 7:39pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/22 "2022-08-30T19:39:41Z")

</div>

> [@stevengj](#):
>
> What would the user do with a grapheme index?

Graphemes are what (end-)users care about in most cases. If the goal was to show “the filenames differ at position N” to an average, non-developer user, then N being in terms of graphemes would make the most intuitive sense. (For eg. I originally tested the code segments here with strings in my native language, and was myself confused for a moment at the result, until I counted how many codepoints were there in each of the graphemes upto the difference.)

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 30, 2022, 7:52pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/23 "2022-08-30T19:52:49Z")

</div>

> [@digital\_carver](#):
>
> If the goal was to show “the filenames differ at position N” to an average, non-developer user, then N being in terms of graphemes would make the most intuitive sense.

I agree, if it’s a non-programming interface (as opposed to, say, an exception string) where the user will never take the position and use it to index into a Julia string. (Though Julia 1.9 will include [grapheme slicing](https://github.com/JuliaLang/julia/pull/44266).)

(It might be reasonable to implement a `graphemeind(s, i)` function that returns the index of the grapheme containing `s[i]` in the `Unicode` stdlib. I haven’t seen that functionality in other languages, though, e.g. it doesn’t seem to be in the Python [`grapheme`](https://pypi.org/project/grapheme/) library.)

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [August 30, 2022, 8:00pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/24 "2022-08-30T20:00:02Z")

</div>

the intention was to provide a different proposal from the many already arrived.  
If I had tried to find a better performing solution it would have taken 50 times the time it took me to find this one.  
Regarding correctness, I point out that a “simpler” solution was required to the problem of finding the position of a SINGLE non-matching character, not the first of many non-matching characters.  
I used findfirst because a “simple” find did not come to mind (and I don’t know if it exists).  
But the same function works with findlast (or even findall), for the given problem.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [August 30, 2022, 8:02pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/25 "2022-08-30T20:02:38Z")

</div>

> [@rocco\_sprmnt21](#):
>
> Regarding correctness, I point out that a “simpler” solution was required to the problem of finding the position of a SINGLE non-matching character, not the first of many non-matching characters.

It also fails this test:

```julia
julia> s1, s2 = "zzzz", "zzzx"
("zzzz", "zzzx")

julia> findfirst(==(only(setdiff(s1,s2))), s1)
ERROR: ArgumentError: Collection is empty, must contain exactly 1 element

```

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [August 30, 2022, 8:03pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/26 "2022-08-30T20:03:48Z")

</div>

@rocco_sprmnt21, your proposal seems very simple and elegant. I was looking for a function to `subtract` strings, which doesn’t seem to exist, probably for a good reason… But your `setdiff` (nearly) does the trick, although slowly compared to the rest.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [August 30, 2022, 8:06pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/27 "2022-08-30T20:06:40Z")

</div>

it can be useful, but you have to be careful, as @stevengj shows, for corner cases.

---

<div class="post-metadata">

### Author: ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)
#### Post date: [August 30, 2022, 9:47pm UTC](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560/28 "2022-08-30T21:47:40Z")

</div>

if you like setdiff, here are some amended versions

```julia
# for ASCII strings
setdiff(enumerate(s1),enumerate(s2))

# for Unicode strings
first(setdiff(pairs(s1),pairs(s2)))

```

[Previous page](https://discourse.julialang.org/t/find-the-position-of-a-single-non-matching-character-between-two-strings/86560.md?page=1)
