问题
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