dkfloza
dkfloza

Reputation: 41

How to break a word into characters in swift

How can i do the same thing in swift 2 :

String a = "danial";
  for (int i= 0 ; i < a.length() ; i++) {
    System.out.println(a.charAt(i));
  }
}

Upvotes: 1

Views: 401

Answers (4)

Shobhit C
Shobhit C

Reputation: 847

As of Swift 4.*,

let stringArray = Array(wordString)

Upvotes: 0

petegrif
petegrif

Reputation: 69

Let a = array("danial")  // a = ["d", "a", "n", "i", "a", "l"]

i.e. array of Character

Upvotes: -1

Bhanu
Bhanu

Reputation: 1249

Try this:

It was initial created.

let string : String = "danial"
let characters = Array(string)
println(characters)
//["d","a","n","i","a","l"]

String conforms to the SequenceType protocol, and its sequence generator enumerates the characters.

Swift 2, String does no longer conform to SequenceType, but the characters property provides a sequence of the Unicode characters:

let string = "danial"
let characters = Array(string.characters)
print(characters)
//["d","a","n","i","a","l"]

Upvotes: 0

iRiziya
iRiziya

Reputation: 3245

Try this :

let myString = "danial"
let characters = Array(myString.characters)
print(characters) //OP : ["d","a","n","i","a","l"]
print(characters[0]) //OP : ["d"]

Upvotes: 2

Related Questions