如何在swift中确定单词长度

时间:2016-06-18 14:31:33

标签: ios xcode swift

let word : String = "hello"

var wordLength : Int = word.length //what to do here ?

for i in wordLength {
    let index = word.startIndex.advancedBy(i)
    print(word[index])
}

我想单独打印变量字的所有字符,但我不知道如何确定单词何时结束,或者我必须循环直到整个单词被打印。

2 个答案:

答案 0 :(得分:0)

let word = "hello"

for i in word.characters {
    print(i)
}

答案 1 :(得分:0)

Swift 3:String + Emoji

let word = "Hello :-)  "

let wordLengthInt_0 = word.characters.count
// 12 (!!!!!)

let wordLengthInt_1 = word.utf16.count
// 13

let wordLengthInt_2 = (word as NSString).length
// 13