Swift get the different characters in two strings

You should probably update the question to specify if you are looking for characters that do not exist in the other string or if you actually want to know if the strings are different and starting at which index.

To get a list of unique characters that only exist on one of the strings, you can do a set operation like this:

let x = "A B C"
let y = "A b C"
let setX = Set(x.characters)
let setY = Set(y.characters)
let diff = setX.union(setY).subtract(setX.intersect(setY)) // ["b", "B"]

If you want to know the index where the strings begin to differ, do a loop over the characters and compare the strings index by index.


UPDATE for Swift 4.0 or greater

Because of the change of String to also be a collection, this answer can be shortened to

let difference = zip(x, y).filter{ $0 != $1 }

For Swift version 3.*

let difference = zip(x.characters, y.characters).filter{$0 != $1}

enter image description here