swift Character类型详解及实例
1、遍历字符串
// for-in 遍历String
for c in str.characters{
print(c)
}
//c是Character类型
2、Character声明
// Character和String的相互转换
//let mark = "!" //此时mark是String类型
let mark: Character = "!" //Character必须要显示声明
//str + mark //类型不同拼接会报错
str + String(mark) //进行类型转换后再拼接
// append 相当于 +=
str.append(mark)
//var类型才可以调用append方法,let类型不可以
3、Character类型的特性
// Characters是基于Unicode的
let englishLetter: Character = "a"
let chineseLetter: Character = "哈"
let dog: Character = "��"
let coolGuy = "\u{1F60E}"
和别的语言不一样,englishLetter、chineseLetter、dog、coolGuy这几个Character的单独都为1
// String对Unicode的支持
var coolLetters = "abc" //abc
coolLetters.characters.count //3
var chineseLetters = "你好吗" //你好吗
chineseLetters.characters.count //3
var coolGuys = "\u{1F60E}\u{1F60E}\u{1F60E}" //������
coolGuys.characters.count //3
Swift 的Character 非常智能,便于人类对字符的理解
var cafe = "café" //café
var cafe2 = "cafe\u{0301}" //café
cafe.characters.count //4
cafe2.characters.count //4
// String的比较
cafe == cafe2
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
您可能感兴趣的文章:详解Swift中的Characters字符类型与String字符串类型