如果 String 的最后一个字符包含此数组 Kotlin
If last character of String cointains this array Kotlin
If
(某些字符串以 arrayOf(X, Y, Z)
中的字符结尾){
做...
}
尝试使用
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.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM)
Internal error:
正在寻找任何其他解决方案
尝试使用正则表达式
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))) {
//...
If
(某些字符串以 arrayOf(X, Y, Z)
中的字符结尾){
做...
}
尝试使用
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.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error:
正在寻找任何其他解决方案
尝试使用正则表达式
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))) {
//...