0

I want to search in an array if it contains a string and if it is in the array return the index number where it is located.

I'm new to swift and the only thing I know is how to check if the string contains the string but I have no idea how to get the index from that array.

Yorrick

1 Answers1

3

If you want index of all the elements from array that contains string you can try like this.

let array = ["One","Two","Three","Four","Five"]
let indexArray = array.indices.filter { array[$0].localizedCaseInsensitiveContains("o") }

print(indexArray) // [0, 1, 3]
Nirav D
  • 71,513
  • 12
  • 161
  • 183