Array help

How do i get the number of Strings that match in a String array?

Replies

Can you set some parameters for your "how"? If you want to do this in FP (functional programming) kind of way, you can use the reduce method:


let array = ["abc", "acb", "aabcc"]
let count = array.reduce (0) {
  $1.contains ("abc") ? $0 + 1 : $0
}
print (count) // prints: 2


Or you can use filter:


let filtered = array.filter {
  $0.contains ("abc")
}
print (filtered.count) // prints: 2


Or you can do it procedurally using a for…in loop.


What exactly you do depends on what you want, and which part of this is giving you trouble.