XOR operation between String in Kotlin

Viewed 1024

I am new in Kotlin and I want to perform XOR operation between Strings.

I know that I can perform like Java by converting string to char array and perform XOR on each character

But is there any function available in Kotlin by which I can do it easily.

Like I have Three String Y1, Y2 and Y3

I want to perform XOR operation between them like

 var result = Y1 XOR Y2 XOR Y3

I am not getting how can I achive with Kotlin, can anyone help me, thanks in advance

1 Answers

Write an infix fun to implement that Java function.

infix fun String.xor(that: String) = mapIndexed { index, c ->
    that[index].toInt().xor(c.toInt())
}.joinToString(separator = "") {
    it.toChar().toString()
}

"102" xor "103" xor "104" // "105"
Related