AAA AAA - 1 year ago 89
iOS Question

How to get first letter of all strings in an array in iOS Swift?

I have a group of strings stored in an array.

stringArray = [Aruna, Bala, Chitra, Divya, Fatima]

I want to get the first letters of all the strings and store it in an array.
letterArray = [A, B, C, D, F]

Note: They are not within quotes "--"

Answer Source

Not sure what you mean by 'they are not within quotes', but if they are actually Strings then something like this:

var letterArray = [Character]()
for string in stringArray {


To have it as String instead as you wish:

var letterArray = [String]()
for string in stringArray {


As Leo Dabus suggests, if you pass an empty string the above will fail. If you know there will never be an empty string this doesn't apply, but I've updated the above to handle this case:

var letterArray = [String]()
for string in stringArray {
    if let letter = string.characters.first {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download