swift return array from another array with contain string

拜拜、爱过 提交于 2019-12-11 19:39:03

问题


if we have an array that contain strings for example {"houssam","hassan","taleb"}

and we have a string = "ss"

I need to return an array that return the string which contain ss, so in this case we have {"houssam","hassan"}

What is the best method to do that?

Thanks,


回答1:


You can try this:

let string = "ss"
let array = ["houssam","hassan","taleb"]
let filtered = array.filter() { $0.containsString(string) }



回答2:


let filterUsers:[String] = []
let users = [String] = ["houssam","hassan","taleb"]
let searchString = "ss"
filteredUsers = users.filter({( user: String) -> Bool in
    let stringMatch = user.rangeOfString(searchString)
    return stringMatch != nil
})

We filter the array of users and check to see if and where it contains "ss". It adds everything to the array where it found "ss" at least once.




回答3:


This is my demo. var arr: Array = ["loveyou","loveher","hateyou"] var Newarr = arr.filter({ $0.containsString("love")}) print(Newarr)



来源:https://stackoverflow.com/questions/33648498/swift-return-array-from-another-array-with-contain-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!