字符串和字符
今天继续学习swift中的字符串和字符
string类型是字符类型的一个集合,在swift中string类型是值类型。
声明空的字符串:
var emptyStr = ""
var anotheremptyStr = String()
字符串是可变的,可以通过 +
连接。
遍历字符串中的字符:
for c in "Char!?.".characters {
print(c)
}
字符和字符串互转:
let chas:[Character] = ["a","b","c"]
let str = String(chas)
print(str)
字符串判断相等: ==
, 不等: !=
let aa = [
"act 1,1","act 1,2",3","act 2,"act 3,1"
]
var preCount = 0
var sufCount = 0
for value in aa {
if value.hasPrefix("act 1") {
preCount++
}
if value.hasSuffix("1") {
sufCount++
}
}
print(preCount)
print(sufCount)
hasPrefix和hasSuffix判断字符串的前缀和后缀是否相等。