本文介绍了区分大小写Kotlin/ignoreCase的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图忽略字符串的区分大小写.例如,用户可以放置巴西"或巴西",然后就会触发乐趣.我该如何执行呢?我是Kotlin的新手.
I am trying to ignore case sensitivity on a string. For example, a user can put "Brazil" or "brasil" and the fun will trigger. How do I implement this? I am new to Kotlin.
fun questionFour() {
val edittextCountry = findViewById<EditText>(R.id.editTextCountry)
val answerEditText = edittextCountry.getText().toString()
if (answerEditText == "Brazil") {
correctAnswers++
}
if (answerEditText == "Brasil") {
correctAnswers++
}
}
编辑
另一个人帮我这样写.我现在对这种方式的疑问是是否有一种更干净的方式编写这种方式?"
EDIT
Another person helped me write like this. My question now about this way is "Is there a cleaner way to write this?"
fun questionFour() {
val edittextCountry = findViewById<EditText>(R.id.editTextCountry)
val answerEditText = edittextCountry.getText().toString()
if (answerEditText.toLowerCase() == "Brazil".toLowerCase() || answerEditText.toLowerCase() == "Brasil".toLowerCase()) {
correctAnswers++
}
}
答案
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++
}
}
推荐答案
您可以调用 equals
函数直接运行,它将允许您指定可选参数ignoreCase
:
if (answerEditText.equals("brasil", ignoreCase = true)) {
correctAnswers++
}
这篇关于区分大小写Kotlin/ignoreCase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!