1.String
字符串是一系列字符组成的。Swift字符串由String类型表示。
let name: String = ""2.多行字符串
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"""
let multilineString = """
These are the same.
"""
print(quotation)
print(multilineString)3.初始化
let name = ""
let name2: String = ""
let name3 = String()4.字符串判空
if name.isEmpty {
print(name)
}5.字符遍历
let name = "abc"
for char in name {
print(char)
}6.拼接字符串
var a = "sdfk"
var b = "dfdk"
let c = a + b
a += c
b.append("bbbb")
print("a的数\(a)")
print("b的数\(b)")
print("c的数\(c)")7.字符个数
var a = "sdfk"
print(a.count)8.访问字符串
let title = "AbcdEfghHikjLemn"
//根据下标,获取值
print(title[title.index(after: title.startIndex)])//b
print(title[title.index(before: title.endIndex)])//n
print(title[title.index(title.startIndex,offsetBy: 0)])//A
print(title[title.index(title.endIndex,offsetBy: -2)])//m9.插入字符串
var word = "hello"
//插入单个字符
word.insert("!", at: word.endIndex)
//插入多个字符
word.insert(contentsOf: "abc", at: word.endIndex)
//在第一个字符插入
word.insert(contentsOf: "kf", at: title.index(title.startIndex, offsetBy: 1))
print(word)10.删除字符串
var removeStr = "hello word"
//删除第一个
removeStr.removeFirst()
//删除最后一个
removeStr.removeLast()
//删除指定位置
removeStr.remove(at: removeStr.startIndex)
//删除范围内的
let range = removeStr.startIndex...removeStr.index(removeStr.startIndex, offsetBy: 1)
removeStr.removeSubrange(range)
print(removeStr)11.截取字符串
var fixStr = "helld word"
//截取前面的
var substr1 = fixStr.prefix(3)
//截取后面的
var substr2 = fixStr.suffix(3)
//截取范围内的
var fixRange = fixStr.startIndex..<fixStr.index(fixStr.startIndex, offsetBy: 3)
var substr3 = fixStr[fixRange]
print(substr3)12.替换字符串
var fixStr = "helld word"
var substr4 = fixStr.replacingOccurrences(of: "word", with: "String")//hello String下篇文章:swift从入门到精通1.3-Dictionary