如果字符串的最后一个字符包含此数组Kotlin



If(某些字符串以arrayOf(X, Y, Z)中的字符结尾({
do…

尝试使用

var test = "Some string Z"
if (test.isNotEmpty() && test.last() in arrayOf('X', 'Y', 'Z')) //check if the last char == 'X' || 'Y' || 'Z'
{
test = test.dropLast(1) + 'A' // if yes replace with `A`
}

但android给出了一个编译错误:

e:org.getbrains.kotlin.codegen.CompilationException:后端(JVM(内部错误:

搜索任何其他解决方案

尝试使用regex

var test = "Some string Z"
val array = arrayOf('X', 'Y', 'Z')
if (test.matches(".*(${array.joinToString("|")})$".toRegex())) {
test = test.dropLast(1) + 'A'
}

忽略区分大小写的奖励

//...
val array = arrayOf('x', 'Y', 'z')
if (test.matches(".*(${array.joinToString("|")})$".toRegex(RegexOption.IGNORE_CASE))) {
//...