过滤字符串数组,包括“类似”字符串。条件 [英] Filter array of strings, including "like" condition

查看:188
本文介绍了过滤字符串数组,包括“类似”字符串。条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的主数组是 [Hello,Bye,Halo] ,我正在搜索lo ,它只会将数组过滤到 [Hello,Halo]

If my main array is ["Hello","Bye","Halo"], and I'm searching for "lo", it will filter the array only to ["Hello", "Halo"].

这就是我尝试过的:

 let matchingTerms = filter(catalogNames) {
        $0.rangeOfString(self.txtField.text!, options: .CaseInsensitiveSearch) !=  nil
    }

抛出

Type of expression is ambiguous without more context

有任何建议吗?

推荐答案

使用包含而不是:

let arr = ["Hello","Bye","Halo"]
let filtered = arr.filter { $0.contains("lo") }
print(filtered)

输出


[Hello,Halo]

["Hello", "Halo"]

感谢@ user3441734指出该功能当然只有avai当您导入基础

Thanks to @user3441734 for pointing out that functionality is of course only available when you import Foundation

这篇关于过滤字符串数组,包括“类似”字符串。条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆