How to filter an array in Swift similar to like?

Asked

Viewed 65 times

0

Let cast = ["Vivien", "Marlon", "Kim", "Karl"]
would like to return like ('%ar%')
"result expected [ "Marlon", "Karl"] "

  • Solved: for i in 0.. <cast.Count ː Let text = cast[i] if text.lowercased(). range(of:"ar") != nil' print(cast[i]) } }

1 answer

0

You can use the filter array to do this. Follow example:

import Foundation
let castArray = ["Vivien", "Marlon", "Kim", "Karl"]
let filtered = castArray.filter { $0.containsString("ar") }
print(filtered)

I hope I helped. Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.