Swift学习笔记之字符和字符串

就是字符串和字符,没什么好说的

字符串操作

循环获取字符

var name = "Tomas";
for letter in name.characters{
    print(letter);
}

字符数组创建字符串

let cat : [Character] = ["m","y","c","😂"];
let catStr = String(cat);
print(catStr);

字符插入


let name = "Tomas";
var say = "my name is \(name)";
print(say);

计算字符数

let sayHello = "Hello 大家好,我是梁胖子";
print("say hello lenght:\(sayHello.characters.count)")

下标获取字符

let name = "Tomas!";

print(name[name.startIndex]);
print(name[name.index(before: name.endIndex)]);
print(name[name.index(name.startIndex, offsetBy: 4)])