//TODO:字符串
func stringAp(){
let a = 10
let b = "trut"
//转换
var c = String(a)
var e = String(a+a)+"tf"
var d = "\(a)"
var z = intptr_t("444")!
//拼接
var f = "I have \(a) apples."
let g = "I have \(b + b) pieces of fruit."
var h = d.appending(c);
var myStr = "Hello"
//查 字符串下标元字符
var sub = myStr[myStr.startIndex] //0下标 T
sub = myStr[myStr.index(before: myStr.endIndex)] //之前一个
sub = myStr[myStr.index(after: myStr.startIndex)] //之后一个
sub = myStr[myStr.index(myStr.startIndex, offsetBy: 2)] //偏移量
/*sub = myStr[myStr.endIndex] ;myStr[myStr.index(before: myStr.startIndex)] 这两种情况会越界 */
//字符串截取
let wef = myStr.substring(to: myStr.index(before: myStr.endIndex)) //截取从0下标到目标位置中间的字符串 Hello--Hell
let wef2 = myStr.substring(from: myStr.index(after: myStr.startIndex)) //截取从包含目标下标开始到最后的字符串 Hello--ello
let wef3 = myStr.substring(with: myStr.index(after: myStr.startIndex)..<myStr.index(myStr.endIndex, offsetBy: -2)) //去掉左右两端,剩下中间部分 Hello--el
//增
myStr.insert("!", at: myStr.endIndex) //Hello!
myStr.insert(contentsOf: "Man".characters, at: myStr.endIndex) //Hello!Man
myStr.insert(contentsOf: "!".characters, at: myStr.index(before: myStr.endIndex)) //Hello!Ma!n
//删
/*myStr.remove(at: myStr.endIndex) 越界*/
myStr.remove(at: myStr.index(before: myStr.endIndex)) // Hello!Ma!
let range = myStr.index(myStr.endIndex, offsetBy: -6)..<myStr.endIndex
myStr.removeSubrange(range) //Hel
//改
myStr = "Hello"
let newStr = myStr.replacingOccurrences(of: "el", with: "EL") //HEL
let newStr2 = newStr.replacingOccurrences(of: "E", with: "e") //HeL
myStr.replaceSubrange(myStr.startIndex..<myStr.index(myStr.startIndex, offsetBy: 2), with: "dwe")
myStr.replaceSubrange(myStr.startIndex..<myStr.index(myStr.startIndex, offsetBy: 2), with: "abc")
myStr.replaceSubrange(myStr.startIndex..<myStr.index(myStr.startIndex, offsetBy: 2), with: "efg")
var sdfeb=myStr.replacingCharacters(in: myStr.index(myStr.endIndex, offsetBy: -2)..<myStr.endIndex, with: "allo") // efgcello
//是否为空字符串
var boo:Bool = f.isEmpty
boo = (boo as? Bool)!
//Character 元字符
let charac : Character? = "t"
//遍历字符串每个characters
for character in f.characters {
print("通过遍历字符数组\(character)")
}
for index in f.characters.indices {
print("通过下标\(f[index])")
}
//characters数组转String
let character2:[Character] = ["v","w","c","e"]
let str:String? = String(character2)
}