I have an array as follows:
let array = ["sam", "andrew", "character"]
And I want to get every element in the array that starts with a character I type
My problem is when i type "a" the output is "sam", "andrew", and "character".
I want to get the output to only be "andrew". (The string must start from left to right when searching)
You need to filter your array using hasPrefix
var names = ["sam", "andrew", "character"]
var searchString = "a"
let filteredNames = names.filter({ $0.hasPrefix(searchString) })
print(filteredNames)
You must use string.hasPrefix(string)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With