【Swift學習】Swift編程之旅---字符與字符串(五)

  String是swift的字符串類型。一個字符串是一個有效的字符序列,所以還可使字符集合表示。經過+符號能夠鏈接字符串。 String 類型是一種快速、現代化的字符串實現。每個字符串都是由獨立編碼的 Unicode 字符組成,並提供了用於訪問這些字符在不一樣Unicode表示的支持。使用""來標示字符串。swift

  1、初始化空字符串  數組

var emptyString = ""    
var anotherEmptyString = String()

 

 這2種初始化方法是等價的。app

isEmpty能夠判斷當前字符串是否爲空字符串。ide

if emptyString.isEmpty { print("Nothing to see here") }

 

 Swift 的 String 類型是值類型。若是您建立了一個新的字符串值,那麼當其進行常量、變量賦值操做或在函數/方法中傳遞時,會進行值拷貝。在不一樣狀況下,都會對已有字符串值建立新副本,並對該新副本進行傳遞或賦值。函數

 

  2、字符(Characters)的使用編碼

  你能夠經過for-in循環來訪問字符串單獨的字符spa

for character in "Dog!🐶".characters { print(character) }

 

另外,經過標明一個 Character 類型註解並經過字符字面量進行賦值,能夠創建一個獨立的字符常量或變量:
let yenSign: Character = "¥" 

 字符串值可使用傳遞一個字符數組來構造。3d

let catCharacters: [Character] = ["C", "a", "t", "!", "🐱"] let catString = String(catCharacters) print(catString) // Prints "Cat!🐱
 

   

  3、鏈接字符串和字符code

  字符串值能夠經過+符號來實現字符串鏈接來創建新的字符串值。orm

let string1 = "hello" let string2 = " there" var welcome = string1 + string2 // welcome now equals "hello there

 

 您還可使用賦值操做符(+ =)將一個字符串值附加到一個已有的字符串變量

var instruction = "look over" instruction += string2 // instruction now equals "look over there

 

 使用append()方法講一個字符值附加到一個字符串值後面。

let exclamationMark: Character = "!" welcome.append(exclamationMark) // welcome now equals "hello there!

 

 你不能將字符串或字符附加到現有的字符變量,由於字符值必須包含一個字符

 

  4、字符串插值

  字符串插值是一種全新的構建字符串的方式,能夠在其中包含常量、變量、字面量和表達式。您插入的字符串字面量的每一項都被包裹在以反斜線爲前綴的圓括號中:

let multiplier = 3 let message = "\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)" // message is "3 times 2.5 is 7.5

 

在上面的例子中,multiplier 做爲 \(multiplier) 被插入到一個字符串字面量中。當建立字符串執行插值計算時此佔位符會被替換爲 multiplier 實際的值。
 
multiplier 的值也做爲字符串中後面表達式的一部分。該表達式計算 Double(multiplier) * 2.5 的值並將結果 (7.5) 插入到字符串中。在這個例子中,表達式寫爲 \(Double(multiplier) * 2.5) 幷包含在字符串字面量中。

 注意:您插值字符串中寫在括號中的表達式不能包含非轉義雙引號 (") 和反斜槓 (\),而且不能包含回車或換行符。

 

   5、字符計數

  咱們可使用String的characters屬性的count屬性來取得字符串的字符數也就是字符串的長度。看以下例子

 

輸出

 

  注意swift的特徵值擴展字形集羣的使用意味着字符串鏈接和修改可能不會影響一個字符串的字符數

輸出

 

  6、訪問和修改字符串(Accessing and Modifying a String)

 

  咱們經過String的方法和屬性,或使用下標語法訪問和修改字符串。

  

  1.字符串索引

  每個字符串值都關聯一個索引類型,String.Index,該索引對應於字符串中的每一個字符的位置,不一樣的角色須要不一樣數量的內存來存儲,因此爲了肯定哪些字符是在一個特定的位置,你必須遍歷每一個Unicode標量從字符串的開始或結束。基於這個緣由,沒法用整型值來索引swift的字符串。 

  使用startIndex屬性來訪問字符串的第一個字符的位置,使用endIndex屬性來訪問最後一個字符以後的位置,做爲一個結果,endIndex屬性不是一個有效的字符串下標的參數。

若是一個字符串爲空,那麼startIndexendIndex是相等的。

  String.Index值可使用successor()方法來訪問它的下一個字符。試圖訪問一個字符串的範圍以外的索引將觸發一個運行時錯誤。使用predecessor()方法來訪問字符串的上一個字符

下面經過一個例子來講明這些方法

輸出結果

當咱們試圖這樣寫代碼時

print(greeting[greeting.startIndex.predecessor()])訪問起始位置的上一個字符

print(greeting[greeting.endIndex.successor()])訪問末尾位置的下一個字符

都會發生運行時錯誤

 

使用characters屬性的indices屬性來建立一個全部索引的範圍

 

  2.插入和刪除(Inserting and Removing)

使用insert(_:atIndex:)向一個字符串在指定位置的插入一個字符

var welcome = "hello" welcome.insert("!", atIndex: welcome.endIndex) // welcome now equals "hello!

 使用insertContentsOf(_:at:)向一個字符串在指定位置的插入一個字符串

welcome.insertContentsOf(" there".characters, at: welcome.endIndex.predecessor()) // welcome now equals "hello there!

 

使用removeAtIndex(_:)向一個字符串在指定位置刪除一個字符

welcome.removeAtIndex(welcome.endIndex.predecessor()) // welcome now equals "hello there

 

使用removeRange(_:)向一個字符串在指定位置刪除一段子字符串

let range = welcome.endIndex.advancedBy(-6)..<welcome.endIndex welcome.removeRange(range) // welcome now equals "hello"

 

   7、比較字符串

  Swift 提供了三種方式來比較字符串的值:字符串相等,前綴相等和後綴相等。
 
  1. 字符串相等
  若是兩個字符串以同一順序包含徹底相同的字符,則認爲二者字符串相等:
let quotation = "We're a lot alike, you and I." let sameQuotation = "We're a lot alike, you and I." if quotation == sameQuotation { println("These two strings are considered equal") } // prints "These two strings are considered equal"

 

  2.前綴/後綴相等

經過調用字符串的 hasPrefix/hasSuffix 方法來檢查字符串是否擁有特定前綴/後綴。兩個方法均須要以字符串做爲參數傳入並返回 Boolean 值。兩個方法均執行基本字符串和前綴/後綴字符串之間逐個字符的比較操做。
 
下面的例子以一個字符串數組表示莎士比亞話劇《羅密歐與朱麗葉》中前兩場的場景位置
let romeoAndJuliet = [ "Act 1 Scene 1: Verona, A public place", "Act 1 Scene 2: Capulet's mansion", "Act 1 Scene 3: A room in Capulet's mansion", "Act 1 Scene 4: A street outside Capulet's mansion", "Act 1 Scene 5: The Great Hall in Capulet's mansion", "Act 2 Scene 1: Outside Capulet's mansion", "Act 2 Scene 2: Capulet's orchard", "Act 2 Scene 3: Outside Friar Lawrence's cell", "Act 2 Scene 4: A street in Verona", "Act 2 Scene 5: Capulet's mansion", "Act 2 Scene 6: Friar Lawrence's cell" ] 

利用 hasPrefix 方法使用romeoAndJuliet數組來計算話劇中第一幕的場景數:

var act1SceneCount = 0 
for scene in romeoAndJuliet { if scene.hasPrefix("Act 1 ") { ++act1SceneCount } } println("There are \(act1SceneCount) scenes in Act 1") // prints "There are 5 scenes in Act 1" 

 

 一樣,可以使用hasSuffix方法來計算髮生在Capulet公館和Lawrence牢房內以及周圍的場景數。

var mansionCount = 0 
var cellCount = 0 
for scene in romeoAndJuliet { if scene.hasSuffix("Capulet's mansion") { ++mansionCount } else if scene.hasSuffix("Friar Lawrence's cell") { ++cellCount } } println("\(mansionCount) mansion scenes; \(cellCount) cell scenes") // prints "6 mansion scenes; 2 cell scenes" 

 

   3.大寫和小寫字符串

經過字符串的 uppercaseString 和 lowercaseString 屬性來獲取一個字符串的大寫/小寫版本。

let normal = "Could you help me, please?" let shouty = normal.uppercaseString // shouty 值爲 "COULD YOU HELP ME, PLEASE?" let whispered = normal.lowercaseString // whispered 值爲 "could you help me, please?" 
相關文章
相關標籤/搜索