简体   繁体   中英

How to make if String doesn't ends with in Kotlin

I had my own idea but it didn't work

String.text != String.text.toString().lastOrNull() in arrayOf('+', '-', '/', '*', '.')

With error

operator "!=' cannot be applied to 'CharSequence!' 'and Boolean'

Full:

if(operation && currentOpperand.text != currentOpperand.text.toString().lastOrNull() arrayOf('+', '-', '/', '*', '.')

(I want to make if Boolean "operation" == true and string doesn't ends with this array of chars print "+". (making calculator))

try this:

if (operation && currentOpperand.text.lastOrNull() !in arrayOf('+', '-', '/', '*', '.')){

}

or

if (operation && arrayOf('+', '-', '/', '*', '.').all { c -> !currentOpperand.text.endsWith(c) }){

}

or with regex :

if (operation && currentOpperand.text.matches(Regex(".*[^+-/*.]\$"))){
    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM