To find the characters in the first string that are not present in the second string in Kotlin, you can use the subtract
method with sets.
In this example,
str1
and str2
with different values.toSet()
method.str2
from the set of characters in str1
using the subtract
method.str1
but not in str2
.// Find Characters Not Present in Second String
fun main() {
val str1 = "Hello World"
val str2 = "apple"
val diff = str1.toSet() - str2.toSet()
println("Characters not present in second string: $diff")
}
Characters not present in second string: [H, o, , W, r, d]
In this tutorial, we learned How to find the Characters in the First String that are not Present in the Second String in Kotlin language with well detailed examples.