阳仔博客

阳仔

iOS - 字符串操作for遍历

使用for循环遍历字符串,并获取字符,包含正序遍历字符串,逆序遍历字符串

let string:String = "Hello, playground"
/// 正序遍历字符串
for i in 0 ..< string.characters.count {
    let c = (str as NSString).characterAtIndex(i)
    let cStr = Character(UnicodeScalar(c))
    print(cStr)
}

/// 逆序遍历字符串
let startIndex = string.characters.count - 1
for i in startIndex.stride(to: -1, by: -1) {
    let c = (str as NSString).characterAtIndex(i)
    let cStr = Character(UnicodeScalar(c))
    print(cStr)
}
阅读更多
版权声明:阳仔原创,转载请注明出处。 https://blog.csdn.net/honey199396/article/details/52353824
文章标签: ios string 遍历
个人分类: iOS
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭