NSPredicate过滤器数组使用分隔符之前的所有字符

时间:2016-01-07 18:16:02

标签: ios swift nspredicate

我目前正在使用

进行过滤
self.searchArray.removeAll(keepCapacity: false)
let searchPredicate = NSPredicate(format: "SELF CONTAINS[c] %@", searchController.searchBar.text!)
let array = (Array(exampleArray) as NSArray).filteredArrayUsingPredicate(searchPredicate)
self.searchArray = array as! [String]

它过滤整个字符串,但是,我只想使用在分隔符之前存在的所有字符进行过滤。例如:

每个数组值都有一个分隔符,即“$%^”

示例数组包含[abc $%^ 12],[efg $%^ 32],[tyh $%^ 77]

我只希望过滤包含在$%^之前的所有字符,即abc,efg和tyh

2 个答案:

答案 0 :(得分:1)

您可以在不使用NSPredicate的情况下执行此操作。 Stub

let searchBarText = "h"
let exampleArray = ["abc$%^12", "efg$%^32", "tyh$%^77"]

let searchResults = exampleArray.filter {
    let components = $0.componentsSeparatedByString("$%^")
    return components[0].containsString(searchBarText)
}

答案 1 :(得分:0)

'纯'迅捷解决方案:-)(我不喜欢基金会的任何帮助)

let arr = ["abc$%^12", "efg$%^32", "tyh$%^77", "nodelemiter"]
let delimiter = "$%^"

func splitByDelimeter(let str: String, delimiter: String)->(String,String) {
    var str1 = ""
    var str2 = str
    while !str2.hasPrefix(delimiter) && str2.characters.count > 0 {
        str1.append(str2.removeAtIndex(str2.startIndex))
    }
    if (str1 == str) { return (str1, "") }
    let r = Range(start: str2.startIndex, end: str2.startIndex.advancedBy(delimiter.characters.count))
    str2.removeRange(r)
    return (str1, str2)
}

let res = arr.map { splitByDelimeter($0, delimiter: delimiter).0 }
print(res)  // ["abc", "efg", "tyh", "nodelemiter"]

let res2 = arr.map { splitByDelimeter($0, delimiter: delimiter) }
print(res2) // [("abc", "12"), ("efg", "32"), ("tyh", "77"), ("nodelemiter", "")]

它应该可以工作,即使只有分隔符,或者字符串中有多个分隔符。首次出现的分隔符会将字符串拆分为两部分。

更新

在String扩展的帮助下,您可以将其作为...

let arr = ["abc$%^12", "efg$%^32", "tyh$%^77", "nodelemiter", "$%^jhdk$%^jdhjahsd", "22lemar$%^fralemdo"]

extension String {

    func splitBy(delimiter: String)->(String,String) {
        var str1 = ""
        var str2 = self
        while !str2.hasPrefix(delimiter) && str2.characters.count > 0 {
            str1.append(str2.removeAtIndex(str2.startIndex))
        }
        if (str1 == self) { return (str1, "") }
        let r = Range(start: str2.startIndex, end: str2.startIndex.advancedBy(delimiter.characters.count))
        str2.removeRange(r)
        return (str1, str2)
    }

    func contains(string: String)->Bool {
        guard !self.isEmpty else {
            return false
        }
        var s = self.characters.map{ $0 }
        let c = string.characters.map{ $0 }
        repeat {
            if s.startsWith(c){
                return true
            } else {
                s.removeFirst()
            }
        } while s.count > c.count - 1
        return false
    }
}

let filtered = arr.map{ $0.splitBy("$%^").0 }.filter { $0.contains("lem") }
print(filtered) // ["nodelemiter", "22lemar"]

或者您可以使用hasPrefix通过在搜索框中键入更多字符来缩小搜索范围......等等。

let arr2 = ["abc$%^12", "abefg$%^32", "tyhab$%^77"]
let filtered2 = arr2.map{ $0.splitBy("$%^").0 }.filter { $0.hasPrefix("ab") }
print(filtered2) // ["abc", "abefg"]