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,
You can try this:
let string = "ss"
let array = ["houssam","hassan","taleb"]
let filtered = array.filter() { $0.containsString(string) }
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.
This is my demo.
var arr: Array = ["loveyou","loveher","hateyou"]
var Newarr = arr.filter({ $0.containsString("love")})
print(Newarr)