//: Playground - noun: a place where people can play
import UIKit
// # 字符串字面量
// 多行字符串字面量,也是String類型。String理論上也能做到多行,只是這樣更方便了。
let quotation = """
The White Rabbit put on his spectacles. "Where shall I begin,
please your Majesty?" he asked.
"Begin at the beginning," the King said gravely, "and go on
till you come to the end; then stop."
"""
// 在多行字符串中使用三引號,用反斜杠轉譯至少一個雙引號,借此避免真三引號的出現。另注意縮進以結尾三引號為準。
let threeDoubleQuotes = """
Escaping the first quote \"""
Escaping all three quotes \"\"\"
"""
print(threeDoubleQuotes)
// # 初始化一個空字符串
// trick: 檢查是否為空
var emptyString = ""
if emptyString.isEmpty {
print("Nothing to see here")
}
// # 字符串可變性
// # 字符串是值類型
// trick: 字符串是值類型,在c中改變a、b對a、b本身無影響
var a = "wanna"
var b = "gonna"
var c = [a, b]
// # 操作字符
// for-in loop
for character in "Dog!??" {
print(character)
}
// 單獨創建字符類型,但用的仍是字符串字面量
let exclamationMark: Character = "!"
// String值可以通過傳入 Character值的字符串作為實際參數到它的初始化器來構造,這是因為String有這樣的init
let catCharacters: [Character] = ["C", "a", "t", "!", "??"]
let catString = String(catCharacters)
// # 連接字符串和字符
var welcome = ""
welcome += "Bonjour"
welcome.append(exclamationMark)
// # 字符串插值
// # Unicode
// Unicode 是一種在不同書寫系統中編碼、表示和處理文本的國際標準。不是所有的Unicode標量碼都對應著字符
// 字符串字面量中的轉譯特殊字符
"\0"http:// 空字符
"\\"http:// 反斜杠
"\t"http:// 水平制表符
"\n"http:// 換行符,n是指newline
"\r"http:// 回車符,r是指return,以上兩者在mac下沒有什么區別???
"\""http:// 雙引號
"\'"http:// 單引號
print("hao\rde")
"\u{1F443}" // 用Unicode標量碼
// 擴展字形集群
let precomposed: Character = "\u{D55C}" // ?
let decomposed: Character = "\u{1112}\u{1161}\u{11AB}" // ?, ?, ?
let enclosedEAcute: Character = "\u{E9}\u{20DD}" // 封閉標記
// 區域指示符號的 Unicode 標量可以成對組合來成為單一的 Character值
let regionalIndicatorForUS: Character = "\u{1F1FA}\u{1F1F8}"
// # 字符統計
// 注意.count屬性要遍歷所有Unicode標量碼才能斷定字符個數,而不是簡單的Unicode碼個數
var word = "cafe"
word.count
word += "\u{301}" // combined
word.count
// # 訪問和修改字符串
// String不是[Character],下標運算不能用整數,只能用index。原因就在于擴展字形集群導致每個字符所占大小可以變化,用整數就會出現一個字符的某個部分這種情況
// endIndex屬性是 String中最后一個字符后的位置,并非最后一個字符位置.如果 String為空,則 startIndex與 endIndex相等。
var greeting = "Bonjour!"
greeting[greeting.startIndex]
greeting[greeting.index(after: greeting.startIndex)]
greeting[greeting.index(before: greeting.endIndex)]
// 用offsetBy來向右偏移
greeting[greeting.index(greeting.startIndex, offsetBy: 1)]
// .indices獲得了所有有效的index(無endIndex)
for index in greeting.indices {
print("\(greeting[index])", terminator: "")
}
// 可以在任何遵循了 Indexable 協議的類型中使用 startIndex 和 endIndex 屬性以及 index(before:) , index(after:) 和 index(_:offsetBy:) 方法。這包括這里使用的 String ,還有集合類型比如 Array , Dictionary 和 Set 。
var test = [1, 2, 3]
test[test.startIndex]
test[0]
// 刪除,remove(at:)返回了一個Character
greeting.remove(at: greeting.index(before: greeting.endIndex))
greeting.removeSubrange(greeting.startIndex..<greeting.endIndex)
// 插入
greeting.insert(contentsOf: ", wee", at: greeting.endIndex)
greeting.insert("!", at: greeting.endIndex)
// 可以在任何遵循了 RangeReplaceableIndexable 協議的類型中使用 insert(_:at:) , insert(contentsOf:at:) , remove(at:) 方法。這包括了這里使用的 String ,同樣還有集合類型比如 Array , Dictionary 和 Set.
// # 字符串比較
// 只要String或Character的擴展字形集群擁有相同的語言意義和外形,我們就說它規范化相等,就算它們實際上是由不同的 Unicode 標量組合而成。
// 調用hasPrefix(_:),hasSuffix(_:)返回布爾值來判斷字符串是否擁有特定前后綴
// # 字符串的Unicode表示法
// 字符串的Unicode標量會被編碼為一定格式
// UTF-8表示法, UTF-16表示法, Unicode標量表示法
3.字符和字符串 Strings and Characters Swift官方文檔——版納的筆記
最后編輯于 :
?著作權歸作者所有,轉載或內容合作請聯系作者
- 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
- 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
- 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
推薦閱讀更多精彩內容
- 字符串和字符 (Strings and Characters) 自從蘋果2014年發布Swift,到現在已經兩年多...
- 初始化空字符串 (Initializing an Empty String) 要創建一個空字符串作為初始值,可以將...