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

标签 kotlin

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

相关文章:

java - Jackson 反序列化器失败时默认为 null

android - 类型不匹配 : inferred type is Context? 但需要上下文 - Kotlin

android - 数据绑定(bind) Kotlin Android 工作室错误

kotlin - Kotlin 是否允许对测试类使用反引号名称?

android - 某些设备上的对话框未填满整个窗口

android - Kotlin 对象加载延迟

Kotlin-非单例类的单个实例?

kotlin - 在声明挂起函数时传达预期的线程类型(IO、默认、主线程)

kotlin - 为什么协程首先在调用者线程上运行,但在第一个挂起点之后它在 “Dispatchers.Unconfined” 中的 DefaultExecutor 上运行?

Java/Kotlin Selenium Chromedriver 2Captcha