gpt4 book ai didi

kotlin - 使用readLine通过用户输入搜索列表

转载 作者:行者123 更新时间:2023-12-02 13:38:10 24 4
gpt4 key购买 nike

我想搜索一个列表,并且仅使用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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com