swift Character类型详解及实例
1、遍历字符串
// for-in 遍历Stringfor 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" //abccoolLetters.characters.count //3var chineseLetters = "你好吗" //你好吗chineseLetters.characters.count //3var 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 //4cafe2.characters.count //4// String的比较cafe == cafe2
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
新闻热点
疑难解答
图片精选