標籤:style class code com 使用 string
Working with Characters (與字元相關)
在Swift中,String類型表示一組有序字元的值.每個字元都是一個Unicode符號.可以使用for-in迴圈來遍曆字串中的每個字元:
for character in "Dog!??" {
println(character)
}
// D
// o
// g
// !
// ??
在Swift中也可以使用Character類型來顯式的建立一個單字元的常量或者變數:
let yenSign: Character = "¥"
Counting Characters (計算字元個數)
調用全域函數countElements,傳遞一個字串作為它的參數,可以計算出這個字串中字元的數量:
let unusualMenagerie = "Koala ??, Snail ??, Penguin ??, Dromedary ??"
println("unusualMenagerie has \(countElements(unusualMenagerie)) characters")
// prints "unusualMenagerie has 40 characters”
注意點:
不同的Unicode字元,以及相同Unicode字元不同的表示,他們需要的記憶體儲存空間是不同的.因此字元與表示這個字元的字串有時候的儲存空間也是不同的.這樣以來,一個字串的 長度就必須依據通過遍曆它的每個字元來計算獲得.如果你要處理一個特別長的字串,請注意,為了準確的計算出字串的長高度,一定要確保countElements函數遍曆了每個字元.
countElements函數返回的字元數與NSString的length屬性工作表示的字元數會有不一致的時候.NSString的length是根據UTF-16格式編碼的字串中16位單元碼個數來計算的,而不是 依據字串中Unicode字元個數來計算的.根據這種情況,NSString的length屬性在Swift中被視為 utf16count.
Concatenating Strings and Characters (連結字串和字元)
字串和字元的值可以通過加號(+)連結後建立一個新的字串:
let string1 = "hello"
let string2 = " there"
let character1: Character = "!"
let character2: Character = "?"
let stringPlusCharacter = string1 + character1 // equals "hello!"
let stringPlusString = string1 + string2 // equals "hello there"
let characterPlusString = character1 + string1 // equals "!hello"
let characterPlusCharacter = character1 + character2 // equals "!?”
也可以使用複合加法操作符(+=)來給一個已經定義的字串再追加一個字串或字元.
var instruction = "look over"
instruction += string2
// instruction now equals "look over there"
var welcome = "good morning"
welcome += character1
// welcome now equals "good morning!”
注意點:
不能給一個字元變數追加一個字串或者字元,因為一個字元類型的值只能儲存一個字元.
String Interpolation (字串拼接插入操作)
字串的拼接插入操作是構建新的字串的一種方式,通過混合組織常量,變數,文本以及運算式的值在這個字串文本中.插入到字串文本中的每個條目都用括弧()包住,並在前面寫 上飯斜杠(\):
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的實際值取代,從而建立一個明確的字元 串.
字串末端的multiplier的值是長運算式的一部分.運算式計算出 Double(multiplier) * 2.5的值,然後將計算結果值 (7.5)插入到字串中.在例子中,是使用 \(Double(multiplier) * 2.5)方式將值包含到字串中.
注意點:
寫字括弧()裡面的運算式不能包含雙引號”或者反斜線\,也不能包含斷行符號或者分行符號.
Comparing Strings (字串比較)
在Swift中提供了三種方式比較字串:字串相等,首碼相等,和尾碼相等
String Equality (字串相等)
如果兩個字串包含的相同次序的字元,那麼這兩個字串相等:
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”
Prefix and Suffix Equality (首碼和尾碼相等)
調用字串的 hasPrefix 和 hasSuffix方法,可以檢查一個字串中是否有指定的首碼或尾碼字串.這兩個方法都只帶一個String類型的參數,並返回一個布爾類型的值.這個方法都會 進行逐字元的進行比較.
下面的代碼例子中,字串數組表示了Shakespeare’s Romeo and Juliet(莎士比亞的羅密歐與 朱麗葉)前兩章情景:
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方法來計算這個戲劇章節1中情境的數量:
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”
同樣的,你也可以使用Capulet’s mansion 和 Friar Lawrence’s cell作為參數,通過hasSuffix方法來計算情境數:
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”
Uppercase and Lowercase Strings (字串的大小寫轉換)
通過uppercaseString 和lowercaseString屬性,可以進行大寫或者小寫儲存訪問:
let normal = "Could you help me, please?"
let shouty = normal.uppercaseString
// shouty is equal to "COULD YOU HELP ME, PLEASE?"
let whispered = normal.lowercaseString
// whispered is equal to "could you help me, please?”