To find the characters in the first string that are not present in the second string in Swift, you can use sets and filter.
In this example,
str1
and str2
with different values.Set
type.str1
set that are not present in str2
set.// Find Characters Not Present in Second String
let str1 = "Hello World"
let str2 = "apple"
let set1 = Set(str1)
let set2 = Set(str2)
let diff = set1.subtracting(set2)
let result = String(diff)
print("Characters not present in second string: \(result)")
Characters not present in second string: Ho Word
In this tutorial, we learned How to find the Characters in the First String that are not Present in the Second String in Swift language with well detailed examples.