我想搜索一个列表,并且仅使用readLine返回与查询的输入匹配的条目。以下代码仅在用户键入列表中的全名时才有效,而我希望他们能够输入's'并检索所有带有字母s的名称。我应该改用for循环吗?
fun main(args: Array<String>) {
var names = arrayListOf(
"Nathan",
"Gerald",
"Winchester",
"Razi",
"Tetsuo",
"Bob",
"Ryandick",
"Jacafar",
"Palamov",
"Zaxon")
print("Enter search query: ")
val terms:String? = readLine()!!.toString()
val output = names.filter{it == terms}
println(output)
}
最佳答案
只需更改过滤条件:
val output = names.filter { it.contains(terms) }
您可以执行不区分大小写的匹配:
val output = names.filter { it.contains(terms, true) }
关于kotlin - 使用readLine通过用户输入搜索列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51586160/