How to pick specific letters from a string - Swift2

-1

How do I get a letra specified from a variable?

var variavel = "teste";

I'd like to get a letra separada , I need to put each letter in a different variable

Thank you!

    
asked by anonymous 22.01.2016 / 18:34

1 answer

3

You can access the characters property of a String and use it to extract array of its characters. If you want a array String for each Character you need to use the map method to transform [Character] into [String] .

let str = "teste"
let strCharactersArray = Array(str.characters) // ["t", "e", "s", "t", "e"]
let strStringArray = str.characters.map{String($0)} // ["t", "e", "s", "t", "e"]

For you to go through each letter of the String you can use the loop for in :

for character in str.characters {
    print(character)
}
    
22.01.2016 / 18:44