0

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,

3 Answers 3

7

You can try this:

let string = "ss"
let array = ["houssam","hassan","taleb"]
let filtered = array.filter() { $0.containsString(string) }
Sign up to request clarification or add additional context in comments.

Comments

0
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.

Comments

0

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

1 Comment

Tip: If you use the ` symbol to frame your code snippets. It would look like let myFoo = "Hello World". It would has a better readability.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.