How to pick specific letters from a string - Swift2

Asked

Viewed 707 times

-1

How do I get one letra specifies a variable?

var variavel = "teste";

I’d like to take a letra separada, I need to put each letter in a different variable

Thank you!

  • 1

    I don’t understand why they were negative. + 1

1 answer

4

You can access the property characters of a String and use it to extract a array of your characters. If you want a array of String for each Character you need to use the method map to transform the [Character] in [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"]

To go through each letter of the Voce String you can use the loop for in:

for character in str.characters {
    print(character)
}
  • 1

    Perfect! Thank you very much!

  • 2

    @Brunobafilli if the answer solved your problem mark it as right by clicking on the sign ✓ to the left of the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.