gpt4 book ai didi

android - 如何在 .endsWith() Kotlin 中添加多个字符

转载 作者:行者123 更新时间:2023-12-02 12:17:25 25 4
gpt4 key购买 nike

我的代码中有一个表达式 when,它执行以下操作:

when{
currentOpperand.text.toString().endsWith("+") ||
currentOpperand.text.toString().endsWith("-") ||
currentOpperand.text.toString().endsWith("÷") ||
currentOpperand.text.toString().endsWith("×") ||
currentOpperand.text.toString().endsWith(".") ||
currentOpperand.text.toString().endsWith("")
->{
currentOpperand.text = currentOpperand.text.toString() + ""
}

我想让这段代码更短,比如:

when{
currentOpperand.text.toString().endsWith("+", "-", "÷", "×", ".", "") ->{
currentOpperand.text = currentOpperand.text.toString() + ""
}

但是 android studio 给我一个错误:

None of the following functions can be called with the argument supplied

如果可能的话,请评论另一种方法来做这些事情,比如:lastIndexOfAny 或等等。(我想以任何可能的方式在代码中以最短的结果执行此操作)

最佳答案

你可以这样做:

var doesExist = currentOpperand.text.last().toString() in arrayOf("+", "-", "÷", "×", ".", "")

when{
doesExist ->{
currentOpperand.text = currentOpperand.text.toString() + ""
}
}

但问题是: currentOpperand.text = currentOpperand.text.toString() + "" 是多余的,没有用。你为什么要这样做?

关于android - 如何在 .endsWith() Kotlin 中添加多个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63667699/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com