本系列为我本人在学习 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"))
‘’‘