美文网首页
Swift学习笔记·String

Swift学习笔记·String

作者: JokerAIF | 来源:发表于2016-03-08 09:36 被阅读0次

    初学 String 比较关注 String 的基本使用方法

    • String 的访问/遍历方法
      因为不同的字符串可能需要不同数量的内存来存储,所以为了确定哪些character在特定的位置上,我们必须遍历确定每个Unicode的开始结束位置,因此,String不能使用整形作索引。
    let greeting = "Guten Tag!"  
    greeting[greeting.startIndex]  
    //G
    greeting[greeting.endIndex.predecessor()]  
    //!
    greeting[greeting.startIndex.successor()]  
    //u
    let index = greeting.startIndex.advancedBy(7)  
    //a
    greeting[index]  
    //输出 a
    

         使用for-in循环来遍历String的:

    for index in greeting.characters.indices {  
        print("\(greeting[index])", terminator: " ")
    }
    ///prints "G u t e n  T a g !"
    

    相关文章

      网友评论

          本文标题:Swift学习笔记·String

          本文链接:https://www.haomeiwen.com/subject/wikzkttx.html