美文网首页
从 0 到 1 实现 Swift App (一)字符串篇

从 0 到 1 实现 Swift App (一)字符串篇

作者: 朵朵一花浪 | 来源:发表于2021-12-31 11:45 被阅读0次

本系列为我本人在学习 Swift 过程记录,从零开始长时间更新,直到学习完成,该篇介绍 swift 字符串的简单使用
‘’‘

    // 插值
     var welcome = "hello"
     var welcome1 = "hello"

     print(welcome == welcome1)

     welcome1 += ",";

     print(welcome1);
     print(welcome);
     
     for c in welcome1 {
     print(c);
     }
     
     print(#"6+7 = \#(6+7)"#)
     print("6+7 = \(6+7)")
    
     // 字符串增删

     var welcome = "hello";
     welcome.insert("!", at: welcome.endIndex);
     print(welcome)
     
     welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))
     print(welcome)
     
     let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
     
     welcome.removeSubrange(range);
     print(welcome)

     let welcome = "hello,world";
     let index = welcome.firstIndex(of: ",") ?? welcome.endIndex
     let substring = welcome[..<index]
     print(index)
     print(substring)
     
     let welcome1 = String(substring);
     print(welcome1 == welcome);
     
     print(welcome.hasPrefix("hello"))
     print(welcome.hasSuffix("world"))

‘’‘

相关文章

网友评论

      本文标题:从 0 到 1 实现 Swift App (一)字符串篇

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