// STRINGS AS COLLECTIONS
let string = "Matt"
for char in string {
print(char)
}
let stringLength = string.count
//let fourthChar = string[3]
// GRAPHEME CLUSTERS
let cafeNormal = "café"
let cafeCombining = "cafe\u{0301}"
cafeNormal.count
cafeCombining.count
cafeNormal.unicodeScalars.count
cafeCombining.unicodeScalars.count
for codePoint in cafeCombining.unicodeScalars {
print(codePoint.value)
}
// INDEXING STRINGS
let firstIndex = cafeCombining.startIndex
let firstChar = cafeCombining[firstIndex]
//let lastIndex = cafeCombining
let lastIndex = cafeCombining.index(before: cafeCombining.endIndex)
let lastChar = cafeCombining[lastIndex]
let fourthIndex = cafeCombining.index(cafeCombining.startIndex, offsetBy: 3)
let fourthChar = cafeCombining[fourthIndex]
fourthChar.unicodeScalars.count
fourthChar.unicodeScalars.forEach { codePoint in
print(codePoint.value)
}
// EQUALITY WITH COMBINING CHARACTERS
let equal = cafeNormal == cafeCombining
// STRINGS AS BIDIRECTIONAL COLLECTIONS
let name = "Matt"
let backwardsName = name.reversed()
let secondCharIndex = backwardsName.index(backwardsName.startIndex, offsetBy: 1)
let secondChar = backwardsName[secondCharIndex]
let backwardsNameString = String(backwardsName)
// SUBSTRINGS
let fullName = "Matt Galloway"
let spaceIndex = fullName.index(of: " ")!
let firstName = fullName[..<spaceIndex]
let lastName = fullName[fullName.index(after: spaceIndex)...]
let lastNameString = String(lastName)
// ENCODING
let char = "\u{00bd}"
for i in char.utf8 {
print(i)
}
let characters = "+\u{00bd}\u{21e8}\u{1f643}"
for i in characters.utf8 {
print("\(i) : \(String(i, radix: 2))")
}
for i in characters.utf16 {
print("\(i) : \(String(i, radix: 2))")
}
let arrowIndex = characters.index(of: "\u{21e8}")!
characters[arrowIndex]
if let unicodeScalarsIndex = arrowIndex.samePosition(in: characters.unicodeScalars) {
characters.unicodeScalars[unicodeScalarsIndex]
}
if let utf8Index = arrowIndex.samePosition(in: characters.utf8) {
characters.utf8[utf8Index]
}
if let utf16Index = arrowIndex.samePosition(in: characters.utf16) {
characters.utf16[utf16Index]
}
String
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- org.springframework.beans.factory.UnsatisfiedDependencyEx...
- 用以下几种情况的代码及分析大致讲解 String s = "abc" 和 String s = new Strin...
- 先说几个概念吧 1.常量池 指的是在编译期确定,并被保存在已编译的字节码文件中的一些数据,它包括类、方法、接口等中...