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!
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!
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)
}