Swift4.2基礎(chǔ)學(xué)習(xí)筆記(六)

參考資料與鏈接https://www.cnswift.org

Unicode

請參考Unicode

字符串字面量中的特殊字符

  • 轉(zhuǎn)義特殊字符 \0(空字符) , \\(反斜杠) , \t(水平制表符), \n(換行符)
  • 任意的 Unicode 標(biāo)量,寫作 \u{n},里邊的 n是一個 1-8 個與合法 Unicode 碼位相等的16進(jìn)制數(shù)字。
let wiseWords = "\"Imagination is more important than knowledge\" - Einstein"
// "Imagination is more important than knowledge" - Einstein
let dollarSign = "\u{24}" // $, Unicode scalar U+0024
let blackHeart = "\u{2665}" // ?, Unicode scalar U+2665
let sparklingHeart = "\u{1F496}" // ?, Unicode scalar U+1F496

擴(kuò)展字形集群

請參考擴(kuò)展字形集群

字符統(tǒng)計

在字符串中取回 Character值的總數(shù),使用字符串的 count屬性

let unusualMenagerie = "Koala ?, Snail ?, Penguin ?, Dromedary ?"
print("unusualMenagerie has \(unusualMenagerie.count) characters")
// Prints "unusualMenagerie has 40 characters"

訪問和修改字符串

字符串索引

每一個 String值都有相關(guān)的索引類型, String.Index,它相當(dāng)于每個 Character在字符串中的位置。
從 String的開頭或結(jié)尾遍歷每一個 Unicode 標(biāo)量,Swift 的字符串不能通過整數(shù)值索引。

let greeting = "Guten Tag!"
greeting[greeting.startIndex]
// G
greeting[greeting.index(before: greeting.endIndex)]
// !
greeting[greeting.index(after: greeting.startIndex)]
// u
let index = greeting.index(greeting.startIndex, offsetBy: 7)
greeting[index]
// a

注意
endIndex是字符串最后一個元素位置 + 1 的位置,直接使用endIndex會報錯
(endindex是一個sequence的結(jié)尾,但不是最后一個元素,就像C語言的字符串結(jié)尾EOF,是不能訪問的)

  • 你可以在任何遵循了 Indexable 協(xié)議的類型中使用 startIndex 和 endIndex 等屬性,例如String,Array,Dictionary 和 Set

插入和刪除

  • 字符串的特定索引位置插入字符,使用 insert(_:at:)方法
  • 字符串的特定索引位置插入字符串,使用 insert(contentsOf:at:) 方法
var welcome = "hello"
welcome.insert("!", at: welcome.endIndex)
// welcome now equals "hello!"
 
welcome.insert(contentsOf: " there", at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there!"
  • 從字符串的特定索引位置移除字符,使用 remove(at:)方法
  • 移除一小段特定范圍的字符串,使用 removeSubrange(_:) 方法
welcome.remove(at: welcome.index(before: welcome.endIndex))
// welcome now equals "hello there"
 
let range = welcome.index(welcome.endIndex, offsetBy: -6)..<welcome.endIndex
welcome.removeSubrange(range)
// welcome now equals "hello"

注意
你可以在任何遵循了 RangeReplaceableIndexable 協(xié)議的類型中使用 insert(_:at:) , insert(contentsOf:at:) , remove(at:) 方法

子字符串

使用下標(biāo)或者類似 prefix(_:) 的方法——結(jié)果是一個 Substring 的實(shí)例,不是另外一個字符串。
當(dāng)你想要把結(jié)果保存得長久一點(diǎn)時,你需要把子字符串轉(zhuǎn)換為 String 實(shí)例.
只要這個字符串有子字符串在使用中,那么這個字符串就必須一直保存在內(nèi)存里。

let greeting = "Hello, world!"
let index = greeting.index(of: ",") ?? greeting.endIndex
let beginning = greeting[..<index]
// beginning is "Hello"
 
// Convert the result to a String for long-term storage.
let newString = String(beginning)
子字符串關(guān)系圖

字符串比較

字符串和字符相等性

使用 == 或 !=
更多詳細(xì)解釋參照字符串和字符相等性

前綴和后綴相等性

調(diào)用字符串的 hasPrefix()和 hasSuffix()方法,返回Bool值

字符串的Unicode表示法

參照字符串的 Unicode 表示法

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容