Question: I have two strings.

```
str_a = "the_quick_brown_fox"
str_b = "the_quick_red_fox"

```

I want to find the first index at which the two strings differ (i.e. `str_a[i] != str_b[i]`).

I know I could solve this with something like the following:

```
def diff_char_index(str_a, str_b)
  arr_a, arr_b = str_a.split(""), str_b.split("")
  return -1 unless valid_string?(str_a) && valid_string?(str_b)
  arr_a.each_index do |i|
    return i unless arr_a[i] == arr_b[i]
  end
end

def valid_string?(str)
  return false unless str.is_a?(String)
  return false unless str.size > 0
  true
end

diff_char_index(str_a, str_b) # => 10

```

Is there a better way to do this?

Answer: 
```
i = 0
i += 1 while str_a[i] and str_a[i] == str_b[i]
i

```
This uses a binary search to find the index where a slice of `str_a` no longer occurs at the beginning of `str_b`:

```
(0..str_a.length).bsearch { |i| str_b.rindex(str_a[0..i]) != 0 }

```