android - Kotlin中字符串之间的异或操作
问题描述
我是 Kotlin 的新手,我想在字符串之间执行 XOR 操作。
我知道我可以通过将字符串转换为 char 数组并对每个字符执行 XOR来执行 Java
但是 Kotlin 中是否有任何可用的功能可以让我轻松做到这一点。
就像我有三个字符串 Y1、Y2 和 Y3
我想在它们之间执行 XOR 操作,比如
var result = Y1 XOR Y2 XOR Y3
我不知道如何使用 Kotlin 实现,任何人都可以帮助我,在此先感谢
解决方案
编写一个infix fun
来实现该 Java 函数。
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"
推荐阅读
- go - 为什么没有 sync.WaitGroup、sync.Mutex 引用类型,如通道、切片?
- java - 使用导致 NullPointerException 的字符串填充 ArrayDeques
- javascript - NPM 测试错误
- angular - “返回时超出最大调用堆栈大小”与“void 类型不存在订阅”冲突
- report - XLSX 报告应显示在 Odoo10 的单张纸中
- python - 从它们用于 islice 时的产量与产量
- python - Python - 机器学习:从数组列表创建训练和测试集
- python - 不知道如何无限循环这个
- node.js - 是什么导致我的 POST 请求失败(使用 Express/Angular 的 CORS)?
- php - jquery php mysql - 当所有项目显示时如何隐藏加载更多按钮