小编sup*_*ron的帖子

区分大小写Kotlin/ignoreCase

我试图忽略字符串的区分大小写.例如,用户可以放置"巴西"或"巴西",乐趣将触发.我该如何实现?我是Kotlin的新手.

fun questionFour() {
    val edittextCountry = findViewById<EditText>(R.id.editTextCountry)
    val answerEditText = edittextCountry.getText().toString()

    if (answerEditText == "Brazil") {
        correctAnswers++
    }

    if (answerEditText == "Brasil") {
        correctAnswers++
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

另一个人帮我写这样的.我现在关于这种方式的问题是"有更简洁的方式来写这个吗?"

fun questionFour() {
    val edittextCountry = findViewById<EditText>(R.id.editTextCountry)
    val answerEditText = edittextCountry.getText().toString()

    if (answerEditText.toLowerCase() == "Brazil".toLowerCase() || answerEditText.toLowerCase() == "Brasil".toLowerCase()) {
        correctAnswers++
    }
}
Run Code Online (Sandbox Code Playgroud)

回答

fun questionFour() {

        val edittextCountry = findViewById<EditText>(R.id.editTextCountry)
        val answerEditText = edittextCountry.getText().toString()

        if (answerEditText.equals("brasil", ignoreCase = true) || answerEditText.equals("brazil", ignoreCase = true)) {
            correctAnswers++
        }
    }
Run Code Online (Sandbox Code Playgroud)

case-insensitive kotlin

6
推荐指数
2
解决办法
4283
查看次数

标签 统计

case-insensitive ×1

kotlin ×1